-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
258 lines (233 loc) · 8.39 KB
/
index.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
256
257
258
const WORKER = `
self.addEventListener(
'message',
function(event){
var message = event.data.split(':');
var cmd = message[0];
var ID = message[1];
var delay = parseInt(message[2]);
switch(cmd) {
case 'calibrate':
self.postMessage('calibrate:' + ID);
break;
case 'createTimeout':
var $timeout = setTimeout(function(){
self.postMessage('timeout:' + ID + ':' + $timeout);
}, delay);
self.postMessage('createTimeout:' + ID + ':' + $timeout);
break;
case 'createInterval':
var $interval = setInterval(function(){
self.postMessage('interval:' + ID + ':' + $interval);
}, delay);
self.postMessage('createInterval:' + ID + ':' + $interval);
break;
case 'clearTimeout':
clearTimeout(ID);
break;
case 'clearInterval':
clearInterval(ID);
break;
}
},
false
);
`;
interface TimerInfo {
fn: Function | string;
delay: number;
params: any[];
status: boolean;
timer: number;
}
class Timer {
private intervalMap: Map<number, TimerInfo>;
private timeoutMap: Map<number, TimerInfo>;
private immediateSet: Set<number>;
private worker: Worker;
private timeError: number;
private immediateCount: number;
private timerID: number;
constructor() {
this.intervalMap = new Map();
this.timeoutMap = new Map();
this.immediateSet = new Set();
this.timeError = 0;
this.immediateCount = 0;
this.timerID = 0;
this.worker = new Worker(
URL.createObjectURL(
new Blob([WORKER], { type: "application/javascript" })
)
);
this.$setWorkerEventListener();
}
calibrate() {
let now = Date.now();
this.worker.postMessage('calibrate:' + now.toString());
}
setTimeout(
functionOrCode: Function | string,
delay?: number,
...params: any[]
): number{
let timeDelay: number = delay || 0;
if(timeDelay > this.timeError) {
timeDelay -= this.timeError;
}
let timeoutID: number = this.$getRandomCode();
this.timeoutMap.set(timeoutID, {
fn: functionOrCode,
delay: timeDelay,
params: params || [],
status: false,
timer: 0
});
this.worker.postMessage('createTimeout:' + timeoutID + ':' + timeDelay);
return timeoutID;
}
setInterval(functionOrCode: Function | string, delay: number, ...params: any[]): number{
let timeDelay: number = delay || 0;
let intervalID: number = this.$getRandomCode();
this.intervalMap.set(intervalID, {
fn: functionOrCode,
delay: timeDelay,
params: params || [],
status: false,
timer: 0
});
this.worker.postMessage('createInterval:' + intervalID + ':' + timeDelay);
return intervalID;
}
setImmediate(functionOrCode: Function | string, ...params: any[]): number {
if(window['setImmediate']) {
let immediateID: number = window.setImmediate(functionOrCode, ...params);
this.immediateSet.add(immediateID);
return immediateID;
}
let postMessage = Date.now().toString(16);
let scope = this;
function immediateCallback(event: MessageEvent) {
window.removeEventListener('message', immediateCallback);
if(event.data === postMessage) {
if(typeof functionOrCode === 'function') {
functionOrCode.apply(scope, params);
} else {
eval(functionOrCode);
}
}
}
window.addEventListener(
'message',
immediateCallback
);
this.immediateCount ++;
window.postMessage(postMessage, '*');
return this.immediateCount;
}
clearTimeout(timeoutID: number) {
let interval: TimerInfo = this.intervalMap.get(timeoutID) as TimerInfo;
if(interval) {
this.worker.postMessage('clearInterval:' + interval.timer);
this.intervalMap.delete(timeoutID);
}
}
clearInterval(intervalID: number) {
let interval: TimerInfo = this.intervalMap.get(intervalID) as TimerInfo;
if(interval) {
this.worker.postMessage('clearInterval:' + interval.timer);
this.intervalMap.delete(intervalID);
}
}
clearImmediate(immediateID: number) {
if(window['clearImmediate']) {
clearImmediate(immediateID);
this.immediateSet.delete(immediateID);
return void 0;
}
}
clearAll() {
this.timeoutMap.forEach((info: TimerInfo, key: number) => {
this.clearTimeout(key);
});
this.intervalMap.forEach((info: TimerInfo, key: number) => {
this.clearInterval(key);
});
this.immediateSet.forEach((id: number) => {
this.clearImmediate(id);
});
}
hasTimer(timerID: number): boolean {
return (this.timeoutMap.has(timerID) || this.intervalMap.has(timerID) || this.immediateSet.has(timerID));
}
release() {
this.worker.terminate();
this.intervalMap = new Map();
this.timeoutMap = new Map();
this.immediateSet = new Set();
}
private $setWorkerEventListener() {
this.worker.addEventListener("message", event => {
let response: string[] = event.data.split(':')
let cmd: string = response[0];
let ID: number = parseInt(response[1]);
let mark: string = response[2];
switch (cmd) {
case 'createTimeout':
{
let timeoutInfo: TimerInfo = this.timeoutMap.get(ID) as TimerInfo;
if(timeoutInfo) {
timeoutInfo.status = true;
timeoutInfo.timer = parseInt(mark);
}
}
break;
case 'createInterval':
{
let intervalInfo: TimerInfo = this.intervalMap.get(ID) as TimerInfo;
if(intervalInfo) {
intervalInfo.status = true;
intervalInfo.timer = parseInt(mark);
}
}
break;
case 'timeout':
{
let timeoutInfo: TimerInfo = this.timeoutMap.get(ID) as TimerInfo;
if(timeoutInfo && timeoutInfo.status === true) {
if(typeof timeoutInfo.fn === 'function') {
/** @todo 確定下上下文 */
timeoutInfo.fn.apply(this, timeoutInfo.params);
} else {
eval(timeoutInfo.fn);
}
}
}
break;
case 'interval':
{
let intervalInfo: TimerInfo = this.intervalMap.get(ID) as TimerInfo;
if(intervalInfo && intervalInfo.status === true) {
if(typeof intervalInfo.fn === 'function') {
/** @todo 確定下上下文 */
intervalInfo.fn.apply(this, intervalInfo.params);
} else {
eval(intervalInfo.fn);
}
}
}
break;
case 'calibrate':
{
let recievedTime = Date.now();
let sentTime = ID;
this.timeError = recievedTime - sentTime;
}
break;
}
});
}
private $getRandomCode(): number {
return ++ this.timerID;
}
}