-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathscheduler.ts
255 lines (226 loc) · 6.59 KB
/
scheduler.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
Copyright 2017 LinkedIn Corp. Licensed under the Apache License,
Version 2.0 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License
at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
import {
EngineInterface,
SchedulerInterface,
ElementSchedulerInterface,
FrameInterface,
QueueInterface,
MetaInterface
} from './interfaces';
import W from './window-proxy';
import { default as Queue, DOMQueue } from './queue';
import { getGlobalEngine } from './engine';
import { getBoundingClientRect } from '../utils';
const TOKEN_SEED = 'xxxx'.replace(/[xy]/g, function(c) {
let r = (Math.random() * 16) | 0,
v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
let tokenCounter = 0;
export class Frame implements FrameInterface {
constructor(
public dateNow: number,
public highResTime: number,
public scrollTop: number,
public scrollLeft: number,
public width: number,
public height: number,
public x: number,
public y: number,
public top: number,
public left: number
) {}
static generate(root: Element | Window = window): Frame {
const rootMeta = this.revalidateRootMeta(root);
return new Frame(
Date.now(),
performance.now(),
rootMeta.scrollTop,
rootMeta.scrollLeft,
rootMeta.width,
rootMeta.height,
rootMeta.x,
rootMeta.y,
rootMeta.top,
rootMeta.left
);
}
static revalidateRootMeta(root: any = document): MetaInterface {
let _clientRect = null;
let _rootMeta: MetaInterface = {
width: 0,
height: 0,
scrollTop: 0,
scrollLeft: 0,
x: 0,
y: 0,
top: 0,
left: 0
};
// if root is dirty update the cached values
if (W.isDirty) {
W.updateMeta();
}
if (root === window || root === document) {
_rootMeta.height = W.meta.height;
_rootMeta.width = W.meta.width;
_rootMeta.scrollLeft = W.meta.scrollLeft;
_rootMeta.scrollTop = W.meta.scrollTop;
return _rootMeta;
}
_clientRect = getBoundingClientRect(root);
_rootMeta.scrollTop = root.scrollTop;
_rootMeta.scrollLeft = root.scrollLeft;
_rootMeta.width = _clientRect.width;
_rootMeta.height = _clientRect.height;
_rootMeta.x = _clientRect.x;
_rootMeta.y = _clientRect.y;
_rootMeta.top = _clientRect.top;
_rootMeta.left = _clientRect.left;
return _rootMeta;
}
}
export function generateToken() {
return tokenCounter++ + TOKEN_SEED;
}
export abstract class BaseScheduler {
protected root: Element | Window;
protected engine: EngineInterface;
protected queue: QueueInterface;
protected isTicking: Boolean = false;
protected toRemove: Array<string | Element | Function> = [];
protected id?: string;
constructor(customEngine?: EngineInterface, root?: Element | Window | null) {
if (customEngine) {
this.engine = customEngine;
} else {
this.engine = getGlobalEngine();
}
this.root = root || window;
}
protected abstract applyQueue(frame: Frame): void;
protected tick() {
if (this.queue.isEmpty()) {
this.isTicking = false;
} else {
if (this.toRemove.length > 0) {
for (let i = 0; i < this.toRemove.length; i++) {
this.queue.remove(this.toRemove[i]);
}
this.toRemove = [];
}
this.applyQueue(Frame.generate(this.root));
this.engine.scheduleRead(this.tick.bind(this));
}
}
scheduleWork(callback: Function) {
this.engine.scheduleWork(callback);
}
scheduleRead(callback: Function) {
this.engine.scheduleRead(callback);
}
queryElement(el: Element, callback: (clientRect: ClientRect, frame: Frame) => void) {
let clientRect: ClientRect;
let frame: Frame;
this.engine.scheduleRead(() => {
clientRect = getBoundingClientRect(el);
frame = Frame.generate(this.root);
});
this.engine.scheduleWork(() => {
callback(clientRect, frame);
});
}
unwatch(id: string | Element | Function) {
this.toRemove.push(id);
}
unwatchAll() {
this.queue.clear();
}
startTicking() {
if (!this.isTicking) {
this.isTicking = true;
this.engine.scheduleRead(this.tick.bind(this));
}
}
}
export class Scheduler extends BaseScheduler implements SchedulerInterface {
protected queue: Queue = new Queue();
applyQueue(frame: Frame) {
for (let i = 0; i < this.queue.items.length; i++) {
let { id, callback } = this.queue.items[i];
callback(frame, id);
}
}
watch(callback: (frame: FrameInterface) => void): string {
this.startTicking();
let id = generateToken();
this.queue.push({
callback,
id
});
return id;
}
}
export class PredicatedScheduler extends Scheduler implements SchedulerInterface {
predicate: (frame: Frame) => Boolean;
constructor(predicate: (frame: Frame) => Boolean) {
super(undefined, window);
this.predicate = predicate;
}
applyQueue(frame: Frame) {
if (this.predicate(frame)) {
super.applyQueue(frame);
}
}
}
export class ElementScheduler extends BaseScheduler implements ElementSchedulerInterface {
protected queue: DOMQueue;
protected lastVersion: number = W.version;
protected ALLOW_CACHED_SCHEDULER: boolean;
constructor(customEngine?: EngineInterface, root?: Element | Window | null, ALLOW_CACHED_SCHEDULER: boolean = false) {
super(customEngine, root);
this.queue = new DOMQueue();
this.ALLOW_CACHED_SCHEDULER = ALLOW_CACHED_SCHEDULER;
}
get isDirty(): boolean {
return W.version !== this.lastVersion;
}
applyQueue(frame: Frame) {
for (let i = 0; i < this.queue.items.length; i++) {
let { callback, el, id, clientRect } = this.queue.items[i];
if (this.isDirty || !clientRect || !this.ALLOW_CACHED_SCHEDULER) {
clientRect = this.queue.items[i].clientRect = getBoundingClientRect(el);
}
callback(frame, id, clientRect);
}
this.lastVersion = W.version;
}
watch(
el: Element,
callback: (frame: FrameInterface, id: string, clientRect: ClientRect) => void,
id?: string
): string {
this.startTicking();
id = id || generateToken();
let clientRect = null;
this.queue.push({
el,
callback,
id,
clientRect
});
return id;
}
}
let globalScheduler: Scheduler | null = null;
export function getGlobalScheduler() {
return globalScheduler || (globalScheduler = new Scheduler());
}