-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatcher.js
109 lines (85 loc) · 2.53 KB
/
batcher.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
describe('Batcher', function () {
var Batcher = require('vue/src/batcher'),
batcher = new Batcher(),
nextTick = require('vue/src/utils').nextTick
var updateCount = 0
function mockJob (id, middleware) {
return {
id: id,
execute: function () {
updateCount++
this.updated = true
if (middleware) middleware()
}
}
}
it('should push bindings to be updated on nextTick', function (done) {
updateCount = 0
var b1 = mockJob(1),
b2 = mockJob(2)
batcher.push(b1)
batcher.push(b2)
assert.strictEqual(updateCount, 0)
assert.notOk(b1.updated)
assert.notOk(b2.updated)
nextTick(function () {
assert.strictEqual(updateCount, 2)
assert.ok(b1.updated)
assert.ok(b2.updated)
done()
})
})
it('should not push dupicate bindings', function (done) {
updateCount = 0
var b1 = mockJob(1),
b2 = mockJob(1)
batcher.push(b1)
batcher.push(b2)
nextTick(function () {
assert.strictEqual(updateCount, 1)
assert.ok(b1.updated)
assert.notOk(b2.updated)
done()
})
})
it('should push dependency bidnings triggered during flush', function (done) {
updateCount = 0
var b1 = mockJob(1),
b2 = mockJob(2, function () {
batcher.push(b1)
})
batcher.push(b2)
nextTick(function () {
assert.strictEqual(updateCount, 2)
assert.ok(b1.updated)
assert.ok(b2.updated)
done()
})
})
it('should allow overriding jobs with same ID', function (done) {
updateCount = 0
var b1 = mockJob(1),
b2 = mockJob(1)
b2.override = true
batcher.push(b1)
batcher.push(b2)
nextTick(function () {
assert.strictEqual(updateCount, 1)
assert.ok(b1.cancelled)
assert.notOk(b1.updated)
assert.ok(b2.updated)
done()
})
})
it('should execute the _preFlush hook', function (done) {
var executed = false
batcher._preFlush = function () {
executed = true
}
batcher.push(mockJob(1))
nextTick(function () {
assert.ok(executed)
done()
})
})
})