-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (56 loc) · 1.41 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
const originalSetTimeout = setTimeout
const originalClearTimeout = clearTimeout
const originalSetInterval = setInterval
const originalClearInterval = clearInterval
const timeoutTimerSet = new Set()
const intervalTimerSet = new Set()
setTimeout = (callback, delay, ...args) => {
let timer
const callbackWrapper = (...args) => {
timeoutTimerSet.delete(timer)
callback(...args)
}
timer = originalSetTimeout(callbackWrapper, delay, ...args)
timeoutTimerSet.add(timer)
return timer
}
clearTimeout = (timer) => {
timeoutTimerSet.delete(timer)
originalClearTimeout(timer)
}
setInterval = (callback, delay, ...args) => {
const timer = originalSetInterval(callback, delay, ...args)
intervalTimerSet.add(timer)
return timer
}
clearInterval = (timer) => {
intervalTimerSet.delete(timer)
originalClearInterval(timer)
}
const TimerManager = {
clearAll() {
for (const timer of timeoutTimerSet) {
if (timer) {
originalClearTimeout(timer)
}
}
for (const timer of intervalTimerSet) {
if (timer) {
originalClearInterval(timer)
}
}
},
original: {
setTimeout: originalSetTimeout,
clearTimeout: originalClearTimeout,
setInterval: originalSetInterval,
clearInterval: originalClearInterval,
},
getTimeoutTimerSet() {
return timeoutTimerSet
},
getIntervalTimerSet() {
return intervalTimerSet
},
}
module.exports = TimerManager