Skip to content

Commit 224d67d

Browse files
committed
fix #520 let watchers handle callbacks that trigger removeCb()
1 parent 180bd9f commit 224d67d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/watcher.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ p.run = function () {
151151
var cbs = this.cbs
152152
for (var i = 0, l = cbs.length; i < l; i++) {
153153
cbs[i](value, oldValue)
154+
// if a callback also removed other callbacks,
155+
// we need to adjust the loop accordingly.
156+
var removed = l - cbs.length
157+
if (removed) {
158+
i -= removed
159+
l -= removed
160+
}
154161
}
155162
}
156163
}

test/unit/specs/watcher_spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,19 @@ describe('Watcher', function () {
359359
config.async = true
360360
})
361361

362+
it('handle a cb that triggers removeCb', function () {
363+
var watcher = new Watcher(vm, 'a', spy)
364+
watcher.addCb(function () {
365+
watcher.removeCb(spy)
366+
})
367+
watcher.addCb(function () {})
368+
config.async = false
369+
expect(function () {
370+
vm.a = 2
371+
}).not.toThrow()
372+
config.async = true
373+
expect(spy).toHaveBeenCalled()
374+
expect(watcher.cbs.length).toBe(2)
375+
})
376+
362377
})

0 commit comments

Comments
 (0)