Skip to content

Commit 5c73a37

Browse files
committed
async batch update - first pass
1 parent f4861ca commit 5c73a37

File tree

6 files changed

+62
-30
lines changed

6 files changed

+62
-30
lines changed

component.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"src/deps-parser.js",
2121
"src/filters.js",
2222
"src/transition.js",
23+
"src/batch.js",
2324
"src/directives/index.js",
2425
"src/directives/if.js",
2526
"src/directives/repeat.js",

src/batch.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var utils = require('./utils'),
2+
queue, has, waiting
3+
4+
reset()
5+
6+
exports.queue = function (binding, method) {
7+
if (!has[binding.id]) {
8+
queue.push({
9+
binding: binding,
10+
method: method
11+
})
12+
has[binding.id] = true
13+
if (!waiting) {
14+
waiting = true
15+
setTimeout(flush, 0)
16+
}
17+
}
18+
}
19+
20+
function flush () {
21+
for (var i = 0; i < queue.length; i++) {
22+
var task = queue[i]
23+
task.binding['_' + task.method]()
24+
has[task.binding.id] = false
25+
}
26+
reset()
27+
}
28+
29+
function reset () {
30+
queue = []
31+
has = utils.hash()
32+
waiting = false
33+
}

src/binding.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
var batch = require('./batch'),
2+
id = 0
3+
14
/**
25
* Binding class.
36
*
@@ -6,6 +9,7 @@
69
* and multiple computed property dependents
710
*/
811
function Binding (compiler, key, isExp, isFn) {
12+
this.id = id++
913
this.value = undefined
1014
this.isExp = !!isExp
1115
this.isFn = isFn
@@ -24,9 +28,13 @@ var BindingProto = Binding.prototype
2428
*/
2529
BindingProto.update = function (value) {
2630
this.value = value
31+
batch.queue(this, 'update')
32+
}
33+
34+
BindingProto._update = function () {
2735
var i = this.instances.length
2836
while (i--) {
29-
this.instances[i].update(value)
37+
this.instances[i].update(this.value)
3038
}
3139
this.pub()
3240
}
@@ -36,6 +44,10 @@ BindingProto.update = function (value) {
3644
* Force all instances to re-evaluate themselves
3745
*/
3846
BindingProto.refresh = function () {
47+
batch.queue(this, 'refresh')
48+
}
49+
50+
BindingProto._refresh = function () {
3951
var i = this.instances.length
4052
while (i--) {
4153
this.instances[i].refresh()

src/utils.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ var config = require('./config'),
55
console = window.console,
66
ViewModel // late def
77

8-
var defer =
9-
window.webkitRequestAnimationFrame ||
10-
window.requestAnimationFrame ||
11-
window.setTimeout
12-
138
/**
149
* Create a prototype-less object
1510
* which is a better hash/map
@@ -185,12 +180,5 @@ var utils = module.exports = {
185180
if (!config.silent && console) {
186181
console.warn(join.call(arguments, ' '))
187182
}
188-
},
189-
190-
/**
191-
* Defer DOM updates
192-
*/
193-
nextTick: function (cb) {
194-
defer(cb, 0)
195183
}
196184
}

src/viewmodel.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
var Compiler = require('./compiler'),
22
utils = require('./utils'),
33
transition = require('./transition'),
4-
def = utils.defProtected,
5-
nextTick = utils.nextTick
4+
def = utils.defProtected
65

76
/**
87
* ViewModel exposed to the user that holds data,
@@ -104,7 +103,7 @@ def(VMProto, '$appendTo', function (target, cb) {
104103
var el = this.$el
105104
transition(el, 1, function () {
106105
target.appendChild(el)
107-
if (cb) nextTick(cb)
106+
if (cb) setTimeout(cb, 0)
108107
}, this.$compiler)
109108
})
110109

@@ -114,7 +113,7 @@ def(VMProto, '$remove', function (cb) {
114113
if (!parent) return
115114
transition(el, -1, function () {
116115
parent.removeChild(el)
117-
if (cb) nextTick(cb)
116+
if (cb) setTimeout(cb, 0)
118117
}, this.$compiler)
119118
})
120119

@@ -125,7 +124,7 @@ def(VMProto, '$before', function (target, cb) {
125124
if (!parent) return
126125
transition(el, 1, function () {
127126
parent.insertBefore(el, target)
128-
if (cb) nextTick(cb)
127+
if (cb) setTimeout(cb, 0)
129128
}, this.$compiler)
130129
})
131130

@@ -141,7 +140,7 @@ def(VMProto, '$after', function (target, cb) {
141140
} else {
142141
parent.appendChild(el)
143142
}
144-
if (cb) nextTick(cb)
143+
if (cb) setTimeout(cb, 0)
145144
}, this.$compiler)
146145
})
147146

test/unit/specs/viewmodel.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ describe('UNIT: ViewModel', function () {
217217

218218
var enterCalled,
219219
leaveCalled,
220-
callbackCalled,
221-
nextTick = require('vue/src/utils').nextTick
220+
callbackCalled
222221

223222
var v = new Vue({
224223
attributes: {
@@ -254,10 +253,10 @@ describe('UNIT: ViewModel', function () {
254253
v.$appendTo(parent, cb)
255254
assert.strictEqual(v.$el.parentNode, parent)
256255
assert.ok(enterCalled)
257-
nextTick(function () {
256+
setTimeout(function () {
258257
assert.ok(callbackCalled)
259258
done()
260-
})
259+
}, 0)
261260
})
262261

263262
it('$before', function (done) {
@@ -269,10 +268,10 @@ describe('UNIT: ViewModel', function () {
269268
assert.strictEqual(v.$el.parentNode, parent)
270269
assert.strictEqual(v.$el.nextSibling, ref)
271270
assert.ok(enterCalled)
272-
nextTick(function () {
271+
setTimeout(function () {
273272
assert.ok(callbackCalled)
274273
done()
275-
})
274+
}, 0)
276275
})
277276

278277
it('$after', function (done) {
@@ -287,10 +286,10 @@ describe('UNIT: ViewModel', function () {
287286
assert.strictEqual(v.$el.nextSibling, ref2)
288287
assert.strictEqual(ref1.nextSibling, v.$el)
289288
assert.ok(enterCalled)
290-
nextTick(function () {
289+
setTimeout(function () {
291290
assert.ok(callbackCalled)
292291
next()
293-
})
292+
}, 0)
294293

295294
function next () {
296295
reset()
@@ -299,10 +298,10 @@ describe('UNIT: ViewModel', function () {
299298
assert.notOk(v.$el.nextSibling)
300299
assert.strictEqual(ref2.nextSibling, v.$el)
301300
assert.ok(enterCalled)
302-
nextTick(function () {
301+
setTimeout(function () {
303302
assert.ok(callbackCalled)
304303
done()
305-
})
304+
}, 0)
306305
}
307306
})
308307

@@ -314,7 +313,7 @@ describe('UNIT: ViewModel', function () {
314313
assert.notOk(v.$el.parentNode)
315314
assert.ok(enterCalled)
316315
assert.ok(leaveCalled)
317-
nextTick(function () {
316+
setTimeout(function () {
318317
assert.ok(callbackCalled)
319318
done()
320319
})

0 commit comments

Comments
 (0)