From 472ae8065f3dc34f6b46ca97fe25a04584611fd5 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Fri, 7 Jun 2024 09:04:00 -0500 Subject: [PATCH] fix: structured clone of transferred listeners --- src/lifecycleEvents.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/lifecycleEvents.ts b/src/lifecycleEvents.ts index 64f5a82176..e713c29f8a 100644 --- a/src/lifecycleEvents.ts +++ b/src/lifecycleEvents.ts @@ -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; +type ListenerMap = Map; +type UniqueListenerMap = Map; declare const global: { salesforceCoreLifecycle?: Lifecycle; @@ -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); @@ -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([[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); @@ -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)]));