-
Notifications
You must be signed in to change notification settings - Fork 2
/
batch.spec.ts
635 lines (541 loc) · 18.5 KB
/
batch.spec.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import { autorun, batch, define, observable, reaction } from "@formily/reactive";
// batch 批量操作普通用法
describe("normal batch", () => {
// 不使用 batch 每次修改 observable 都会响应一次
test("no batch", () => {
const obs = observable({ aa: { bb: 123 } });
const handler = jest.fn();
autorun(() => handler(obs.aa.bb));
expect(handler).toHaveBeenCalledTimes(1);
obs.aa.bb = 111;
obs.aa.bb = 222;
expect(handler).toHaveBeenCalledTimes(3);
obs.aa.bb = 333;
obs.aa.bb = 444;
expect(handler).toHaveBeenCalledTimes(5);
});
// batch 内部所有修改只记录一次响应
test("batch", () => {
const obs = observable({ aa: { bb: 123 } });
const handler = jest.fn();
autorun(() => handler(obs.aa.bb));
expect(handler).toHaveBeenCalledTimes(1);
obs.aa.bb = 111;
obs.aa.bb = 222;
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenLastCalledWith(222);
batch(() => {
obs.aa.bb = 333;
obs.aa.bb = 444;
});
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenLastCalledWith(444);
});
// 在 track 函数中使用 batch
test("batch track", () => {
const obs = observable({
aa: { bb: { cc: 123 } },
cc: 1,
});
// 这里会和 action 不同,batch 会收集依赖,每次都响应依赖对象更新
const handler = jest.fn();
autorun(() => batch(() => {
handler(obs.aa.bb);
obs.cc += 20;
}));
expect(handler).toHaveBeenCalledTimes(1);
expect(obs.cc).toEqual(21);
// 更新数据会继续响应
obs.aa.bb = { cc: 321 };
expect(handler).toHaveBeenCalledTimes(2);
expect(obs.cc).toEqual(41);
});
// batch.bound 绑定一个批量操作
test("batch.bound", () => {
const obs = observable({ aa: { bb: 123 } });
const setData = batch.bound!(() => {
obs.aa.bb = 333;
obs.aa.bb = 444;
});
const handler = jest.fn();
autorun(() => handler(obs.aa.bb));
expect(handler).toHaveBeenCalledTimes(1);
expect(obs.aa.bb).toBe(123);
obs.aa.bb = 111;
obs.aa.bb = 222;
expect(handler).toHaveBeenCalledTimes(3);
expect(obs.aa.bb).toBe(222);
// 批量操作增加 1 次
setData();
batch(() => {});
expect(handler).toHaveBeenCalledTimes(4);
expect(obs.aa.bb).toBe(444);
});
// 在 track 函数中使用 batch.bound
test("batch.bound track", () => {
const obs = observable({
aa: { bb: 123 },
cc: 1
});
const handler = jest.fn();
autorun(() => {
const action = batch.bound!(() => {
handler(obs.aa.bb);
obs.cc += 20;
});
action();
});
expect(handler).toHaveBeenCalledTimes(1);
expect(obs.cc).toBe(21);
obs.aa.bb = 321;
expect(handler).toHaveBeenCalledTimes(2);
expect(obs.cc).toBe(41);
});
// batch.scope 在 batch 中分批执行
test("batch.scope", () => {
const obs = observable<Partial<Record<string, number|string>>>({});
const handler = jest.fn();
// 初始化执行 1 次
autorun(() => handler(obs.aa, obs.bb, obs.cc, obs.dd));
expect(handler).toHaveBeenCalledTimes(1);
batch(() => {
// 分批 +1
batch.scope!(() => {
obs.aa = 123;
});
// 分批 +1
batch.scope!(() => {
obs.cc = "ccccc";
});
// 批量操作 +1
obs.bb = 321;
obs.dd = "ddddd";
});
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenNthCalledWith(1, undefined, undefined, undefined, undefined);
expect(handler).toHaveBeenNthCalledWith(2, 123, undefined, undefined, undefined);
expect(handler).toHaveBeenNthCalledWith(3, 123, undefined, "ccccc", undefined);
expect(handler).toHaveBeenNthCalledWith(4, 123, 321, "ccccc", "ddddd");
});
// 使用 batch.socpe.bound
test("batch.scope bound", () => {
const obs = observable<Partial<Record<string, number | string>>>({});
const handler = jest.fn();
autorun(() => handler(obs.aa, obs.bb, obs.cc, obs.dd));
expect(handler).toHaveBeenCalledTimes(1);
const outScope = batch.scope?.bound!((num: number) => {
obs.aa = num;
});
batch(() => {
// 分批操作 +1
outScope && outScope(123);
const innerScope = batch.scope?.bound!((value: string) => {
obs.cc = value;
});
// 分批操作 +1
innerScope && innerScope("ccccc");
// 批量操作 +1,总计 4 次
obs.bb = 321;
obs.dd = "ddddd";
// 再添加 3 次外部分批操作,3 次内部分批操作
if (outScope) {
outScope(111);
outScope(222);
outScope(333);
}
// 实际增加的是 6 - 1,总计 4 + 5 = 9
// 所以建议 scope 放在 batch 顶部
if (innerScope) {
innerScope("qqqqq");
innerScope("rrrrr");
innerScope("sssss");
}
});
expect(handler).toHaveBeenCalledTimes(9);
});
// 在 track 函数中使用 batch.scope
test("batch.scope track", () => {
const obs = observable({
aa: { bb: 123 },
cc: 1
});
const handler = jest.fn();
autorun(() => batch.scope!(() => {
handler(obs.aa.bb);
obs.cc += 20;
}));
// 注意 分批操作是可以在 batch 外部执行的
batch.scope!(() => {});
// 不能执行的只有autorun.memo 和 autorun.effect
expect(() => autorun.memo(() => ({}))).toThrow();
expect(() => autorun.effect(() => {})).toThrow();
// 初始化执行 1 次
expect(handler).toHaveBeenCalledTimes(1);
expect(obs.cc).toBe(21);
// 由于 batch 会收集依赖,所以修改数据会继续响应
obs.aa.bb = 321;
expect(handler).toHaveBeenCalledTimes(2);
expect(obs.cc).toBe(41);
});
// 在 track 函数中使用 batch.scope.bound
test("batch.scope bound track", () => {
const obs = observable({
aa: { bb: 123 },
cc: 1,
});
const handler = jest.fn();
autorun(() => {
const bound = batch.scope?.bound!((num: number) => {
handler(obs.aa.bb);
obs.cc += num;
});
bound && bound(20);
});
// 初始化执行一次
expect(handler).toHaveBeenCalledTimes(1);
expect(obs.cc).toBe(21);
// batch 会收集依赖,再次修改会继续响应
obs.aa.bb = 321;
expect(handler).toHaveBeenCalledTimes(2);
expect(obs.cc).toBe(41);
// 修改 cc 会先赋值,然后再发生响应
// 由于 obs 是通过 observable 深度劫持,所以修改 cc 也会响应
obs.cc = 10;
expect(handler).toHaveBeenCalledTimes(3);
expect(obs.cc).toBe(30);
});
// 在 batch 中抛出错误
test("batch error", () => {
const handler = jest.fn();
try {
batch(() => {
throw "123";
});
} catch(e) {
handler(e);
}
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith("123");
});
});
// define 定义模型中使用 batch 批量操作
describe("annotation batch", () => {
// define 中使用 batch
test("batch", () => {
const obs = define({
aa: { bb: 123 },
setData(data: number[]) {
this.aa.bb = data[0] ?? 0;
this.aa.bb = data[1] ?? 0;
}
}, {
aa: observable,
setData: batch,
});
const handler = jest.fn();
autorun(() => handler(obs.aa.bb));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(123);
obs.aa.bb = 111;
obs.aa.bb = 222;
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenNthCalledWith(2, 111);
expect(handler).toHaveBeenNthCalledWith(3, 222);
// 原本只能通过 batch.bound 实现的功能,通过 define 可以让 batch 实现
obs.setData([333, 444]);
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenNthCalledWith(4, 444);
});
// 在 track 函数中使用模型 batch
test("batch track", () => {
const handler = jest.fn();
const obs = define({
aa: { bb: 123 },
cc: 1,
setData(num: number) {
handler(obs.aa.bb);
obs.cc += num;
}
}, {
aa: observable,
setData: batch
});
autorun(() => obs.setData(20));
expect(handler).toHaveBeenCalledTimes(1);
expect(obs.cc).toBe(21);
// 即便在 track 中使用,batch.bound 仍旧会收集依赖
obs.aa.bb = 321;
expect(handler).toHaveBeenCalledTimes(2);
expect(obs.cc).toBe(41);
});
// define 中使用 batch.bound
test("batch.bound", () => {
const obs = define({
aa: { bb: 123 },
setData(data: number[]) {
this.aa.bb = data[0] ?? 0;
this.aa.bb = data[1] ?? 0;
}
}, {
aa: observable,
setData: batch.bound
});
const handler = jest.fn();
autorun(() => handler(obs.aa.bb));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(123);
obs.aa.bb = 111;
obs.aa.bb = 222;
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenNthCalledWith(2, 111);
expect(handler).toHaveBeenNthCalledWith(3, 222);
obs.setData([333, 444]);
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenNthCalledWith(4, 444);
});
// track 函数中使用模型 batch.bound
test("batch.bound track", () => {
const handler = jest.fn();
const obs = define({
aa: { bb: 123 },
cc: 1,
setData(num: number) {
handler(obs.aa.bb);
obs.cc += num;
}
}, {
aa: observable,
setData: batch.bound
});
autorun(() => obs.setData(20));
expect(handler).toHaveBeenCalledTimes(1);
expect(obs.cc).toBe(21);
obs.aa.bb = 321;
expect(handler).toHaveBeenCalledTimes(2);
expect(obs.cc).toBe(41);
});
// define 中使用 batch.scope
test("batch.scope", () => {
const obs = define<DefineDataType>({
aa: null, bb: null, cc: null, dd: null,
scope1(val) {
this.aa = val;
},
scope2(val) {
this.cc = val;
}
}, {
aa: observable,
bb: observable,
cc: observable,
dd: observable,
scope1: batch.scope,
scope2: batch.scope,
});
const handler = jest.fn();
autorun(() => handler(obs.aa, obs.bb, obs.cc, obs.dd));
expect(handler).toHaveBeenCalledTimes(1);
batch(() => {
obs.scope1 && obs.scope1(123);
obs.scope2 && obs.scope2("ccccc");
obs.bb = 321;
obs.dd = "ddddd";
});
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenNthCalledWith(1, null, null, null, null);
expect(handler).toHaveBeenNthCalledWith(2, 123, null, null, null);
expect(handler).toHaveBeenNthCalledWith(3, 123, null, "ccccc", null);
expect(handler).toHaveBeenNthCalledWith(4, 123, 321, "ccccc", "ddddd");
});
// define 中使用 batch.scope.bound
test("batch.scope bound", () => {
const obs = define<DefineDataType>({
aa: null, bb: null, cc: null, dd: null,
scope1(val) {
this.aa = val;
},
scope2(val) {
this.cc = "ccccc";
}
}, {
aa: observable,
bb: observable,
cc: observable,
dd: observable,
scope1: batch.scope?.bound,
scope2: batch.scope?.bound,
});
const handler = jest.fn();
autorun(() => handler(obs.aa, obs.bb, obs.cc, obs.dd));
expect(handler).toHaveBeenCalledTimes(1);
batch(() => {
obs.scope1 && obs.scope1(123);
obs.scope2 && obs.scope2("ccccc");
obs.bb = 321;
obs.dd = "ddddd";
});
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenNthCalledWith(1, null, null, null, null);
expect(handler).toHaveBeenNthCalledWith(2, 123, null, null, null);
expect(handler).toHaveBeenNthCalledWith(3, 123, null, "ccccc", null);
expect(handler).toHaveBeenNthCalledWith(4, 123, 321, "ccccc", "ddddd");
});
// 在 track 函数中使用模型 batch.scope
test("batch.scope track", () => {
const handler = jest.fn();
const obs = define({
aa: { bb: 123 },
cc: 1,
scope(num: number) {
handler(this.aa.bb);
this.cc += num;
}
}, {
aa: observable,
scope: batch.scope
});
autorun(() => obs.scope!(20));
expect(handler).toHaveBeenCalledTimes(1);
expect(obs.cc).toBe(21);
obs.aa.bb = 321;
expect(handler).toHaveBeenCalledTimes(2);
expect(obs.cc).toBe(41);
// 并没有劫持 cc,所以 scope 不响应 cc 的更新
// 定义模型中只要没有 observable 绑定,就不会作为依赖跟踪
obs.cc = 10;
expect(handler).toHaveBeenCalledTimes(2);
expect(obs.cc).toBe(10);
});
// 在 track 函数中使用 batch.scope.bound
test("batch.scope bound track", () => {
const handler = jest.fn();
const obs = define({
aa: { bb: 123 },
cc: 1,
scope(num: number) {
handler(this.aa.bb);
this.cc += num;
}
}, {
aa: observable,
scope: batch.scope?.bound
});
autorun(() => obs.scope(20));
expect(handler).toHaveBeenCalledTimes(1);
expect(obs.cc).toBe(21);
obs.aa.bb = 321;
expect(handler).toHaveBeenCalledTimes(2);
expect(obs.cc).toBe(41);
// cc 并没有被 observable 劫持
obs.cc = 10;
expect(handler).toHaveBeenCalledTimes(2);
expect(obs.cc).toBe(10);
});
});
// 批量操作结束回调,batch 独有 action 没有
describe("batch endpoint", () => {
// batch.endpoint 注册批量执行结束回调
test("normal endpoint", () => {
const tokens: string[] = [];
const inner = batch.bound!(() => {
batch.endpoint!(() => tokens.push("endpoint"));
tokens.push("inner");
});
const wrapper = batch.bound!(() => {
inner();
tokens.push("wrapper");
});
wrapper();
expect(tokens).toEqual(["inner", "wrapper", "endpoint"]);
});
// batch.endpoint 意外的结束 - 不提供回调也不会执行
test("unexpect endpoint", () => {
const token: string[] = [];
const inner = batch.bound!(() => {
batch.endpoint!();
token.push("inner");
});
const wrapper = batch.bound!(() => {
inner();
token.push("wrapper");
});
wrapper();
expect(token).toEqual(["inner", "wrapper"]);
});
// 直接使用 batch.endpoint
test("no wrapper endpoint", () => {
const tokens: string[] = [];
batch.endpoint!(() => tokens.push("endpoint"));
expect(tokens).toEqual(["endpoint"]);
});
});
// 在 reaction 有效的收集依赖触发 subscrible
test("reaction collect in batch valid", () => {
const handler = jest.fn();
const obs = observable({ aa: 11, bb: 22, cc: 33 });
reaction(
() => obs.aa,
(newValue, oldValue) => {
// 这里并没有任何意义,永远得到 undefined
void obs.cc;
// 为了让 subscrible 有意义,加一个 handler
handler(newValue, oldValue);
}
);
expect(handler).toHaveBeenCalledTimes(0);
const fn = jest.fn();
autorun(() => {
batch.scope!(() => {
obs.aa = obs.bb;
});
fn();
});
expect(fn).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(22, 11);
obs.bb = 44;
expect(fn).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenCalledTimes(2);
expect(obs.aa).toBe(44);
expect(handler).toHaveBeenCalledWith(44, 22);
});
// 在 reaction subscript 中无效的依赖不会反向触发响应
test("reaction collect in batch invalid", () => {
const obs = observable({ aa: 11, bb: 22, cc: 33 });
const fn = jest.fn();
const subscrible = jest.fn();
// 初始化不响应
reaction(
() => obs.aa,
() => {
void obs.cc;
subscrible();
}
);
expect(subscrible).toHaveBeenCalledTimes(0);
autorun(() => {
batch.scope!(() => {
obs.aa = obs.bb;
});
fn();
});
// 初始化随着 autorun 默认执行赋值 aa,触发 subscrible
expect(fn).toHaveBeenCalledTimes(1);
expect(subscrible).toHaveBeenCalledTimes(1);
// 修改 bb 触发连锁反应
obs.bb = 44;
expect(fn).toHaveBeenCalledTimes(2);
expect(subscrible).toHaveBeenCalledTimes(2);
// 但是修改 cc 在 subscrible 中是不会主动收集依赖
obs.cc = 55;
expect(fn).toHaveBeenCalledTimes(3);
expect(subscrible).toHaveBeenCalledTimes(2);
});
type DefineDataType = {
aa: null | number;
bb: null | number;
cc: null | string;
dd: null | string;
scope1: { (val: number): void };
scope2: { (val: string): void };
}