forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeartbeatPlugin.ts
More file actions
43 lines (35 loc) · 1.2 KB
/
HeartbeatPlugin.ts
File metadata and controls
43 lines (35 loc) · 1.2 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
import { KnownEventDataKeys } from "../../models/Event.js";
import { EventPluginContext } from "../EventPluginContext.js";
import { IEventPlugin } from "../IEventPlugin.js";
export class HeartbeatPlugin implements IEventPlugin {
public priority = 100;
public name = "HeartbeatPlugin";
private _interval: number;
private _intervalId = 0;
constructor(heartbeatInterval: number = 30000) {
this._interval = heartbeatInterval >= 30000 ? heartbeatInterval : 60000;
}
public startup(): Promise<void> {
clearInterval(this._intervalId);
this._intervalId = 0;
// TODO: Do we want to send a heartbeat for the last user?
return Promise.resolve();
}
public suspend(): Promise<void> {
clearInterval(this._intervalId);
this._intervalId = 0;
return Promise.resolve();
}
public run(context: EventPluginContext): Promise<void> {
clearInterval(this._intervalId);
this._intervalId = 0;
const user = context.event.data?.[KnownEventDataKeys.UserInfo];
if (user?.identity) {
this._intervalId = setInterval(
() => void context.client.submitSessionHeartbeat(<string>user.identity),
this._interval,
);
}
return Promise.resolve();
}
}