Skip to content

Commit

Permalink
Prevent double wrapping event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan committed Aug 1, 2022
1 parent 2bae628 commit e763510
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ All notable changes to this project will be documented in this file.

### :bug: (Bug Fix)

* fix(context-async-hooks): Ensure listeners added using `once` can be removed using `removeListener`
[#3132](https://github.com/open-telemetry/opentelemetry-js/pull/3132)

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const ADD_LISTENER_METHODS = [
];

export abstract class AbstractAsyncHooksContextManager
implements ContextManager {
implements ContextManager {
abstract active(): Context;

abstract with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(
Expand Down Expand Up @@ -177,6 +177,10 @@ implements ContextManager {
) {
const contextManager = this;
return function (this: never, event: string, listener: Func<void>) {
// do not double wrap. e.g. once calls on
if (contextManager._wrapped) {
return original.call(this, event, listener);
}
let map = contextManager._getPatchMap(ee);
if (map === undefined) {
map = contextManager._createPatchMap(ee);
Expand All @@ -189,7 +193,11 @@ implements ContextManager {
const patchedListener = contextManager.bind(context, listener);
// store a weak reference of the user listener to ours
listeners.set(listener, patchedListener);
return original.call(this, event, patchedListener);

contextManager._wrapped = true;
const val = original.call(this, event, patchedListener);
contextManager._wrapped = false;
return val;
};
}

Expand All @@ -204,4 +212,5 @@ implements ContextManager {
}

private readonly _kOtListeners = Symbol('OtListeners');
private _wrapped = false;
}

0 comments on commit e763510

Please sign in to comment.