Skip to content

Commit

Permalink
Automatically submit heartbeats after calling setUserIdentity unless …
Browse files Browse the repository at this point in the history
…passed in interval is 0. removed client.config.useSessions().
  • Loading branch information
niemyjski committed Jun 24, 2021
1 parent a49f2e1 commit 3c3671b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
67 changes: 23 additions & 44 deletions packages/core/src/configuration/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ConsoleLog } from "../logging/ConsoleLog.js";
import { NullLog } from "../logging/NullLog.js";
import { UserInfo } from "../models/data/UserInfo.js";
import { HeartbeatPlugin } from "../plugins/default/HeartbeatPlugin.js";
import { ReferenceIdPlugin } from "../plugins/default/ReferenceIdPlugin.js";
import { EventPluginContext } from "../plugins/EventPluginContext.js";
import { IEventPlugin } from "../plugins/IEventPlugin.js";
import { DefaultEventQueue } from "../queue/DefaultEventQueue.js";
Expand Down Expand Up @@ -362,23 +361,14 @@ export class Configuration {
/**
* Register an plugin to be used in this configuration.
*/
public addPlugin(
name: string | undefined,
priority: number,
pluginAction: (context: EventPluginContext) => Promise<void>,
): void;
public addPlugin(
pluginOrName: IEventPlugin | string | undefined,
priority?: number,
pluginAction?: (context: EventPluginContext) => Promise<void>,
): void {
public addPlugin(name: string | undefined, priority: number, pluginAction: (context: EventPluginContext) => Promise<void>): void;
public addPlugin(pluginOrName: IEventPlugin | string | undefined, priority?: number, pluginAction?: (context: EventPluginContext) => Promise<void>): void {
const plugin: IEventPlugin = pluginAction
? { name: pluginOrName as string, priority, run: pluginAction }
: pluginOrName as IEventPlugin;

if (!plugin || !(plugin.startup || plugin.run)) {
this.services.log.error(
"Add plugin failed: startup or run method not defined",
);
this.services.log.error("Add plugin failed: startup or run method not defined");
return;
}

Expand All @@ -390,17 +380,8 @@ export class Configuration {
plugin.priority = 0;
}

let pluginExists = false;
const plugins = this._plugins; // optimization for minifier.
for (const p of plugins) {
if (p.name === plugin.name) {
pluginExists = true;
break;
}
}

if (!pluginExists) {
plugins.push(plugin);
if (!this._plugins.find(f => f.name === plugin.name)) {
this._plugins.push(plugin);
}
}

Expand Down Expand Up @@ -443,26 +424,33 @@ export class Configuration {
}
}

public setUserIdentity(userInfo: UserInfo): void;
public setUserIdentity(identity: string): void;
public setUserIdentity(identity: string, name: string): void;
public setUserIdentity(
userInfoOrIdentity: UserInfo | string,
name?: string,
): void {
/**
* Set the default user identity for all events. If the heartbeat interval is
* greater than 0 (default: 30000ms), heartbeats will be sent after the first
* event submission.
*/
public setUserIdentity(userInfo: UserInfo, heartbeatInterval?: number): void;
public setUserIdentity(identity: string, heartbeatInterval?: number): void;
public setUserIdentity(identity: string, name: string, heartbeatInterval?: number): void;
public setUserIdentity(userInfoOrIdentity: UserInfo | string, nameOrHeartbeatInterval?: string | number, heartbeatInterval: number = 30000): void {
const name: string | undefined = typeof nameOrHeartbeatInterval === "string" ? nameOrHeartbeatInterval : undefined;
const userInfo: UserInfo = typeof userInfoOrIdentity !== "string"
? userInfoOrIdentity
: { identity: userInfoOrIdentity, name };

const shouldRemove: boolean = !userInfo ||
(!userInfo.identity && !userInfo.name);
const interval: number = typeof nameOrHeartbeatInterval === "number" ? nameOrHeartbeatInterval : heartbeatInterval;
const plugin = new HeartbeatPlugin(interval);

const shouldRemove: boolean = !userInfo || (!userInfo.identity && !userInfo.name);
if (shouldRemove) {
this.removePlugin(plugin)
delete this.defaultData[KnownEventDataKeys.UserInfo];
} else {
this.addPlugin(plugin)
this.defaultData[KnownEventDataKeys.UserInfo] = userInfo;
}

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

/**
Expand All @@ -472,15 +460,6 @@ export class Configuration {
return "exceptionless-js/2.0.0-dev";
}

/**
* Automatically send a heartbeat to keep the session alive.
*/
public useSessions(sendHeartbeats = true, heartbeatInterval = 30000): void {
if (sendHeartbeats) {
this.addPlugin(new HeartbeatPlugin(heartbeatInterval));
}
}

/**
* Use localStorage for persisting things like server configuration cache and persisted queue entries (depends on usePersistedQueueStorage).
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/plugins/default/HeartbeatPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class HeartbeatPlugin implements IEventPlugin {
}

public run(context: EventPluginContext): Promise<void> {
if (this._interval <= 0) {
return Promise.resolve();
}

clearInterval(this._intervalId);
this._intervalId = 0;

Expand Down

0 comments on commit 3c3671b

Please sign in to comment.