@@ -31,7 +31,8 @@ function Watcher (vm, expression, cb, options) {
3131 options = options || { }
3232 this . deep = ! ! options . deep
3333 this . user = ! ! options . user
34- this . deps = Object . create ( null )
34+ this . deps = [ ]
35+ this . newDeps = [ ]
3536 // setup filters if any.
3637 // We delegate directive filters here to the watcher
3738 // because they need to be included in the dependency
@@ -56,12 +57,15 @@ var p = Watcher.prototype
5657 */
5758
5859p . 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 ) {
6466 dep . addSub ( this )
67+ } else {
68+ old [ i ] = null
6569 }
6670 }
6771}
@@ -123,7 +127,6 @@ p.set = function (value) {
123127
124128p . beforeGet = function ( ) {
125129 Observer . target = this
126- this . newDeps = { }
127130}
128131
129132/**
@@ -132,12 +135,15 @@ p.beforeGet = function () {
132135
133136p . afterGet = function ( ) {
134137 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 )
138143 }
139144 }
140145 this . deps = this . newDeps
146+ this . newDeps = [ ]
141147}
142148
143149/**
@@ -220,8 +226,9 @@ p.teardown = function () {
220226 if ( ! this . vm . _isBeingDestroyed ) {
221227 this . vm . _watcherList . $remove ( this )
222228 }
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 )
225232 }
226233 this . active = false
227234 this . vm = this . cbs = this . value = null
0 commit comments