-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.ts
68 lines (54 loc) · 1.18 KB
/
tracker.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
68
import { Atom } from 'mobx';
let nextComputationId = 0;
let currentComputation = null;
let _computations = {};
let active = true;
class Dependency {
private atom: Atom;
private _dependentsById: {[id: string]: Computation} = {};
constructor() {
this.atom = new Atom('Tracker');
}
changed(): void {
this.atom.reportChanged();
}
depend(computation?: Computation): boolean {
if (! computation) {
if (! Tracker.active)
return false;
computation = Tracker.currentComputation;
}
var id = computation._id;
if (! (id in this._dependentsById)) {
this._dependentsById[id] = computation;
computation.onInvalidate(() => {
delete this._dependentsById[id];
});
return true;
}
return false;
}
hasDependents(): boolean {
return true;
}
}
class Computation {
stopped: boolean;
firstRun: boolean = true;
_id: number;
constructor() {
this._id = nextComputationId++;
_computations[this._id] = this;
}
onInvalidate(callback: (computation?: Computation) => void) {
}
}
function autorun() {
}
export const Tracker = {
Dependency,
Computation,
autorun,
currentComputation,
active
};