Skip to content

Commit 0e09e68

Browse files
committed
ref(core): Use EventTarget for client hooks
1 parent f78aa3f commit 0e09e68

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

packages/core/src/client.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ function setupWeightBasedFlushing<
149149
});
150150
}
151151

152+
class HookEvent extends Event {
153+
hookData: unknown[];
154+
constructor(hook: string, ...rest: unknown[]) {
155+
super(hook);
156+
this.hookData = rest;
157+
}
158+
}
159+
152160
/**
153161
* Base implementation for all JavaScript SDK clients.
154162
*
@@ -200,8 +208,7 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
200208
/** Holds flushable */
201209
private _outcomes: { [key: string]: number };
202210

203-
// eslint-disable-next-line @typescript-eslint/ban-types
204-
private _hooks: Record<string, Set<Function>>;
211+
private _hooksTarget: EventTarget;
205212

206213
private _promiseBuffer: PromiseBuffer<unknown>;
207214

@@ -215,7 +222,7 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
215222
this._integrations = {};
216223
this._numProcessing = 0;
217224
this._outcomes = {};
218-
this._hooks = {};
225+
this._hooksTarget = new EventTarget();
219226
this._eventProcessors = [];
220227
this._promiseBuffer = makePromiseBuffer(options.transportOptions?.bufferSize ?? DEFAULT_TRANSPORT_BUFFER_SIZE);
221228

@@ -840,23 +847,21 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
840847
* Register a hook on this client.
841848
*/
842849
public on(hook: string, callback: unknown): () => void {
843-
const hookCallbacks = (this._hooks[hook] = this._hooks[hook] || new Set());
844-
845850
// Wrap the callback in a function so that registering the same callback instance multiple
846851
// times results in the callback being called multiple times.
847852
// @ts-expect-error - The `callback` type is correct and must be a function due to the
848853
// individual, specific overloads of this function.
849854
// eslint-disable-next-line @typescript-eslint/ban-types
850-
const uniqueCallback: Function = (...args: unknown[]) => callback(...args);
855+
const uniqueCallback = (event: HookEvent) => callback(event.hookData);
851856

852-
hookCallbacks.add(uniqueCallback);
857+
this._hooksTarget.addEventListener(hook, uniqueCallback as EventListener);
853858

854859
// This function returns a callback execution handler that, when invoked,
855860
// deregisters a callback. This is crucial for managing instances where callbacks
856861
// need to be unregistered to prevent self-referencing in callback closures,
857862
// ensuring proper garbage collection.
858863
return () => {
859-
hookCallbacks.delete(uniqueCallback);
864+
this._hooksTarget.removeEventListener(hook, uniqueCallback as EventListener);
860865
};
861866
}
862867

@@ -1066,10 +1071,7 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
10661071
* Emit a hook that was previously registered via `on()`.
10671072
*/
10681073
public emit(hook: string, ...rest: unknown[]): void {
1069-
const callbacks = this._hooks[hook];
1070-
if (callbacks) {
1071-
callbacks.forEach(callback => callback(...rest));
1072-
}
1074+
this._hooksTarget.dispatchEvent(new HookEvent(hook, ...rest));
10731075
}
10741076

10751077
/**

0 commit comments

Comments
 (0)