@axah/koa
Advanced tools
Comparing version 5.7.0 to 5.7.1
@@ -14,3 +14,3 @@ "use strict"; | ||
const original = emitter[method]; | ||
const wrapped = wrapper(original, method); | ||
const wrapped = wrapper(original); | ||
wrapped[isWrappedSymbol] = true; | ||
@@ -21,19 +21,50 @@ // eslint-disable-next-line no-param-reassign | ||
/** | ||
* Wraps EventEmitter listener registration methods of the | ||
* given emitter, so that all listeners are run in scope of | ||
* the provided async resource. | ||
* Wraps EventEmitter listener registration methods of the given emitter, | ||
* so that all listeners are run in scope of the provided async resource. | ||
* | ||
* Supports registering same listener function to multiple events (or | ||
* even the same one), as well as subsequent deregistering. | ||
*/ | ||
function wrapEmitter(emitter, asyncResource) { | ||
for (const method of addMethods) { | ||
wrapEmitterMethod(emitter, method, (original) => function wrap(name, handler) { | ||
// eslint-disable-next-line no-param-reassign | ||
handler[wrappedSymbol] = asyncResource.runInAsyncScope.bind(asyncResource, handler, emitter); | ||
wrapEmitterMethod(emitter, method, (original) => function wrap(event, handler) { | ||
let wrapped = emitter[wrappedSymbol]; | ||
if (wrapped === undefined) { | ||
wrapped = {}; | ||
// eslint-disable-next-line no-param-reassign | ||
emitter[wrappedSymbol] = wrapped; | ||
} | ||
const wrappedHandler = asyncResource.runInAsyncScope.bind(asyncResource, handler, emitter); | ||
const existing = wrapped[event]; | ||
if (existing === undefined) { | ||
wrapped[event] = wrappedHandler; | ||
} | ||
else if (typeof existing === 'function') { | ||
wrapped[event] = [existing, wrappedHandler]; | ||
} | ||
else { | ||
wrapped[event].push(wrappedHandler); | ||
} | ||
// @ts-ignore | ||
return original.call(this, name, handler[wrappedSymbol]); | ||
return original.call(this, event, wrappedHandler); | ||
}); | ||
} | ||
for (const method of removeMethods) { | ||
wrapEmitterMethod(emitter, method, (original) => function wrap(name, handler) { | ||
wrapEmitterMethod(emitter, method, (original) => function wrap(event, handler) { | ||
let wrappedHandler; | ||
const wrapped = emitter[wrappedSymbol]; | ||
if (wrapped !== undefined) { | ||
const existing = wrapped[event]; | ||
if (existing !== undefined) { | ||
if (typeof existing === 'function') { | ||
wrappedHandler = existing; | ||
delete wrapped[event]; | ||
} | ||
else { | ||
wrappedHandler = existing.pop(); | ||
} | ||
} | ||
} | ||
// @ts-ignore | ||
return original.call(this, name, handler[wrappedSymbol] || handler); | ||
return original.call(this, event, wrappedHandler || handler); | ||
}); | ||
@@ -40,0 +71,0 @@ } |
{ | ||
"name": "@axah/koa", | ||
"version": "5.7.0", | ||
"version": "5.7.1", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "license": "UNLICENSED", |
Sorry, the diff of this file is not supported yet
46061
700