-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
100 lines (81 loc) · 1.59 KB
/
index.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import Token from 'cancellation-token';
function job(cb, token) {
return function execJob() {
if (token.cancelled === false) {
cb();
}
};
}
export class Scheduler {
constructor(runner) {
this.sync = [];
this.layout = [];
this.measure = [];
this.affect = [];
this._nextFlush = null;
this._runner = runner;
}
schedule(queueName, cb, parent) {
let token = new Token(parent);
this[queueName].push(job(cb, token));
this._flush();
return token;
}
forget(token) {
if (token) {
token.cancel();
}
}
_flush() {
if (this._nextFlush !== null) {
return;
}
this._nextFlush = requestAnimationFrame(() => {
this._nextFlush = null;
this.flush();
});
}
flush() {
let i;
let q;
if (this._runner) {
this._runner.begin();
}
if (this.sync.length) {
q = this.sync;
this.sync = [];
for (i = 0; i < q.length; i++) {
q[i]();
}
}
if (this.layout.length) {
q = this.layout;
this.layout = [];
for (i = 0; i < q.length; i++) {
q[i]();
}
}
if (this._runner) {
this._runner.end();
this._runner.begin();
}
if (this.measure.length) {
q = this.measure;
this.measure = [];
for (i = 0; i < q.length; i++) {
q[i]();
}
}
if (this.affect.length) {
q = this.affect;
this.affect = [];
for (i = 0; i < q.length; i++) {
q[i]();
}
}
if (this._runner) {
this._runner.end();
}
}
}
export default new Scheduler();