1 | const {compile, root, setter, bind, arg0} = require('../../index');
|
2 | const {evalOrLoad, funcLibrary} = require('../test-utils');
|
3 |
|
4 | describe('batching', () => {
|
5 | describe('re-entrant runInBatch', () => {
|
6 | it('should work', () => {
|
7 | const modelFactory = evalOrLoad(compile({
|
8 | sum: root.get('items').sum().call('updateTotal', bind('setTotal')),
|
9 | total: root.get('total').call('tap'),
|
10 | setTotal: setter('total'),
|
11 | updateItems: setter('items', arg0)
|
12 | }, {compiler: 'optimizing'}))
|
13 | function updateTotal(newTotal, setTotal) {
|
14 | this.$runInBatch(() => {
|
15 | setTotal(newTotal)
|
16 | })
|
17 | }
|
18 | const inst = modelFactory({items: [1, 2, 3]}, {...funcLibrary, updateTotal});
|
19 | expect(inst.total).toEqual(6);
|
20 | expect(inst.$model.total).toEqual(6);
|
21 | inst.$runInBatch(() => {
|
22 | inst.updateItems(3, 4);
|
23 | })
|
24 | })
|
25 | })
|
26 | describe('empty batching strategy', () => {
|
27 | const emptyBatchingStrategy = function () {
|
28 | this.$endBatch();
|
29 | };
|
30 |
|
31 | it('should behave as if there is no batching at all', () => {
|
32 | const carmiModelFactory = evalOrLoad(
|
33 | compile(
|
34 | {
|
35 | counter: root.get('model').get('counter'),
|
36 | setCounter: setter('model', 'counter'),
|
37 | incrementCounter: bind('incrementCounter', bind('setCounter'), root.get('model').get('counter'))
|
38 | },
|
39 | true
|
40 | )
|
41 | );
|
42 |
|
43 | const carmiInstance = carmiModelFactory(
|
44 | {model: {counter: 1}},
|
45 | {
|
46 | incrementCounter: (setCounter, originalCounter) => {
|
47 | setCounter(originalCounter + 1);
|
48 | }
|
49 | },
|
50 | emptyBatchingStrategy
|
51 | );
|
52 |
|
53 | carmiInstance.incrementCounter();
|
54 | carmiInstance.incrementCounter();
|
55 |
|
56 | expect(carmiInstance.counter).toBe(3);
|
57 | });
|
58 | });
|
59 | });
|