Skip to content

Commit

Permalink
feat(interceptors): add event handler instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 17, 2018
1 parent 5107dca commit 1a6ac54
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/interceptors/src/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class StatelessEventBus implements

addHandler(id: string, spec: api.EventDef) {
const iceps = isArray(spec) ?
(<any>spec).map((i) => isFunction(i) ? { pre: i } : i) :
(<any>spec).map(asInterceptor) :
isFunction(spec) ? [{ pre: spec }] : [spec];
if (iceps.length > 0) {
if (this.handlers[id]) {
Expand Down Expand Up @@ -248,6 +248,25 @@ export class StatelessEventBus implements
}
}

/**
* Prepends given interceptors (or interceptor functions) to
* selected handlers. If no handler IDs are given, applies
* instrumentation to all currently registered handlers.
*
* @param inject
* @param ids
*/
instrumentWith(inject: (api.Interceptor | api.InterceptorFn)[], ids?: string[]) {
const iceps = inject.map(asInterceptor);
const handlers = this.handlers;
for (let id of ids || Object.keys(handlers)) {
const h = handlers[id];
if (h) {
handlers[id] = iceps.concat(h);
}
}
}

removeHandler(id: string) {
delete this.handlers[id];
}
Expand Down Expand Up @@ -645,3 +664,7 @@ export class EventBus extends StatelessEventBus implements
return false;
}
}

function asInterceptor(i: api.Interceptor | api.InterceptorFn) {
return isFunction(i) ? { pre: i } : i;
}

0 comments on commit 1a6ac54

Please sign in to comment.