-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreateFakeSetTimerSync.js
More file actions
45 lines (43 loc) · 1.55 KB
/
CreateFakeSetTimerSync.js
File metadata and controls
45 lines (43 loc) · 1.55 KB
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
class FakeTimer {
constructor() {
this.original = { //saving original functions in this object
setTimeout: window.setTimeout,
clearTimeout: window.clearTimeout,
dateNow: Date.now,
}
this.timerId = 1; //tracks the id of setTimeout()
this.currentTime = 0; //tracks the currentTime
this.queue = []; //stores the settimeouts
}
install() {
window.setTimeout = (cb, time, ...args) => {
const id = this.timerId++;
this.queue.push({ //new settimeout and various data related to it is pushed
id,
cb,
time: time + this.currentTime,
args,
});
this.queue.sort((a, b) => a.time - b.time); //sorting in ascending order of the time of each settimeout
return id;
}
window.clearTimeout = (removeId) => {
this.queue = this.queue.filter(({ id }) => id !== removeId); //settimeout with id = removeId is filtered out
}
Date.now = () => {
return this.currentTime;
}
}
uninstall() {// original functions are reassigned
window.setTimeout = this.original.setTimeout;
window.clearTimeout = this.original.clearTimeout;
Date.now = this.original.dateNow;
}
tick() { //calling this starts the execution of all settimeouts synchronously
while(this.queue.length) {
const { cb, time, args } = this.queue.shift(); //settimeout is popped from the top
this.currentTime = time; //current time is set to the time after execution of this settimeout
cb(...args); //callback of the settimeout is called with its arguments
}
}
}