@@ -31,7 +31,8 @@ function Watcher (vm, expression, cb, options) {
31
31
options = options || { }
32
32
this . deep = ! ! options . deep
33
33
this . user = ! ! options . user
34
- this . deps = Object . create ( null )
34
+ this . deps = [ ]
35
+ this . newDeps = [ ]
35
36
// setup filters if any.
36
37
// We delegate directive filters here to the watcher
37
38
// because they need to be included in the dependency
@@ -56,12 +57,15 @@ var p = Watcher.prototype
56
57
*/
57
58
58
59
p . addDep = function ( dep ) {
59
- var id = dep . id
60
- if ( ! this . newDeps [ id ] ) {
61
- this . newDeps [ id ] = dep
62
- if ( ! this . deps [ id ] ) {
63
- this . deps [ id ] = dep
60
+ var newDeps = this . newDeps
61
+ var old = this . deps
62
+ if ( _ . indexOf ( newDeps , dep ) < 0 ) {
63
+ newDeps . push ( dep )
64
+ var i = _ . indexOf ( old , dep )
65
+ if ( i < 0 ) {
64
66
dep . addSub ( this )
67
+ } else {
68
+ old [ i ] = null
65
69
}
66
70
}
67
71
}
@@ -123,7 +127,6 @@ p.set = function (value) {
123
127
124
128
p . beforeGet = function ( ) {
125
129
Observer . target = this
126
- this . newDeps = { }
127
130
}
128
131
129
132
/**
@@ -132,12 +135,15 @@ p.beforeGet = function () {
132
135
133
136
p . afterGet = function ( ) {
134
137
Observer . target = null
135
- for ( var id in this . deps ) {
136
- if ( ! this . newDeps [ id ] ) {
137
- this . deps [ id ] . removeSub ( this )
138
+ var i = this . deps . length
139
+ while ( i -- ) {
140
+ var dep = this . deps [ i ]
141
+ if ( dep ) {
142
+ dep . removeSub ( this )
138
143
}
139
144
}
140
145
this . deps = this . newDeps
146
+ this . newDeps = [ ]
141
147
}
142
148
143
149
/**
@@ -220,8 +226,9 @@ p.teardown = function () {
220
226
if ( ! this . vm . _isBeingDestroyed ) {
221
227
this . vm . _watcherList . $remove ( this )
222
228
}
223
- for ( var id in this . deps ) {
224
- this . deps [ id ] . removeSub ( this )
229
+ var i = this . deps . length
230
+ while ( i -- ) {
231
+ this . deps [ i ] . removeSub ( this )
225
232
}
226
233
this . active = false
227
234
this . vm = this . cbs = this . value = null
0 commit comments