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
42 lines (35 loc) · 1.22 KB
/
HeartbeatPlugin.ts
File metadata and controls
42 lines (35 loc) · 1.22 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
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 _heartbeatIntervalId: any;
private _lastUser: IUserInfo;
public run(context: EventPluginContext, next?: () => void): void {
let clearHeartbeatInterval = () => {
if (this._heartbeatIntervalId) {
clearInterval(this._heartbeatIntervalId);
this._heartbeatIntervalId = 0;
}
};
let type = context.event.type;
if (type !== 'heartbeat') {
if (type === 'sessionend') {
clearHeartbeatInterval();
} else {
let user: IUserInfo = context.event.data['@user'];
if (user && user.identity) {
let submitHeartbeatFn = () => context.client.createSessionHeartbeat().setUserIdentity(user).submit();
if (!this._heartbeatIntervalId) {
this._lastUser = user;
} else {
clearHeartbeatInterval();
}
this._heartbeatIntervalId = setInterval(submitHeartbeatFn, 30000);
}
}
}
next && next();
}
}