Skip to content

Commit 3200487

Browse files
committed
tweak batcher
1 parent 720ac15 commit 3200487

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/batcher.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function reset () {
2727
}
2828

2929
/**
30-
* Flush both queues and run the jobs.
30+
* Flush both queues and run the watchers.
3131
*/
3232

3333
function flush () {
@@ -38,53 +38,58 @@ function flush () {
3838
}
3939

4040
/**
41-
* Run the jobs in a single queue.
41+
* Run the watchers in a single queue.
4242
*
4343
* @param {Array} queue
4444
*/
4545

4646
function run (queue) {
47-
// do not cache length because more jobs might be pushed
48-
// as we run existing jobs
47+
// do not cache length because more watchers might be pushed
48+
// as we run existing watchers
4949
for (var i = 0; i < queue.length; i++) {
50-
var job = queue[i]
51-
var id = job.id
50+
var watcher = queue[i]
51+
var id = watcher.id
5252
has[id] = null
53-
job.run()
53+
watcher.run()
54+
// in dev build, check and stop circular updates.
5455
if (process.env.NODE_ENV !== 'production' && has[id] != null) {
5556
circular[id] = (circular[id] || 0) + 1
5657
if (circular[id] > config._maxUpdateCount) {
5758
queue.splice(has[id], 1)
5859
_.warn(
5960
'You may have an infinite update loop for watcher ' +
60-
'with expression: ' + job.expression
61+
'with expression: ' + watcher.expression
6162
)
6263
}
6364
}
6465
}
6566
}
6667

6768
/**
68-
* Push a job into the job queue.
69+
* Push a watcher into the watcher queue.
6970
* Jobs with duplicate IDs will be skipped unless it's
7071
* pushed when the queue is being flushed.
7172
*
72-
* @param {Object} job
73+
* @param {Watcher} watcher
7374
* properties:
74-
* - {String|Number} id
75-
* - {Function} run
75+
* - {Number} id
76+
* - {Function} run
7677
*/
7778

78-
exports.push = function (job) {
79-
var id = job.id
79+
exports.push = function (watcher) {
80+
var id = watcher.id
8081
if (has[id] == null) {
81-
if (internalQueueDepleted && !job.user) {
82-
job.run()
82+
// if an internal watcher is pushed, but the internal
83+
// queue is already depleted, we run it immediately.
84+
if (internalQueueDepleted && !watcher.user) {
85+
watcher.run()
8386
return
8487
}
85-
var q = job.user ? userQueue : queue
88+
// push watcher into appropriate queue
89+
var q = watcher.user ? userQueue : queue
8690
has[id] = q.length
87-
q.push(job)
91+
q.push(watcher)
92+
// queue the flush
8893
if (!waiting) {
8994
waiting = true
9095
_.nextTick(flush)

src/watcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function Watcher (vm, expOrFn, cb, options) {
2727
var isFn = typeof expOrFn === 'function'
2828
this.vm = vm
2929
vm._watchers.push(this)
30-
this.expression = isFn ? '' : expOrFn
30+
this.expression = isFn ? expOrFn.toString() : expOrFn
3131
this.cb = cb
3232
this.id = ++uid // uid for batching
3333
this.active = true

0 commit comments

Comments
 (0)