@@ -37,8 +37,8 @@ function Watcher (vm, expOrFn, cb, options) {
37
37
this . id = ++ uid // uid for batching
38
38
this . active = true
39
39
this . dirty = this . lazy // for lazy watchers
40
- this . deps = [ ]
41
- this . newDeps = [ ]
40
+ this . deps = Object . create ( null )
41
+ this . newDeps = null
42
42
this . prevError = null // for async error stacks
43
43
// parse expression for getter/setter
44
44
if ( isFn ) {
@@ -64,15 +64,12 @@ function Watcher (vm, expOrFn, cb, options) {
64
64
*/
65
65
66
66
Watcher . prototype . addDep = function ( dep ) {
67
- var newDeps = this . newDeps
68
- var old = this . deps
69
- if ( _ . indexOf ( newDeps , dep ) < 0 ) {
70
- newDeps . push ( dep )
71
- var i = _ . indexOf ( old , dep )
72
- if ( i < 0 ) {
67
+ var id = dep . id
68
+ if ( ! this . newDeps [ id ] ) {
69
+ this . newDeps [ id ] = dep
70
+ if ( ! this . deps [ id ] ) {
71
+ this . deps [ id ] = dep
73
72
dep . addSub ( this )
74
- } else {
75
- old [ i ] = null
76
73
}
77
74
}
78
75
}
@@ -169,6 +166,7 @@ Watcher.prototype.set = function (value) {
169
166
170
167
Watcher . prototype . beforeGet = function ( ) {
171
168
Dep . target = this
169
+ this . newDeps = Object . create ( null )
172
170
}
173
171
174
172
/**
@@ -177,17 +175,15 @@ Watcher.prototype.beforeGet = function () {
177
175
178
176
Watcher . prototype . afterGet = function ( ) {
179
177
Dep . target = null
180
- var oldDeps = this . deps
181
- var i = oldDeps . length
178
+ var ids = Object . keys ( this . deps )
179
+ var i = ids . length
182
180
while ( i -- ) {
183
- var dep = oldDeps [ i ]
184
- if ( dep ) {
185
- dep . removeSub ( this )
181
+ var id = ids [ i ]
182
+ if ( ! this . newDeps [ id ] ) {
183
+ this . deps [ id ] . removeSub ( this )
186
184
}
187
185
}
188
186
this . deps = this . newDeps
189
- this . newDeps = oldDeps
190
- oldDeps . length = 0 // reuse old dep array
191
187
}
192
188
193
189
/**
@@ -282,9 +278,10 @@ Watcher.prototype.evaluate = function () {
282
278
*/
283
279
284
280
Watcher . prototype . depend = function ( ) {
285
- var i = this . deps . length
281
+ var depIds = Object . keys ( this . deps )
282
+ var i = depIds . length
286
283
while ( i -- ) {
287
- this . deps [ i ] . depend ( )
284
+ this . deps [ depIds [ i ] ] . depend ( )
288
285
}
289
286
}
290
287
@@ -300,9 +297,10 @@ Watcher.prototype.teardown = function () {
300
297
if ( ! this . vm . _isBeingDestroyed ) {
301
298
this . vm . _watchers . $remove ( this )
302
299
}
303
- var i = this . deps . length
300
+ var depIds = Object . keys ( this . deps )
301
+ var i = depIds . length
304
302
while ( i -- ) {
305
- this . deps [ i ] . removeSub ( this )
303
+ this . deps [ depIds [ i ] ] . removeSub ( this )
306
304
}
307
305
this . active = false
308
306
this . vm = this . cb = this . value = null
0 commit comments