-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathpolling-scheduler.ts
More file actions
43 lines (33 loc) · 920 Bytes
/
polling-scheduler.ts
File metadata and controls
43 lines (33 loc) · 920 Bytes
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
import { createLogger } from '../../factories/logger-factory'
type Tick = () => Promise<void> | void
const debug = createLogger('dashboard-service:polling')
export class PollingScheduler {
private timer: NodeJS.Timer | undefined
public constructor(
private readonly intervalMs: number,
private readonly tick: Tick,
) { }
public start(): void {
if (this.timer) {
return
}
debug('starting scheduler with interval %d ms', this.intervalMs)
this.timer = setInterval(() => {
Promise.resolve(this.tick())
.catch((error) => {
console.error('dashboard-service: polling tick failed', error)
})
}, this.intervalMs)
}
public stop(): void {
if (!this.timer) {
return
}
debug('stopping scheduler')
clearInterval(this.timer)
this.timer = undefined
}
public isRunning(): boolean {
return Boolean(this.timer)
}
}