-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredux.ts
385 lines (334 loc) · 12.4 KB
/
redux.ts
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import {
IAnyStateTreeNode,
isStateTreeNode,
getSnapshot,
applyAction,
onSnapshot,
getType,
hasParent,
getParent,
onPatch,
addMiddleware,
IMiddlewareEvent,
getPath,
applySnapshot
} from "mobx-state-tree"
/**
* Creates a tiny proxy around a MST tree that conforms to the redux store api.
* This makes it possible to use MST inside a redux application.
*
* See the [redux-todomvc example](https://github.com/mobxjs/mobx-state-tree/blob/e9e804c8c43e1edde4aabbd52675544e2b3a905b/examples/redux-todomvc/src/index.js#L20) for more details.
*
* @export
* @param {*} model
* @param {...MiddleWare[]} middlewares
* @returns {IReduxStore}
*/
export const asReduxStore = function (model: IAnyStateTreeNode, ...middlewares: any[]) {
if (!isStateTreeNode(model)) throw new Error("Expected model object")
let store = {
getState: () => getSnapshot(model),
dispatch: (action: any) => {
runMiddleWare(action, runners.slice(), (newAction: any) =>
applyAction(model, reduxActionToAction(newAction))
)
},
subscribe: (listener: any) => onSnapshot(model, listener)
}
let runners = middlewares.map((mw) => mw(store))
return store
}
function reduxActionToAction(action: any) {
const actionArgs = Object.assign({}, action)
delete actionArgs.type
return {
name: action.type,
args: [actionArgs]
}
}
function runMiddleWare(action: any, runners: any[], next: any) {
function n(retVal: any) {
const f = runners.shift()
if (f) f(n)(retVal)
else next(retVal)
}
n(action)
}
// devtools
type ChangesMadeSetter = () => void
interface ActionContext {
parent?: ActionContext
name: string
targetTypePath: string
id: number
runningAsync: boolean
errored: boolean
errorReported: boolean
step: number | undefined
callArgs: any[]
changesMadeSetter: ChangesMadeSetter | undefined
}
function getActionContextNameAndTypePath(actionContext: ActionContext, logArgsNearName: boolean) {
let name = actionContext.name
let targetTypePath = actionContext.targetTypePath
if (logArgsNearName) {
let args = actionContext.callArgs.map((a) => JSON.stringify(a)).join(", ")
if (args.length > 64) {
args = args.slice(0, 64) + "..."
}
name += `(${args})`
}
if (actionContext.runningAsync) {
name += ` (${actionContext.step !== undefined ? actionContext.step : "?"})`
}
if (actionContext.errored) {
name += ` -error thrown-`
}
if (actionContext.parent) {
const ret = getActionContextNameAndTypePath(actionContext.parent, logArgsNearName)
if (ret) {
name = `${ret.name} >>> ${name}`
targetTypePath = `${ret.targetTypePath} >>> ${targetTypePath}`
}
}
return {
name,
targetTypePath
}
}
function getTypeName(node: IAnyStateTreeNode) {
return getType(node).name || "(UnnamedType)"
}
function getTargetTypePath(node: IAnyStateTreeNode): string[] {
let current: IAnyStateTreeNode | undefined = node
const names = []
while (current) {
names.unshift(getTypeName(current))
current = hasParent(current) ? getParent(current) : undefined
}
return names
}
/**
* Connects a MST tree to the Redux devtools.
* See this [example](https://github.com/mobxjs/mobx-state-tree/blob/e9e804c8c43e1edde4aabbd52675544e2b3a905b/examples/redux-todomvc/src/index.js#L21)
* for a setup example.
*
* @export
* @param {*} remoteDevDep
* @param {IAnyStateTreeNode} model
* @param {{
* logIdempotentActionSteps?: boolean
* logChildActions?: boolean
* logArgsNearName?: boolean
* }} [options]
*/
export function connectReduxDevtools(
remoteDevDep: any,
model: IAnyStateTreeNode,
options?: {
logIdempotentActionSteps?: boolean
logChildActions?: boolean
logArgsNearName?: boolean
}
) {
const opts = {
logIdempotentActionSteps: true,
logChildActions: false,
logArgsNearName: true,
...options
}
let handlingMonitorAction = 0
// Connect to the monitor
const remotedev = remoteDevDep.connectViaExtension({
name: getType(model).name
})
// Subscribe to change state (if need more than just logging)
remotedev.subscribe((message: any) => {
if (message.type === "DISPATCH") {
handleMonitorActions(remotedev, model, message)
}
})
const initialState = getSnapshot(model)
remotedev.init(initialState)
const actionContexts = new Map<number, ActionContext>()
let changesMadeSetter: ChangesMadeSetter | undefined = undefined
if (!opts.logIdempotentActionSteps) {
onPatch(model, () => {
if (!handlingMonitorAction && changesMadeSetter) {
changesMadeSetter()
}
})
}
addMiddleware(model, actionMiddleware, false)
function actionMiddleware(call: IMiddlewareEvent, next: any) {
if (handlingMonitorAction) {
next(call)
return
}
let context!: ActionContext
// find the context of the parent action (if any)
for (let i = call.allParentIds.length - 1; i >= 0; i--) {
const parentId = call.allParentIds[i]
const foundContext = actionContexts.get(parentId)
if (foundContext) {
context = foundContext
break
}
}
// if it is an action we need to create a new action context
// and also if there's no context (e.g. the middleware was connected in the middle of an action with a flow)
if (call.type === "action" || !context) {
const targetTypePath = getTargetTypePath(call.context).join("/")
const parentContext = context
const path = call.context ? `root${getPath(call.context)}` : "*unknown*"
context = {
// use a space rather than a dot so that the redux devtools move the actions to the next line if there's not enough space
name: `[${path}] ${call.name || "*unknownAction*"}`,
targetTypePath: targetTypePath,
id: call.id,
runningAsync: false,
errored: false,
errorReported: false,
step: call.type === "action" ? 0 : undefined,
callArgs: [],
changesMadeSetter: undefined
}
if (call.type === "action") {
if (call.args) {
context.callArgs = [...call.args]
}
// subaction, assign the parent action context
if (call.parentId) {
context.parent = parentContext
}
actionContexts.set(call.id, context)
}
}
let changesMade = false
context.changesMadeSetter = () => {
changesMade = true
}
let oldChangesMadeSetter = changesMadeSetter
changesMadeSetter = context.changesMadeSetter
// capture any errors and rethrow them later (after it is logged)
let errorThrown
try {
next(call)
} catch (e) {
errorThrown = e
context.errored = true
}
changesMadeSetter = oldChangesMadeSetter
context.changesMadeSetter = undefined
const changedTheModel = opts.logIdempotentActionSteps ? true : changesMade
switch (call.type) {
case "flow_spawn":
case "flow_resume":
case "flow_resume_error": // not errored since the promise error might be caught
// when this events come we can be sure that this action is being run async, as well as its parent actions
context.runningAsync = true
let parent = context.parent
while (parent) {
parent.runningAsync = true
parent = parent.parent
}
break
case "flow_throw":
context.errored = true
break
}
// only log if:
// - it is a sync (never run async code) action
// - a flow_resume
// - a flow_throw that wasn't reported as an error before
// we don't include other kinds since flow_spawn never contain state changes and flow_resume_error might be caught by and handled the parent
const syncAction = call.type === "action" && !context.runningAsync
let log =
syncAction ||
call.type === "flow_resume" ||
(call.type === "flow_throw" && !context.errorReported)
// do not log child actions if asked not to, but only for sync actions
if (!opts.logChildActions && context.parent && !context.runningAsync) {
log = false
// give the child action changes to the parent action
if (changesMade && context.parent.changesMadeSetter) {
context.parent.changesMadeSetter()
}
}
if (log) {
const logStep = (logContext: ActionContext) => {
const sn = getSnapshot(model)
const names = getActionContextNameAndTypePath(logContext, opts.logArgsNearName!)
const copy = {
type: names.name,
targetTypePath: names.targetTypePath,
args: logContext.callArgs
}
remotedev.send(copy, sn)
// we do it over the original context, not the log context, since the original context might throw but the original context might not
if (context.errored) {
context.errorReported = true
}
// increase the step for logging purposes, as well as any parent steps (since child steps count as a parent step)
if (context.step !== undefined) {
context.step++
}
let parent = context.parent
while (parent) {
if (parent.step !== undefined) {
parent.step++
}
parent = parent.parent
}
}
// if it is an async subaction we need to log it since it made a change, but we will log it as if it were the root
const logAsRoot = context.parent && !opts.logChildActions
if (changedTheModel) {
let logContext = context
if (logAsRoot) {
while (logContext.parent) {
logContext = logContext.parent
}
}
logStep(logContext)
} else if (!logAsRoot && context.errored && !context.errorReported) {
logStep(context)
}
}
// once the action is totally finished remove it from the context list to avoid mem leaks
if (call.type === "flow_return" || call.type === "flow_throw" || !context!.runningAsync) {
actionContexts.delete(context!.id!)
}
// rethrow previously captured excepton if needed
if (errorThrown) {
throw errorThrown
}
}
function handleMonitorActions(remotedev2: any, model2: any, message: any) {
try {
handlingMonitorAction++
switch (message.payload.type) {
case "RESET":
applySnapshot(model2, initialState)
return remotedev2.init(initialState)
case "COMMIT":
return remotedev2.init(getSnapshot(model2))
case "ROLLBACK":
return remotedev2.init(remoteDevDep.extractState(message))
case "JUMP_TO_STATE":
case "JUMP_TO_ACTION":
applySnapshot(model2, remoteDevDep.extractState(message))
return
case "IMPORT_STATE":
const nextLiftedState = message.payload.nextLiftedState
const computedStates = nextLiftedState.computedStates
applySnapshot(model2, computedStates[computedStates.length - 1].state)
remotedev2.send(null, nextLiftedState)
return
default:
}
} finally {
handlingMonitorAction--
}
}
}