-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDep.ts
67 lines (53 loc) · 1.4 KB
/
Dep.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* @flow */
import * as _u from './VueUtils';
let uid = 0
/**
* A dep is an observable that can have multiple
* directives subscribing to it.
*/
export default class Dep {
static target?: Watcher;
id: number;
subs: Array < Watcher > ;
express = [];
constructor() {
this.id = uid++
this.subs = [];
}
addSub(sub: Watcher) {
this.subs.push(sub)
// console.warn("addSub", JSON.stringify(this.subs));
}
removeSub(sub: Watcher) {
_u.remove(this.subs, sub)
}
depend() {
if (Dep.target) {
// console.warn("dependdependdependdependdepend");
Dep.target.addDep(this)
}
}
notify() {
// stabilize the subscriber list first
// console.warn("notify", JSON.stringify(this.subs));
const subs = this.subs.slice();
// console.warn("notify", JSON.stringify(this.subs));
subs.sort((a, b) => a.id - b.id)
for (let i = 0, l = subs.length; i < l; i++) {
subs[i].update()
}
}
}
// The current target watcher being evaluated.
// This is globally unique because only one watcher
// can be evaluated at a time.
Dep.target = null
const targetStack = []
export function pushTarget(target?: Watcher) {
// targetStack.push(target)
Dep.target = target
}
export function popTarget() {
// targetStack.pop()
Dep.target = null;
}