@@ -12,7 +12,7 @@ import {
12
12
} from '../util/index'
13
13
14
14
let uid = 0
15
- let prevTarget
15
+ const targetStack = [ ]
16
16
17
17
/**
18
18
* A watcher parses an expression, collects dependencies,
@@ -83,7 +83,7 @@ export default class Watcher {
83
83
* Evaluate the getter, and re-collect dependencies.
84
84
*/
85
85
get ( ) {
86
- this . beforeGet ( )
86
+ this . pushTarget ( )
87
87
let value : any
88
88
try {
89
89
value = this . getter . call ( this . vm , this . vm )
@@ -116,18 +116,26 @@ export default class Watcher {
116
116
if ( this . deep ) {
117
117
traverse ( value )
118
118
}
119
- this . afterGet ( )
119
+ this . popTarget ( )
120
+ this . cleanupDeps ( )
120
121
return value
121
122
}
122
123
123
124
/**
124
- * Prepare for dependency collection.
125
+ * Set this watcher as the active dep target
125
126
*/
126
- beforeGet ( ) {
127
- prevTarget = Dep . target
127
+ pushTarget ( ) {
128
+ if ( Dep . target ) targetStack . push ( Dep . target )
128
129
Dep . target = this
129
130
}
130
131
132
+ /**
133
+ * Restore previous dep target
134
+ */
135
+ popTarget ( ) {
136
+ Dep . target = targetStack . pop ( )
137
+ }
138
+
131
139
/**
132
140
* Add a dependency to this directive.
133
141
*/
@@ -145,8 +153,7 @@ export default class Watcher {
145
153
/**
146
154
* Clean up for dependency collection.
147
155
*/
148
- afterGet ( ) {
149
- Dep . target = prevTarget
156
+ cleanupDeps ( ) {
150
157
let i = this . deps . length
151
158
while ( i -- ) {
152
159
const dep = this . deps [ i ]
@@ -207,12 +214,8 @@ export default class Watcher {
207
214
* This only gets called for lazy watchers.
208
215
*/
209
216
evaluate ( ) {
210
- // avoid overwriting another watcher that is being
211
- // collected.
212
- const current = Dep . target
213
217
this . value = this . get ( )
214
218
this . dirty = false
215
- Dep . target = current
216
219
}
217
220
218
221
/**
0 commit comments