-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathasync.ts
336 lines (317 loc) · 9.43 KB
/
async.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
import {
unprotect,
types,
addMiddleware,
recordActions,
process
// TODO: export IRawActionCall
} from "../src"
import { test, CallbackTestContext, Context } from "ava"
import { reaction } from "mobx"
function delay<T>(time: number, value: T, shouldThrow = false): Promise<T> {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (shouldThrow) reject(value)
else resolve(value)
}, time)
})
}
function testCoffeeTodo(
t: CallbackTestContext & Context<any>,
generator: (self: any) => (x: string) => IterableIterator<any>,
shouldError: boolean,
resultValue: any,
producedCoffees: string[],
expectedEvents: any[]
) {
const Todo = types
.model({
title: "get coffee"
})
.actions(self => ({
startFetch: process(generator(self))
}))
const events: any[] = []
const coffees: string[] = []
const t1 = Todo.create({})
unprotect(t1)
addMiddleware(t1, (c, next) => {
events.push(c)
return next(c)
})
reaction(() => t1.title, coffee => coffees.push(coffee))
function handleResult(res) {
t.is(res, resultValue)
t.deepEqual(coffees, producedCoffees)
const filtered = filterRelevantStuff(events)
t.deepEqual(
filtered,
expectedEvents,
"Wrong events, expected\n" + JSON.stringify(filtered, null, 2)
)
t.end()
}
t1.startFetch("black").then(
r => {
t.is(shouldError, false, "Ended up in OK handler")
handleResult(r)
},
r => {
t.is(shouldError, true, "Ended up in ERROR handler")
console.error(r)
handleResult(r)
}
)
}
test.cb("can handle async actions", t => {
testCoffeeTodo(
t,
self =>
function* fetchData(this: any, kind: string) {
self.title = "getting coffee " + kind
self.title = yield delay(100, "drinking coffee")
return "awake"
},
false,
"awake",
["getting coffee black", "drinking coffee"],
[
{ args: ["black"], id: 1, rootId: 1, type: "action", name: "startFetch" },
{ args: ["black"], id: 2, rootId: 1, type: "process_spawn", name: "fetchData" },
{
args: ["drinking coffee"],
id: 2,
rootId: 1,
type: "process_yield",
name: "fetchData"
},
{ args: ["awake"], id: 2, rootId: 1, type: "process_return", name: "fetchData" }
]
)
})
test.cb("can handle erroring actions", t => {
testCoffeeTodo(
t,
self =>
function* fetchData(this: any, kind: string) {
throw kind
},
true,
"black",
[],
[
{ type: "action", name: "startFetch", id: 3, args: ["black"], rootId: 3 },
{ name: "fetchData", type: "process_spawn", id: 4, args: ["black"], rootId: 3 },
{ name: "fetchData", type: "process_throw", id: 4, args: ["black"], rootId: 3 }
]
)
})
test.cb("can handle try catch", t => {
testCoffeeTodo(
t,
self =>
function* fetchData(this: any, kind: string) {
try {
yield delay(10, "tea", true)
} catch (e) {
self.title = e
return "biscuit"
}
},
false,
"biscuit",
["tea"],
[
{ type: "action", name: "startFetch", id: 5, args: ["black"], rootId: 5 },
{ name: "fetchData", type: "process_spawn", id: 6, args: ["black"], rootId: 5 },
{ name: "fetchData", type: "process_yield_error", id: 6, args: ["tea"], rootId: 5 },
{ name: "fetchData", type: "process_return", id: 6, args: ["biscuit"], rootId: 5 }
]
)
})
test.cb("empty sequence works", t => {
testCoffeeTodo(
t,
self => function* fetchData(this: any, kind: string) {},
false,
undefined,
[],
[
{ type: "action", name: "startFetch", id: 7, args: ["black"], rootId: 7 },
{ name: "fetchData", type: "process_spawn", id: 8, args: ["black"], rootId: 7 },
{ name: "fetchData", type: "process_return", id: 8, args: [undefined], rootId: 7 }
]
)
})
test.cb("can handle throw from yielded promise works", t => {
testCoffeeTodo(
t,
self =>
function* fetchData(this: any, kind: string) {
yield delay(10, "x", true)
},
true,
"x",
[],
[
{ type: "action", name: "startFetch", id: 9, args: ["black"], rootId: 9 },
{ name: "fetchData", type: "process_spawn", id: 10, args: ["black"], rootId: 9 },
{ name: "fetchData", type: "process_yield_error", id: 10, args: ["x"], rootId: 9 },
{ name: "fetchData", type: "process_throw", id: 10, args: ["x"], rootId: 9 }
]
)
})
test.cb("typings", t => {
const M = types
.model({
title: types.string
})
.actions(self => {
function* a(x: string) {
yield delay(10, "x", false)
self.title = "7"
return 23
}
const b = process(function* b(x: string) {
yield delay(10, "x", false)
self.title = "7"
return 24
})
return { a: process(a), b }
})
const m1 = M.create({ title: "test " })
const resA = m1.a("z") // Arg typings are correct. TODO: Result type is incorrect; any
const resB = m1.b("z") // Arg typings are correct, TODO: Result is correctly promise, but incorrect generic arg
Promise.all([resA, resB]).then(([x1, x2]) => {
t.is(x1, 23)
t.is(x2, 24)
t.end()
})
})
test.cb("typings", t => {
const M = types
.model({
title: types.string
})
.actions(self => {
function* a(x: string) {
yield delay(10, "x", false)
self.title = "7"
return 23
}
const b = process(function* b(x: string) {
yield delay(10, "x", false)
self.title = "7"
return 24
})
return {
a: process(a),
b
}
})
const m1 = M.create({ title: "test " })
const resA = m1.a("z") // Arg typings are correct. TODO: Result type is incorrect; any
const resB = m1.b("z") // Arg typings are correct, TODO: Result is correctly promise, but incorrect generic arg
Promise.all([resA, resB]).then(([x1, x2]) => {
t.is(x1, 23)
t.is(x2, 24)
t.end()
})
})
test.cb("recordActions should only emit invocation", t => {
let calls = 0
const M = types
.model({
title: types.string
})
.actions(self => {
function* a(x: string) {
yield delay(10, "x", false)
calls++
return 23
}
return {
a: process(a)
}
})
const m1 = M.create({ title: "test " })
const recorder = recordActions(m1)
m1.a("x").then(() => {
recorder.stop()
t.deepEqual(recorder.actions, [
{
args: ["x"],
name: "a",
path: ""
}
])
t.is(calls, 1)
recorder.replay(m1)
setTimeout(() => {
t.is(calls, 2)
t.end()
}, 50)
})
})
test.cb("can handle nested async actions", t => {
const uppercase = process(function* uppercase(value) {
const res = yield delay(20, value.toUpperCase())
return res
})
testCoffeeTodo(
t,
self =>
function* fetchData(this: any, kind: string) {
self.title = yield uppercase("drinking " + kind)
return self.title
},
false,
"DRINKING BLACK",
["DRINKING BLACK"],
[
{ type: "action", name: "startFetch", id: 21, args: ["black"], rootId: 21 },
{ name: "fetchData", type: "process_spawn", id: 22, args: ["black"], rootId: 21 },
{
name: "uppercase",
type: "process_spawn",
id: 23,
args: ["drinking black"],
rootId: 21
},
{
name: "uppercase",
type: "process_yield",
id: 23,
args: ["DRINKING BLACK"],
rootId: 21
},
{
name: "uppercase",
type: "process_return",
id: 23,
args: ["DRINKING BLACK"],
rootId: 21
},
{
name: "fetchData",
type: "process_yield",
id: 22,
args: ["DRINKING BLACK"],
rootId: 21
},
{
name: "fetchData",
type: "process_return",
id: 22,
args: ["DRINKING BLACK"],
rootId: 21
}
]
)
})
function filterRelevantStuff(stuff: any): any {
return stuff.map(x => {
delete x.context
return x
})
}