-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
262 lines (215 loc) · 7.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* eslint-env browser*/
var SignalStore = require('cerebral-module-signal-store')
var utils = require('./utils')
var requestAnimationFrame = requestAnimationFrame || function (cb) { setTimeout(cb) }
var staticTree = require('cerebral/src/staticTree')
module.exports = function Devtools () {
if (typeof window === 'undefined') { return function () {} }
if (
typeof window.chrome === 'undefined' &&
!process && !process.versions && !process.versions.electron
) { return function () {} }
return function init (module, controller) {
controller.addContextProvider(require('./providers/actionServicesCallsProvider'))
controller.addContextProvider(require('./providers/actionOutputProvider'))
controller.addContextProvider(require('./providers/actionInputProvider'))
controller.addContextProvider(require('./providers/signalOptionsProvider'))
module.addModules({
store: SignalStore()
})
module.addSignals({
modelChanged: [
function changeModel (arg) {
arg.state.set(arg.input.path, arg.input.value)
}
]
})
var signalStore = controller.getServices()[module.name].store
var isInitialized = false
var hasInitialPayload = false
var disableDebugger = false
var APP_ID = String(Date.now())
var VERSION = 'v4'
var isAwaitingFrame = false
var nextSignalInLine = 0
var hasExecutingSignal = function (signal) {
function traverseSignals (signals) {
return signals.reduce(function (hasExecutingSignal, signal) {
if (hasExecutingSignal || signal.isExecuting) {
return true
}
return traverseChain(signal.branches)
}, false)
}
function traverseChain (chain) {
return chain.reduce(function (hasExecutingSignal, action) {
if (hasExecutingSignal) {
return true
}
if (Array.isArray(action)) {
return traverseChain(action)
}
return traverseAction(action)
}, false)
}
function traverseAction (action) {
var hasExecutingSignal = false
if (action.outputPath) {
hasExecutingSignal = traverseChain(action.outputs[action.outputPath])
}
if (action.signals) {
hasExecutingSignal = hasExecutingSignal || traverseSignals(action.signals)
}
return hasExecutingSignal
}
if (signal.isExecuting) {
return true
}
return traverseChain(signal.branches)
}
var getOldestExecutingSignalIndex = function (signals, fromIndex) {
for (var x = fromIndex; x < signals.length; x++) {
if (hasExecutingSignal(signals[x])) {
return x
}
}
return signals.length - 1
}
var update = function (signalType, data, forceUpdate) {
if (!forceUpdate && (disableDebugger || !data || !hasInitialPayload)) {
return
}
var detail = {
type: signalType,
app: APP_ID,
version: VERSION,
data: data
}
var event = new CustomEvent('cerebral.dev.update', {
detail: JSON.stringify(detail)
})
window.dispatchEvent(event)
}
var getInit = function () {
var signals = signalStore.getSignals()
nextSignalInLine = signals.length ? getOldestExecutingSignalIndex(signals, nextSignalInLine) : 0
hasInitialPayload = true
return {
initialModel: controller.get(),
signals: signals,
disableDebugger: disableDebugger,
isExecutingAsync: signalStore.isExecutingAsync()
}
}
var updateSignals = function () {
if (isAwaitingFrame) {
return
}
isAwaitingFrame = true
requestAnimationFrame(function () {
var signals = signalStore.getSignals()
// In case last executed signal is now done
update('signals', {
signals: signals.slice(nextSignalInLine),
isExecutingAsync: signalStore.isExecutingAsync()
})
// Set new last executed signal
nextSignalInLine = signals.length ? getOldestExecutingSignalIndex(signals, nextSignalInLine) : 0
isAwaitingFrame = false
})
}
var updateSettings = function () {
update('settings', {
disableDebugger: disableDebugger
}, true)
}
window.addEventListener('cerebral.dev.components', function (event) {
update('components', event.detail, true)
})
window.addEventListener('cerebral.dev.debuggerPing', function () {
var signals = []
if (utils.hasLocalStorage()) {
disableDebugger = JSON.parse(localStorage.getItem('cerebral_disable_debugger'))
}
signalStore.setSignals(signals)
signalStore.rememberInitial(signalStore.getSignals().length - 1)
isInitialized = true
var event = new CustomEvent('cerebral.dev.cerebralPong', {
detail: JSON.stringify({
type: 'init',
app: APP_ID,
version: VERSION,
data: getInit()
})
})
window.dispatchEvent(event)
})
window.addEventListener('cerebral.dev.toggleDisableDebugger', function () {
disableDebugger = !disableDebugger
updateSettings()
})
window.addEventListener('cerebral.dev.resetStore', function () {
signalStore.reset()
controller.emit('change')
update()
})
window.addEventListener('cerebral.dev.remember', function (event) {
signalStore.remember(event.detail)
})
window.addEventListener('cerebral.dev.rewrite', function (event) {
var signals = signalStore.getSignals()
signals.splice(event.detail + 1, signals.length - 1 - event.detail)
signalStore.remember(event.detail)
})
window.addEventListener('cerebral.dev.logPath', function (event) {
var name = event.detail.name
var value = controller.get(event.detail.path)
// toValue instead?
console.log('CEREBRAL - ' + name + ':', value.toJS ? value.toJS() : value)
})
window.addEventListener('cerebral.dev.logModel', function (event) {
console.log('CEREBRAL - model:', controller.logModel())
})
window.addEventListener('cerebral.dev.changeModel', function (event) {
module.getSignals().modelChanged(event.detail)
})
window.addEventListener('unload', function () {
signalStore.removeRunningSignals()
if (utils.hasLocalStorage()) {
localStorage.setItem('cerebral_disable_debugger', isInitialized && JSON.stringify(disableDebugger))
}
})
document.addEventListener('visibilitychange', function () {
if (!document.hidden) {
updateSettings()
}
})
var services = {
update: update,
start: function () {
console.warn('Cerebral: devtools.start() method is deprecated. Devtools has started automatically.')
}
}
module.addServices(services)
controller.getDevtools = function () {
console.warn('Cerebral: controller.getDevtools() method is deprecated. Please upgrade your view package to latest version.')
return services
}
function start () {
if (window.__CEREBRAL_DEVTOOLS_GLOBAL_HOOK__) {
window.__CEREBRAL_DEVTOOLS_GLOBAL_HOOK__.signals = controller.getSignals()
window.__CEREBRAL_DEVTOOLS_GLOBAL_HOOK__.staticTree = staticTree
}
var event = new CustomEvent('cerebral.dev.cerebralPing')
window.dispatchEvent(event)
console.assert(controller.listeners('modulesLoaded')[0] === start, 'Cerebral devtools: Please do not place any listeners to `modulesLoaded` event before devtools\'s one.')
}
var listeners = controller.listeners('modulesLoaded')
controller.removeAllListeners('modulesLoaded')
controller.on('modulesLoaded', start)
listeners.forEach(function (listener) {
controller.on('modulesLoaded', listener)
})
controller.on('change', updateSignals)
}
}