Skip to content

Commit

Permalink
fix: structured clone of transferred listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jun 7, 2024
1 parent f8876d8 commit 472ae80
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/lifecycleEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { Logger } from './logger/logger';
// Data of any type can be passed to the callback. Can be cast to any type that is given in emit().
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type callback = (data: any) => Promise<void>;
type ListenerMap = Map<string, callback>;
type UniqueListenerMap = Map<string, ListenerMap>;

declare const global: {
salesforceCoreLifecycle?: Lifecycle;
Expand Down Expand Up @@ -89,8 +91,11 @@ export class Lifecycle {
) {
const oldInstance = global.salesforceCoreLifecycle;
// use the newer version and transfer any listeners from the old version
// object spread keeps them from being references
global.salesforceCoreLifecycle = new Lifecycle({ ...oldInstance.listeners }, oldInstance.uniqueListeners);
// object spread and the clone fn keep them from being references
global.salesforceCoreLifecycle = new Lifecycle(
{ ...oldInstance.listeners },
cloneUniqueListeners(oldInstance.uniqueListeners)
);
// clean up any listeners on the old version
Object.keys(oldInstance.listeners).map((eventName) => {
oldInstance.removeAllListeners(eventName);
Expand Down Expand Up @@ -176,7 +181,7 @@ export class Lifecycle {
if (uniqueListenerIdentifier) {
if (!this.uniqueListeners.has(eventName)) {
// nobody is listening to the event yet
this.uniqueListeners.set(eventName, new Map<string, callback>([[uniqueListenerIdentifier, cb]]));
this.uniqueListeners.set(eventName, new Map([[uniqueListenerIdentifier, cb]]));
} else if (!this.uniqueListeners.get(eventName)?.has(uniqueListenerIdentifier)) {
// the unique listener identifier is not already registered
this.uniqueListeners.get(eventName)?.set(uniqueListenerIdentifier, cb);
Expand Down Expand Up @@ -231,3 +236,7 @@ export class Lifecycle {
}
}
}

const cloneListeners: (listeners: ListenerMap) => ListenerMap = (listeners) => new Map(Array.from(listeners.entries()));
const cloneUniqueListeners = (uniqueListeners: UniqueListenerMap): UniqueListenerMap =>
new Map(Array.from(uniqueListeners.entries()).map(([key, value]) => [key, cloneListeners(value)]));

2 comments on commit 472ae80

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: 472ae80 Previous: f7328a2 Ratio
Child logger creation 481148 ops/sec (±0.95%) 465974 ops/sec (±1.80%) 0.97
Logging a string on root logger 829596 ops/sec (±6.24%) 774476 ops/sec (±11.99%) 0.93
Logging an object on root logger 588018 ops/sec (±7.41%) 568889 ops/sec (±8.05%) 0.97
Logging an object with a message on root logger 6081 ops/sec (±211.48%) 7208 ops/sec (±204.90%) 1.19
Logging an object with a redacted prop on root logger 427055 ops/sec (±11.28%) 384994 ops/sec (±12.95%) 0.90
Logging a nested 3-level object on root logger 362855 ops/sec (±8.03%) 374888 ops/sec (±6.98%) 1.03

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: 472ae80 Previous: f7328a2 Ratio
Child logger creation 329736 ops/sec (±0.72%) 328651 ops/sec (±1.10%) 1.00
Logging a string on root logger 802178 ops/sec (±4.92%) 867117 ops/sec (±4.46%) 1.08
Logging an object on root logger 578979 ops/sec (±5.94%) 575272 ops/sec (±5.87%) 0.99
Logging an object with a message on root logger 5015 ops/sec (±207.58%) 3522 ops/sec (±220.49%) 0.70
Logging an object with a redacted prop on root logger 454361 ops/sec (±15.05%) 462524 ops/sec (±6.80%) 1.02
Logging a nested 3-level object on root logger 317849 ops/sec (±5.54%) 323712 ops/sec (±4.96%) 1.02

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.