|
1 | | -type HeartbeatServiceOptions = { |
2 | | - heartbeat: () => Promise<void>; |
| 1 | +type IntervalServiceOptions = { |
| 2 | + onInterval: () => Promise<void>; |
| 3 | + onError?: (error: unknown) => Promise<void>; |
3 | 4 | intervalMs?: number; |
4 | 5 | leadingEdge?: boolean; |
5 | | - onError?: (error: unknown) => Promise<void>; |
6 | 6 | }; |
7 | 7 |
|
8 | | -export class HeartbeatService { |
9 | | - private _heartbeat: () => Promise<void>; |
| 8 | +export class IntervalService { |
| 9 | + private _onInterval: () => Promise<void>; |
| 10 | + private _onError?: (error: unknown) => Promise<void>; |
| 11 | + |
10 | 12 | private _intervalMs: number; |
11 | | - private _nextHeartbeat: NodeJS.Timeout | undefined; |
| 13 | + private _nextInterval: NodeJS.Timeout | undefined; |
12 | 14 | private _leadingEdge: boolean; |
13 | | - private _isHeartbeating: boolean; |
14 | | - private _onError?: (error: unknown) => Promise<void>; |
| 15 | + private _isEnabled: boolean; |
| 16 | + |
| 17 | + constructor(opts: IntervalServiceOptions) { |
| 18 | + this._onInterval = opts.onInterval; |
| 19 | + this._onError = opts.onError; |
15 | 20 |
|
16 | | - constructor(opts: HeartbeatServiceOptions) { |
17 | | - this._heartbeat = opts.heartbeat; |
18 | 21 | this._intervalMs = opts.intervalMs ?? 45_000; |
19 | | - this._nextHeartbeat = undefined; |
| 22 | + this._nextInterval = undefined; |
20 | 23 | this._leadingEdge = opts.leadingEdge ?? false; |
21 | | - this._isHeartbeating = false; |
22 | | - this._onError = opts.onError; |
| 24 | + this._isEnabled = false; |
23 | 25 | } |
24 | 26 |
|
25 | 27 | start() { |
26 | | - if (this._isHeartbeating) { |
| 28 | + if (this._isEnabled) { |
27 | 29 | return; |
28 | 30 | } |
29 | 31 |
|
30 | | - this._isHeartbeating = true; |
| 32 | + this._isEnabled = true; |
31 | 33 |
|
32 | 34 | if (this._leadingEdge) { |
33 | | - this.#doHeartbeat(); |
| 35 | + this.#doInterval(); |
34 | 36 | } else { |
35 | | - this.#scheduleNextHeartbeat(); |
| 37 | + this.#scheduleNextInterval(); |
36 | 38 | } |
37 | 39 | } |
38 | 40 |
|
39 | 41 | stop() { |
40 | | - if (!this._isHeartbeating) { |
| 42 | + if (!this._isEnabled) { |
41 | 43 | return; |
42 | 44 | } |
43 | 45 |
|
44 | | - this._isHeartbeating = false; |
45 | | - this.#clearNextHeartbeat(); |
| 46 | + this._isEnabled = false; |
| 47 | + this.#clearNextInterval(); |
46 | 48 | } |
47 | 49 |
|
48 | 50 | resetCurrentInterval() { |
49 | | - if (!this._isHeartbeating) { |
| 51 | + if (!this._isEnabled) { |
50 | 52 | return; |
51 | 53 | } |
52 | 54 |
|
53 | | - this.#clearNextHeartbeat(); |
54 | | - this.#scheduleNextHeartbeat(); |
| 55 | + this.#clearNextInterval(); |
| 56 | + this.#scheduleNextInterval(); |
55 | 57 | } |
56 | 58 |
|
57 | 59 | updateInterval(intervalMs: number) { |
58 | 60 | this._intervalMs = intervalMs; |
59 | 61 | this.resetCurrentInterval(); |
60 | 62 | } |
61 | 63 |
|
62 | | - #doHeartbeat = async () => { |
63 | | - this.#clearNextHeartbeat(); |
| 64 | + #doInterval = async () => { |
| 65 | + this.#clearNextInterval(); |
64 | 66 |
|
65 | | - if (!this._isHeartbeating) { |
| 67 | + if (!this._isEnabled) { |
66 | 68 | return; |
67 | 69 | } |
68 | 70 |
|
69 | 71 | try { |
70 | | - await this._heartbeat(); |
| 72 | + await this._onInterval(); |
71 | 73 | } catch (error) { |
72 | 74 | if (this._onError) { |
73 | 75 | try { |
74 | 76 | await this._onError(error); |
75 | 77 | } catch (error) { |
76 | | - console.error("Error handling heartbeat error", error); |
| 78 | + console.error("Error during interval error handler", error); |
77 | 79 | } |
78 | 80 | } |
79 | 81 | } |
80 | 82 |
|
81 | | - this.#scheduleNextHeartbeat(); |
| 83 | + this.#scheduleNextInterval(); |
82 | 84 | }; |
83 | 85 |
|
84 | | - #clearNextHeartbeat() { |
85 | | - if (this._nextHeartbeat) { |
86 | | - clearTimeout(this._nextHeartbeat); |
| 86 | + #clearNextInterval() { |
| 87 | + if (this._nextInterval) { |
| 88 | + clearTimeout(this._nextInterval); |
87 | 89 | } |
88 | 90 | } |
89 | 91 |
|
90 | | - #scheduleNextHeartbeat() { |
91 | | - this._nextHeartbeat = setTimeout(this.#doHeartbeat, this._intervalMs); |
| 92 | + #scheduleNextInterval() { |
| 93 | + this._nextInterval = setTimeout(this.#doInterval, this._intervalMs); |
92 | 94 | } |
93 | 95 | } |
0 commit comments