File tree Expand file tree Collapse file tree 2 files changed +26
-4
lines changed
packages/opentelemetry-context-async-hooks Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -146,10 +146,14 @@ export abstract class AbstractAsyncHooksContextManager
146146 const contextManager = this ;
147147 return function ( this : never , event : string ) {
148148 const map = contextManager . _getPatchMap ( ee ) ;
149- if ( map ?. [ event ] !== undefined ) {
150- delete map [ event ] ;
149+ if ( map !== undefined ) {
150+ if ( arguments . length === 0 ) {
151+ contextManager . _createPatchMap ( ee ) ;
152+ } else if ( map [ event ] !== undefined ) {
153+ delete map [ event ] ;
154+ }
151155 }
152- return original . call ( this , event ) ;
156+ return original . apply ( this , arguments ) ;
153157 } ;
154158 }
155159
@@ -184,7 +188,7 @@ export abstract class AbstractAsyncHooksContextManager
184188 }
185189
186190 private _createPatchMap ( ee : EventEmitter ) : PatchMap {
187- const map = { } ;
191+ const map = Object . create ( null ) ;
188192 // eslint-disable-next-line @typescript-eslint/no-explicit-any
189193 ( ee as any ) [ this . _kOtListeners ] = map ;
190194 return map ;
Original file line number Diff line number Diff line change @@ -415,6 +415,24 @@ for (const contextManagerClass of [
415415 patchedEE . emit ( 'test' ) ;
416416 } ) ;
417417
418+ it ( 'should return current context and removeAllListeners (when enabled)' , done => {
419+ const ee = new EventEmitter ( ) ;
420+ const context = ROOT_CONTEXT . setValue ( key1 , 1 ) ;
421+ const patchedEE = contextManager . bind ( ee , context ) ;
422+ const handler = ( ) => {
423+ assert . deepStrictEqual ( contextManager . active ( ) , context ) ;
424+ patchedEE . removeAllListeners ( ) ;
425+ assert . strictEqual ( patchedEE . listeners ( 'test' ) . length , 0 ) ;
426+ assert . strictEqual ( patchedEE . listeners ( 'test1' ) . length , 0 ) ;
427+ return done ( ) ;
428+ } ;
429+ patchedEE . on ( 'test' , handler ) ;
430+ patchedEE . on ( 'test1' , handler ) ;
431+ assert . strictEqual ( patchedEE . listeners ( 'test' ) . length , 1 ) ;
432+ assert . strictEqual ( patchedEE . listeners ( 'test1' ) . length , 1 ) ;
433+ patchedEE . emit ( 'test' ) ;
434+ } ) ;
435+
418436 /**
419437 * Even if asynchooks is disabled, the context propagation will
420438 * still works but it might be lost after any async op.
You can’t perform that action at this time.
0 commit comments