Skip to content

Add a global Vue.depTarget api support #8484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flow/global-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare interface GlobalAPI {
set: <T>(target: Object | Array<T>, key: string | number, value: T) => T;
delete: <T>(target: Object| Array<T>, key: string | number) => void;
nextTick: (fn: Function, context?: Object) => void | Promise<*>;
depTarget: (target: {storage: ?Watcher}) => {storage: ?Watcher} | void;
use: (plugin: Function | Object) => void;
mixin: (mixin: Object) => void;
compile: (template: string) => { render: Function, staticRenderFns: Array<Function> };
Expand Down
2 changes: 2 additions & 0 deletions src/core/global-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { initMixin } from './mixin'
import { initExtend } from './extend'
import { initAssetRegisters } from './assets'
import { set, del } from '../observer/index'
import { depTarget } from '../observer/dep'
import { ASSET_TYPES } from 'shared/constants'
import builtInComponents from '../components/index'

Expand Down Expand Up @@ -43,6 +44,7 @@ export function initGlobalAPI (Vue: GlobalAPI) {
Vue.set = set
Vue.delete = del
Vue.nextTick = nextTick
Vue.depTarget = depTarget

Vue.options = Object.create(null)
ASSET_TYPES.forEach(type => {
Expand Down
24 changes: 17 additions & 7 deletions src/core/observer/dep.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let uid = 0
* directives subscribing to it.
*/
export default class Dep {
static target: ?Watcher;
static target: {storage: ?Watcher};
id: number;
subs: Array<Watcher>;

Expand All @@ -28,8 +28,8 @@ export default class Dep {
}

depend () {
if (Dep.target) {
Dep.target.addDep(this)
if (Dep.target.storage) {
Dep.target.storage.addDep(this)
}
}

Expand All @@ -45,14 +45,24 @@ export default class Dep {
// the current target watcher being evaluated.
// this is globally unique because there could be only one
// watcher being evaluated at any time.
Dep.target = null
Dep.target = {
storage: null
}
const targetStack = []

export function pushTarget (_target: ?Watcher) {
if (Dep.target) targetStack.push(Dep.target)
Dep.target = _target
if (Dep.target.storage) targetStack.push(Dep.target.storage)
Dep.target.storage = _target
}

export function popTarget () {
Dep.target = targetStack.pop()
Dep.target.storage = targetStack.pop()
}

export function depTarget (target: {storage: ?Watcher}): {storage: ?Watcher} | void {
if (target) {
Dep.target = target
} else {
return Dep.target
}
}
2 changes: 1 addition & 1 deletion src/core/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function defineReactive (
configurable: true,
get: function reactiveGetter () {
const value = getter ? getter.call(obj) : val
if (Dep.target) {
if (Dep.target.storage) {
dep.depend()
if (childOb) {
childOb.dep.depend()
Expand Down
2 changes: 1 addition & 1 deletion src/core/observer/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export default class Watcher {
* Depend on this watcher. Only for computed property watchers.
*/
depend () {
if (this.dep && Dep.target) {
if (this.dep && Dep.target.storage) {
this.dep.depend()
}
}
Expand Down
10 changes: 5 additions & 5 deletions test/unit/modules/observer/dep.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ describe('Dep', () => {
let _target

beforeAll(() => {
_target = Dep.target
_target = Dep.target.storage
})

afterAll(() => {
Dep.target = _target
Dep.target.storage = _target
})

it('should do nothing if no target', () => {
Dep.target = null
Dep.target.storage = null
dep.depend()
})

it('should add itself to target', () => {
Dep.target = jasmine.createSpyObj('TARGET', ['addDep'])
Dep.target.storage = jasmine.createSpyObj('TARGET', ['addDep'])
dep.depend()
expect(Dep.target.addDep).toHaveBeenCalledWith(dep)
expect(Dep.target.storage.addDep).toHaveBeenCalledWith(dep)
})
})

Expand Down
12 changes: 6 additions & 6 deletions test/unit/modules/observer/observer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ describe('Observer', () => {
update: jasmine.createSpy()
}
// collect dep
Dep.target = watcher
Dep.target.storage = watcher
obj.a.b
Dep.target = null
Dep.target.storage = null
expect(watcher.deps.length).toBe(3) // obj.a + a + a.b
obj.a.b = 3
expect(watcher.update.calls.count()).toBe(1)
Expand All @@ -200,10 +200,10 @@ describe('Observer', () => {
expect(watcher.update.calls.count()).toBe(2)
watcher.deps = []

Dep.target = watcher
Dep.target.storage = watcher
obj.a.b
obj.c
Dep.target = null
Dep.target.storage = null
expect(watcher.deps.length).toBe(4)
// set on the swapped object
obj.a.b = 5
Expand Down Expand Up @@ -236,9 +236,9 @@ describe('Observer', () => {
update: jasmine.createSpy()
}
// collect dep
Dep.target = watcher
Dep.target.storage = watcher
expect(obj.a).toBe(2) // Make sure 'this' is preserved
Dep.target = null
Dep.target.storage = null
obj.a = 3
expect(obj.val).toBe(3) // make sure 'setter' was called
obj.val = 5
Expand Down