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
36 lines (28 loc) · 1.03 KB
/
HeartbeatPlugin.ts
File metadata and controls
36 lines (28 loc) · 1.03 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
import { IEventPlugin } from '../IEventPlugin';
import { EventPluginContext } from '../EventPluginContext';
import { IUserInfo } from '../../models/IUserInfo';
export class HeartbeatPlugin implements IEventPlugin {
public priority: number = 100;
public name: string = 'HeartbeatPlugin';
private _heartbeatInterval: number;
private _heartbeatIntervalId: any;
constructor (heartbeatInterval: number = 30000) {
this._heartbeatInterval = heartbeatInterval;
}
public run(context: EventPluginContext, next?: () => void): void {
let clearHeartbeatInterval = () => {
if (this._heartbeatIntervalId) {
clearInterval(this._heartbeatIntervalId);
this._heartbeatIntervalId = 0;
}
};
if (this._heartbeatIntervalId) {
clearHeartbeatInterval();
}
let user: IUserInfo = context.event.data['@user'];
if (user && user.identity) {
this._heartbeatIntervalId = setInterval(() => context.client.submitSessionHeartbeat(user.identity), this._heartbeatInterval);
}
next && next();
}
}