-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathPingTimer.ts
45 lines (37 loc) · 1.18 KB
/
PingTimer.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
import { clearTimeout as clearT, setTimeout as setT } from 'worker-timers'
import isBrowser, { isWebWorker } from './is-browser'
export default class PingTimer {
private keepalive: number
private timer: any
private checkPing: () => void
// dont directly assign globals to class props otherwise this throws in web workers: Uncaught TypeError: Illegal invocation
// See: https://stackoverflow.com/questions/9677985/uncaught-typeerror-illegal-invocation-in-chrome
private _setTimeout: typeof setT =
isBrowser && !isWebWorker
? setT
: (func, time) => setTimeout(func, time)
private _clearTimeout: typeof clearT =
isBrowser && !isWebWorker ? clearT : (timer) => clearTimeout(timer)
constructor(keepalive: number, checkPing: () => void) {
this.keepalive = keepalive * 1000
this.checkPing = checkPing
this.reschedule()
}
clear() {
if (this.timer) {
this._clearTimeout(this.timer)
this.timer = null
}
}
reschedule() {
this.clear()
this.timer = this._setTimeout(() => {
this.checkPing()
// prevent possible race condition where the timer is destroyed on _cleauUp
// and recreated here
if (this.timer) {
this.reschedule()
}
}, this.keepalive)
}
}