@@ -45,8 +45,10 @@ export default function Watcher (vm, expOrFn, cb, options) {
45
45
this . id = ++ uid // uid for batching
46
46
this . active = true
47
47
this . dirty = this . lazy // for lazy watchers
48
- this . deps = Object . create ( null )
49
- this . newDeps = null
48
+ this . deps = [ ]
49
+ this . newDeps = [ ]
50
+ this . depIds = Object . create ( null )
51
+ this . newDepIds = null
50
52
this . prevError = null // for async error stacks
51
53
// parse expression for getter/setter
52
54
if ( isFn ) {
@@ -65,23 +67,6 @@ export default function Watcher (vm, expOrFn, cb, options) {
65
67
this . queued = this . shallow = false
66
68
}
67
69
68
- /**
69
- * Add a dependency to this directive.
70
- *
71
- * @param {Dep } dep
72
- */
73
-
74
- Watcher . prototype . addDep = function ( dep ) {
75
- var id = dep . id
76
- if ( ! this . newDeps [ id ] ) {
77
- this . newDeps [ id ] = dep
78
- if ( ! this . deps [ id ] ) {
79
- this . deps [ id ] = dep
80
- dep . addSub ( this )
81
- }
82
- }
83
- }
84
-
85
70
/**
86
71
* Evaluate the getter, and re-collect dependencies.
87
72
*/
@@ -179,7 +164,25 @@ Watcher.prototype.set = function (value) {
179
164
180
165
Watcher . prototype . beforeGet = function ( ) {
181
166
Dep . target = this
182
- this . newDeps = Object . create ( null )
167
+ this . newDepIds = Object . create ( null )
168
+ this . newDeps . length = 0
169
+ }
170
+
171
+ /**
172
+ * Add a dependency to this directive.
173
+ *
174
+ * @param {Dep } dep
175
+ */
176
+
177
+ Watcher . prototype . addDep = function ( dep ) {
178
+ var id = dep . id
179
+ if ( ! this . newDepIds [ id ] ) {
180
+ this . newDepIds [ id ] = true
181
+ this . newDeps . push ( dep )
182
+ if ( ! this . depIds [ id ] ) {
183
+ dep . addSub ( this )
184
+ }
185
+ }
183
186
}
184
187
185
188
/**
@@ -188,15 +191,17 @@ Watcher.prototype.beforeGet = function () {
188
191
189
192
Watcher . prototype . afterGet = function ( ) {
190
193
Dep . target = null
191
- var ids = Object . keys ( this . deps )
192
- var i = ids . length
194
+ var i = this . deps . length
193
195
while ( i -- ) {
194
- var id = ids [ i ]
195
- if ( ! this . newDeps [ id ] ) {
196
- this . deps [ id ] . removeSub ( this )
196
+ var dep = this . deps [ i ]
197
+ if ( ! this . newDepIds [ dep . id ] ) {
198
+ dep . removeSub ( this )
197
199
}
198
200
}
201
+ this . depIds = this . newDepIds
202
+ var tmp = this . deps
199
203
this . deps = this . newDeps
204
+ this . newDepIds = tmp
200
205
}
201
206
202
207
/**
@@ -291,10 +296,9 @@ Watcher.prototype.evaluate = function () {
291
296
*/
292
297
293
298
Watcher . prototype . depend = function ( ) {
294
- var depIds = Object . keys ( this . deps )
295
- var i = depIds . length
299
+ var i = this . deps . length
296
300
while ( i -- ) {
297
- this . deps [ depIds [ i ] ] . depend ( )
301
+ this . deps [ i ] . depend ( )
298
302
}
299
303
}
300
304
@@ -311,10 +315,9 @@ Watcher.prototype.teardown = function () {
311
315
if ( ! this . vm . _isBeingDestroyed && ! this . vm . _vForRemoving ) {
312
316
this . vm . _watchers . $remove ( this )
313
317
}
314
- var depIds = Object . keys ( this . deps )
315
- var i = depIds . length
318
+ var i = this . deps . length
316
319
while ( i -- ) {
317
- this . deps [ depIds [ i ] ] . removeSub ( this )
320
+ this . deps [ i ] . removeSub ( this )
318
321
}
319
322
this . active = false
320
323
this . vm = this . cb = this . value = null
0 commit comments