-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
flow.js
319 lines (286 loc) · 7.54 KB
/
flow.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
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
import * as mobx from "../../src/mobx.ts"
const { flow } = mobx
function delay(time, value, shouldThrow = false) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (shouldThrow) reject(value)
else resolve(value)
}, time)
})
}
test("it should support async generator actions", done => {
mobx.configure({ enforceActions: true })
const values = []
const x = mobx.observable({ a: 1 })
mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true })
const f = mobx.flow(function*(initial) {
x.a = initial // this runs in action
x.a = yield delay(100, 3) // and this as well!
yield delay(100, 0)
x.a = 4
return x.a
})
setTimeout(() => {
f(2).then(v => {
// note: ideally, type of v should be inferred..
expect(v).toBe(4)
expect(values).toEqual([1, 2, 3, 4])
done()
})
}, 10)
})
test("it should support try catch in async generator", done => {
mobx.configure({ enforceActions: true })
const values = []
const x = mobx.observable({ a: 1 })
mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true })
const f = mobx.flow(function*(initial) {
x.a = initial // this runs in action
try {
x.a = yield delay(100, 5, true) // and this as well!
yield delay(100, 0)
x.a = 4
} catch (e) {
x.a = e
}
return x.a
})
setTimeout(() => {
f(2).then(v => {
// note: ideally, type of v should be inferred..
expect(v).toBe(5)
expect(values).toEqual(values, [1, 2, 5])
done()
})
}, 10)
})
test("it should support throw from async generator", done => {
mobx.flow(function*() {
throw 7
})().then(
() => {
done.fail("should fail")
},
e => {
expect(e).toBe(7)
done()
}
)
})
test("it should support throw from yielded promise generator", done => {
mobx.flow(function*() {
return yield delay(10, 7, true)
})().then(
() => {
done.fail("should fail")
},
e => {
expect(e).toBe(7)
done()
}
)
})
test("it should support asyncAction as decorator", done => {
const values = []
mobx.configure({ enforceActions: true })
class X {
a = 1;
*f(initial) {
this.a = initial // this runs in action
try {
this.a = yield delay(100, 5, true) // and this as well!
yield delay(100, 0)
this.a = 4
} catch (e) {
this.a = e
}
return this.a
}
}
mobx.decorate(X, {
a: mobx.observable,
f: mobx.flow
})
const x = new X()
mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true })
setTimeout(() => {
x.f(2).then(v => {
expect(v).toBe(5)
expect(values).toEqual([1, 2, 5])
expect(x.a).toBe(5)
done()
})
}, 10)
})
test("it should support logging", done => {
mobx.configure({ enforceActions: true })
const events = []
const x = mobx.observable({ a: 1 })
const f = mobx.flow("myaction", function*(initial) {
x.a = initial
x.a = yield delay(100, 5)
x.a = 4
x.a = yield delay(100, 3)
return x.a
})
const d = mobx.spy(ev => events.push(ev))
setTimeout(() => {
f(2).then(() => {
expect(stripEvents(events)).toMatchSnapshot()
d()
done()
})
}, 10)
})
function stripEvents(events) {
return events.map(e => {
delete e.object
delete e.fn
delete e.time
return e
})
}
test("flows can be cancelled - 1 - uncatched cancellation", done => {
let steps = 0
const start = flow(function*() {
steps = 1
yield Promise.resolve()
steps = 2
})
const promise = start()
promise.then(
() => {
debugger
fail()
},
err => {
debugger
expect(steps).toBe(1)
expect("" + err).toBe("Error: FLOW_CANCELLED")
done()
}
)
debugger
promise.cancel()
})
test("flows can be cancelled - 2 - finally clauses are run", done => {
let steps = 0
let finallyHandled = false
const start = flow(function*() {
steps = 1
try {
yield Promise.resolve()
steps = 2
} finally {
expect(steps).toBe(1)
finallyHandled = true
}
})
const promise = start()
promise.then(
res => {
fail()
},
err => {
expect("" + err).toBe("Error: FLOW_CANCELLED")
expect(finallyHandled).toBeTruthy()
done()
}
)
promise.cancel()
})
test("flows can be cancelled - 3 - throw in finally should be catched", done => {
const counter = mobx.observable({ counter: 0 })
const d = mobx.reaction(() => counter.counter, () => {})
mobx.configure({ enforceActions: true })
const start = flow(function*() {
counter.counter = 1
try {
yield Promise.resolve()
counter.counter = 15
} finally {
counter.counter = 4
throw "OOPS"
}
})
const promise = start()
promise.then(
() => fail("flow should not have failed"),
err => {
expect("" + err).toBe("OOPS")
expect(counter.counter).toBe(4)
mobx.configure({ enforceActions: false })
d()
done()
}
)
promise.cancel()
})
test("flows can be cancelled - 4 - pending Promise will be ignored", done => {
let steps = 0
const start = flow(function*() {
steps = 1
yield Promise.reject("This won't be catched anywhere!") // cancel will resolve this flow before this one is throw, so this promise goes uncatched
steps = 2
})
const promise = start()
promise.then(
() => fail(),
err => {
expect(steps).toBe(1)
expect("" + err).toBe("Error: FLOW_CANCELLED")
done()
}
)
promise.cancel()
})
test("flows can be cancelled - 5 - return before cancel", done => {
let steps = 0
const start = flow(function*() {
steps = 1
return Promise.resolve(2) // cancel will be to late..
})
const promise = start()
promise.then(
value => {
expect(value).toBe(2), done()
},
err => {
fail()
}
)
promise.cancel() // no-op
})
test("flows can be cancelled - 5 - flows cancel recursively", done => {
let flow1cancelled = false
let flow2cancelled = false
let stepsReached = 0
const flow1 = flow(function*() {
try {
yield Promise.resolve()
stepsReached++
} finally {
flow1cancelled = true
}
})
const flow2 = flow(function*() {
try {
yield flow1()
stepsReached++
} finally {
flow2cancelled = true
}
})
const p = flow2()
p.then(
() => fail(),
err => {
expect("" + err).toBe("Error: FLOW_CANCELLED")
expect(stepsReached).toBe(0)
expect(flow2cancelled).toBeTruthy()
expect(flow1cancelled).toBeTruthy()
done()
}
)
p.cancel()
})