forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutations.ts
More file actions
340 lines (299 loc) · 9.79 KB
/
mutations.ts
File metadata and controls
340 lines (299 loc) · 9.79 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { Mutation } from '../app'
import {
Apps,
Message,
Tab,
ActionsListItemType,
ActionItem,
ActionGroupItem,
} from './state'
import {
runMutation,
getActionId,
createApp,
ensureFlushExists,
getOperatorId,
} from './utils'
export const setApps: Mutation<Apps> = (state, apps) => {
if (!apps) {
state.isAddingPort = true
}
state.apps = apps || {}
}
export const configurePort: Mutation = (state) => (state.isAddingPort = true)
export const selectPort: Mutation<string> = (state, port) =>
(state.currentPort = port)
export const removeApp: Mutation = (state) => {
delete state.apps[state.currentPort]
const appPorts = Object.keys(state.apps)
if (appPorts.length) {
state.currentPort = appPorts[0]
} else {
state.isAddingPort = true
}
}
export const cancelConfigurePort: Mutation = (state) =>
(state.isAddingPort = false)
export const setCurrentPort: Mutation<string> = (state, currentPort) => {
if (currentPort) {
state.currentPort = currentPort
} else if (Object.keys(state.apps).length) {
state.currentPort = Object.keys(state.apps)[0]
}
}
export const setError: Mutation<string> = (state, error) =>
(state.error = error)
export const setAppLoaded: Mutation = (state) => (state.isLoading = false)
export const setNewPortValue: Mutation<string> = (state, value) =>
(state.newPortValue = value)
export const addNewApp: Mutation = (state) => {
state.apps[state.newPortValue] = createApp({
port: state.newPortValue,
})
state.isAddingPort = false
}
export const resetNewPortValue: Mutation = (state) => (state.newPortValue = '')
export const addMessagesFromClient: Mutation<Message> = (state, message) =>
(state.apps[message.port].messages = message.message
.reverse()
.concat(state.apps[message.port].messages))
export const changeTab: Mutation<Tab> = (state, tab) => (state.currentTab = tab)
export const toggleExpandStatePath: Mutation<string[]> = (state, path) => {
const pathString = path.join('.')
if (state.expandedStatePaths.indexOf(pathString) >= 0) {
state.expandedStatePaths.splice(
state.expandedStatePaths.indexOf(pathString),
1
)
} else {
state.expandedStatePaths = state.expandedStatePaths.concat(pathString)
}
}
export const performMutationsByMessageType: Mutation<Message> = (
state,
message
) => {
message.message.forEach((clientMessage) => {
switch (clientMessage.type) {
case 'init': {
state.apps[message.port] = createApp({
port: message.port,
state: clientMessage.data.state,
})
break
}
case 'flush': {
ensureFlushExists(
state.apps[message.port].flushes,
clientMessage.data.flushId
)
state.apps[message.port].flushes[clientMessage.data.flushId].mutations =
clientMessage.data.mutations
if (typeof clientMessage.data.operatorId === 'undefined') {
state.apps[message.port].flushByActionId[
getActionId(clientMessage.data)
] = {
flushId: clientMessage.data.flushId,
isCollapsed: true,
}
} else {
state.apps[message.port].flushByOperatorId[
getOperatorId(clientMessage.data)
] = {
flushId: clientMessage.data.flushId,
isCollapsed: true,
}
}
clientMessage.data.mutations.forEach(
runMutation(state.apps[message.port].state)
)
break
}
case 'component:add': {
const id = `${clientMessage.data.componentId}_${
clientMessage.data.componentInstanceId
}`
state.apps[message.port].components[id] = {
id,
isMounted: true,
updateCount: 0,
...clientMessage.data,
}
break
}
case 'component:update': {
const id = `${clientMessage.data.componentId}_${
clientMessage.data.componentInstanceId
}`
state.apps[message.port].components[id].paths = clientMessage.data.paths
state.apps[message.port].components[id].updateCount++
ensureFlushExists(
state.apps[message.port].flushes,
clientMessage.data.flushId
)
state.apps[message.port].flushes[
clientMessage.data.flushId
].components.push(id)
break
}
case 'component:remove': {
const id = `${clientMessage.data.componentId}_${
clientMessage.data.componentInstanceId
}`
state.apps[message.port].components[id].isMounted = false
break
}
case 'derived': {
const appState = state.apps[message.port].state
const path = clientMessage.data.path.split('.')
const key = path.pop()
const target = path.reduce((aggr, pathKey) => aggr[pathKey], appState)
target[key] = {
paths: clientMessage.data.paths,
updateCount: clientMessage.data.updateCount,
value: clientMessage.data.value,
}
state.apps[message.port].derived[clientMessage.data.path] =
clientMessage.data
break
}
case 'derived:dirty': {
ensureFlushExists(
state.apps[message.port].flushes,
clientMessage.data.flushId
)
state.apps[message.port].flushes[
clientMessage.data.flushId
].derived.push(clientMessage.data.path)
break
}
case 'computed': {
const appState = state.apps[message.port].state
const path = clientMessage.data.path.split('.')
const key = path.pop()
const target = path.reduce((aggr, pathKey) => aggr[pathKey], appState)
target[key] = {
cache: clientMessage.data.cache,
limit: clientMessage.data.limit,
updateCount: clientMessage.data.updateCount,
}
state.apps[message.port].computed[clientMessage.data.path] =
clientMessage.data
break
}
case 'computed:dirty': {
ensureFlushExists(
state.apps[message.port].flushes,
clientMessage.data.flushId
)
state.apps[message.port].flushes[
clientMessage.data.flushId
].computed.push(clientMessage.data.path)
break
}
case 'action:start': {
const app = state.apps[message.port]
const action = clientMessage.data
const id = getActionId(action)
const isSelectingFirstAction =
!app.currentActionId || app.currentActionId === app.actionsList[0].id
app.actions[id] = {
...action,
isRunning: true,
operators: {},
}
if (
!app.actionsList.length ||
app.actionsList[0].actionId !== action.actionId
) {
app.actionsList.unshift({
type: ActionsListItemType.ACTION,
id,
actionId: action.actionId,
})
} else if (app.actionsList[0].type === ActionsListItemType.ACTION) {
const existingId = (app.actionsList[0] as ActionItem).id
app.actionsList[0] = {
type: ActionsListItemType.GROUP,
id: getActionId(action),
actionId: action.actionId,
isCollapsed: true,
actionIds: [id, existingId],
}
} else if (app.actionsList[0].type === ActionsListItemType.GROUP) {
;(app.actionsList[0] as ActionGroupItem).actionIds.unshift(id)
;(app.actionsList[0] as ActionGroupItem).id = id
}
if (isSelectingFirstAction) {
app.currentActionId = id
}
break
}
case 'operator:start': {
const operator = clientMessage.data
const id = `${operator.actionId}_${operator.executionId}`
const action = state.apps[message.port].actions[id]
action.operators[operator.operatorId] = {
...operator,
isRunning: true,
isCollapsed: true,
mutations: [],
effects: [],
}
break
}
case 'operator:end': {
const operator = clientMessage.data
const id = `${operator.actionId}_${operator.executionId}`
const action = state.apps[message.port].actions[id]
action.operators[operator.operatorId].isAsync = operator.isAsync
action.operators[operator.operatorId].isRunning = false
action.operators[operator.operatorId].result = operator.result
break
}
case 'action:end': {
const app = state.apps[message.port]
const action = clientMessage.data
const id = `${action.actionId}_${action.executionId}`
app.actions[id].isRunning = false
break
}
case 'mutations': {
const mutations = clientMessage.data
const id = `${mutations.actionId}_${mutations.executionId}`
const operator =
state.apps[message.port].actions[id].operators[mutations.operatorId]
operator.mutations = mutations.mutations
break
}
case 'effect': {
const effect = clientMessage.data
const id = getActionId(effect)
const operator =
state.apps[message.port].actions[id].operators[effect.operatorId]
operator.effects.push(effect)
break
}
}
})
}
export const toggleActionItemCollapse: Mutation<string> = (state, actionId) => {
for (let index in state.currentApp.actionsList) {
const item = state.currentApp.actionsList[index]
if (
item.type === ActionsListItemType.GROUP &&
item.id === actionId &&
state.currentApp.currentActionId === actionId
) {
item.isCollapsed = !item.isCollapsed
break
}
}
}
export const selectAction: Mutation<string> = (state, actionId) =>
(state.currentApp.currentActionId = actionId)
type Collapse = {
isCollapsed: boolean
}
export const toggleCollapsed: Mutation<Collapse> = (_, item) =>
(item.isCollapsed = !item.isCollapsed)