Skip to content

Commit 35d137c

Browse files
committed
move dep target logic into Dep itself
1 parent 2c63e94 commit 35d137c

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

src/core/observer/dep.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ export default class Dep {
4646
// this is globally unique because there could be only one
4747
// watcher being evaluated at any time.
4848
Dep.target = null
49+
const targetStack = []
50+
51+
export function pushTarget (_target: Watcher) {
52+
if (Dep.target) targetStack.push(Dep.target)
53+
Dep.target = _target
54+
}
55+
56+
export function popTarget () {
57+
Dep.target = targetStack.pop()
58+
}

src/core/observer/watcher.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import config from '../config'
4-
import Dep from './dep'
4+
import Dep, { pushTarget, popTarget } from './dep'
55
import { queueWatcher } from './scheduler'
66
import {
77
warn,
@@ -83,7 +83,7 @@ export default class Watcher {
8383
* Evaluate the getter, and re-collect dependencies.
8484
*/
8585
get () {
86-
this.pushTarget()
86+
pushTarget(this)
8787
let value: any
8888
try {
8989
value = this.getter.call(this.vm, this.vm)
@@ -116,26 +116,11 @@ export default class Watcher {
116116
if (this.deep) {
117117
traverse(value)
118118
}
119-
this.popTarget()
119+
popTarget()
120120
this.cleanupDeps()
121121
return value
122122
}
123123

124-
/**
125-
* Set this watcher as the active dep target
126-
*/
127-
pushTarget () {
128-
if (Dep.target) targetStack.push(Dep.target)
129-
Dep.target = this
130-
}
131-
132-
/**
133-
* Restore previous dep target
134-
*/
135-
popTarget () {
136-
Dep.target = targetStack.pop()
137-
}
138-
139124
/**
140125
* Add a dependency to this directive.
141126
*/

0 commit comments

Comments
 (0)