-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathHeartbeatPlugin.ts
56 lines (45 loc) · 1.57 KB
/
HeartbeatPlugin.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
46
47
48
49
50
51
52
53
54
55
56
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: ReturnType<typeof setInterval> | undefined;
constructor(heartbeatInterval: number = 60000) {
this._interval = heartbeatInterval >= 30000 ? heartbeatInterval : 60000;
}
public startup(): Promise<void> {
clearInterval(this._intervalId);
this._intervalId = undefined;
// TODO: Do we want to send a heartbeat for the last user?
return Promise.resolve();
}
public suspend(): Promise<void> {
clearInterval(this._intervalId);
this._intervalId = undefined;
return Promise.resolve();
}
public run(context: EventPluginContext): Promise<void> {
if (this._interval <= 0) {
return Promise.resolve();
}
clearInterval(this._intervalId);
this._intervalId = undefined;
const { config } = context.client;
if (!config.currentSessionIdentifier) {
const user = context.event.data?.[KnownEventDataKeys.UserInfo];
if (!user?.identity) {
return Promise.resolve();
}
config.currentSessionIdentifier = user.identity;
}
if (config.currentSessionIdentifier) {
this._intervalId = setInterval(
() => void context.client.submitSessionHeartbeat(<string>config.currentSessionIdentifier),
this._interval
);
}
return Promise.resolve();
}
}