Skip to content

Commit 3c3671b

Browse files
committed
Automatically submit heartbeats after calling setUserIdentity unless passed in interval is 0. removed client.config.useSessions().
1 parent a49f2e1 commit 3c3671b

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

packages/core/src/configuration/Configuration.ts

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { ConsoleLog } from "../logging/ConsoleLog.js";
55
import { NullLog } from "../logging/NullLog.js";
66
import { UserInfo } from "../models/data/UserInfo.js";
77
import { HeartbeatPlugin } from "../plugins/default/HeartbeatPlugin.js";
8-
import { ReferenceIdPlugin } from "../plugins/default/ReferenceIdPlugin.js";
98
import { EventPluginContext } from "../plugins/EventPluginContext.js";
109
import { IEventPlugin } from "../plugins/IEventPlugin.js";
1110
import { DefaultEventQueue } from "../queue/DefaultEventQueue.js";
@@ -362,23 +361,14 @@ export class Configuration {
362361
/**
363362
* Register an plugin to be used in this configuration.
364363
*/
365-
public addPlugin(
366-
name: string | undefined,
367-
priority: number,
368-
pluginAction: (context: EventPluginContext) => Promise<void>,
369-
): void;
370-
public addPlugin(
371-
pluginOrName: IEventPlugin | string | undefined,
372-
priority?: number,
373-
pluginAction?: (context: EventPluginContext) => Promise<void>,
374-
): void {
364+
public addPlugin(name: string | undefined, priority: number, pluginAction: (context: EventPluginContext) => Promise<void>): void;
365+
public addPlugin(pluginOrName: IEventPlugin | string | undefined, priority?: number, pluginAction?: (context: EventPluginContext) => Promise<void>): void {
375366
const plugin: IEventPlugin = pluginAction
376367
? { name: pluginOrName as string, priority, run: pluginAction }
377368
: pluginOrName as IEventPlugin;
369+
378370
if (!plugin || !(plugin.startup || plugin.run)) {
379-
this.services.log.error(
380-
"Add plugin failed: startup or run method not defined",
381-
);
371+
this.services.log.error("Add plugin failed: startup or run method not defined");
382372
return;
383373
}
384374

@@ -390,17 +380,8 @@ export class Configuration {
390380
plugin.priority = 0;
391381
}
392382

393-
let pluginExists = false;
394-
const plugins = this._plugins; // optimization for minifier.
395-
for (const p of plugins) {
396-
if (p.name === plugin.name) {
397-
pluginExists = true;
398-
break;
399-
}
400-
}
401-
402-
if (!pluginExists) {
403-
plugins.push(plugin);
383+
if (!this._plugins.find(f => f.name === plugin.name)) {
384+
this._plugins.push(plugin);
404385
}
405386
}
406387

@@ -443,26 +424,33 @@ export class Configuration {
443424
}
444425
}
445426

446-
public setUserIdentity(userInfo: UserInfo): void;
447-
public setUserIdentity(identity: string): void;
448-
public setUserIdentity(identity: string, name: string): void;
449-
public setUserIdentity(
450-
userInfoOrIdentity: UserInfo | string,
451-
name?: string,
452-
): void {
427+
/**
428+
* Set the default user identity for all events. If the heartbeat interval is
429+
* greater than 0 (default: 30000ms), heartbeats will be sent after the first
430+
* event submission.
431+
*/
432+
public setUserIdentity(userInfo: UserInfo, heartbeatInterval?: number): void;
433+
public setUserIdentity(identity: string, heartbeatInterval?: number): void;
434+
public setUserIdentity(identity: string, name: string, heartbeatInterval?: number): void;
435+
public setUserIdentity(userInfoOrIdentity: UserInfo | string, nameOrHeartbeatInterval?: string | number, heartbeatInterval: number = 30000): void {
436+
const name: string | undefined = typeof nameOrHeartbeatInterval === "string" ? nameOrHeartbeatInterval : undefined;
453437
const userInfo: UserInfo = typeof userInfoOrIdentity !== "string"
454438
? userInfoOrIdentity
455439
: { identity: userInfoOrIdentity, name };
456440

457-
const shouldRemove: boolean = !userInfo ||
458-
(!userInfo.identity && !userInfo.name);
441+
const interval: number = typeof nameOrHeartbeatInterval === "number" ? nameOrHeartbeatInterval : heartbeatInterval;
442+
const plugin = new HeartbeatPlugin(interval);
443+
444+
const shouldRemove: boolean = !userInfo || (!userInfo.identity && !userInfo.name);
459445
if (shouldRemove) {
446+
this.removePlugin(plugin)
460447
delete this.defaultData[KnownEventDataKeys.UserInfo];
461448
} else {
449+
this.addPlugin(plugin)
462450
this.defaultData[KnownEventDataKeys.UserInfo] = userInfo;
463451
}
464452

465-
this.services.log.info(`user identity: ${shouldRemove ? "null" : <string>userInfo.identity}`);
453+
this.services.log.info(`user identity: ${shouldRemove ? "null" : <string>userInfo.identity} (heartbeat interval: ${interval}ms)`);
466454
}
467455

468456
/**
@@ -472,15 +460,6 @@ export class Configuration {
472460
return "exceptionless-js/2.0.0-dev";
473461
}
474462

475-
/**
476-
* Automatically send a heartbeat to keep the session alive.
477-
*/
478-
public useSessions(sendHeartbeats = true, heartbeatInterval = 30000): void {
479-
if (sendHeartbeats) {
480-
this.addPlugin(new HeartbeatPlugin(heartbeatInterval));
481-
}
482-
}
483-
484463
/**
485464
* Use localStorage for persisting things like server configuration cache and persisted queue entries (depends on usePersistedQueueStorage).
486465
*/

packages/core/src/plugins/default/HeartbeatPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export class HeartbeatPlugin implements IEventPlugin {
2727
}
2828

2929
public run(context: EventPluginContext): Promise<void> {
30+
if (this._interval <= 0) {
31+
return Promise.resolve();
32+
}
33+
3034
clearInterval(this._intervalId);
3135
this._intervalId = 0;
3236

0 commit comments

Comments
 (0)