Skip to content

Commit

Permalink
refactor(interceptors): simplify addHandler, split processXXX, add lo…
Browse files Browse the repository at this point in the history
…gger
  • Loading branch information
postspectacular committed Aug 19, 2019
1 parent 17f050d commit b7455ab
Showing 1 changed file with 54 additions and 27 deletions.
81 changes: 54 additions & 27 deletions packages/interceptors/src/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
Interceptor,
InterceptorContext,
InterceptorFn,
LOGGER,
SideEffect
} from "./api";

Expand Down Expand Up @@ -195,10 +196,10 @@ export class StatelessEventBus implements IDispatch {
this.dispatch([success, res])
).catch((e) => this.dispatch([err, e]));
} else {
console.warn("async effect did not return Promise");
LOGGER.warn("async effect did not return Promise");
}
} else {
console.warn(`skipping invalid async effect: ${id}`);
LOGGER.warn(`skipping invalid async effect: ${id}`);
}
},
-999
Expand All @@ -224,15 +225,11 @@ export class StatelessEventBus implements IDispatch {
}

addHandler(id: string, spec: EventDef) {
const iceps = isArray(spec)
? (<any>spec).map(asInterceptor)
: isFunction(spec)
? [{ pre: spec }]
: [spec];
const iceps = this.interceptorsFromSpec(spec);
if (iceps.length > 0) {
if (this.handlers[id]) {
this.removeHandler(id);
console.warn(`overriding handler for ID: ${id}`);
LOGGER.warn(`overriding handler for ID: ${id}`);
}
this.handlers[id] = iceps;
} else {
Expand All @@ -249,7 +246,7 @@ export class StatelessEventBus implements IDispatch {
addEffect(id: string, fx: SideEffect, priority = 1) {
if (this.effects[id]) {
this.removeEffect(id);
console.warn(`overriding effect for ID: ${id}`);
LOGGER.warn(`overriding effect for ID: ${id}`);
}
this.effects[id] = fx;
const p: EffectPriority = [id, priority];
Expand Down Expand Up @@ -425,22 +422,37 @@ export class StatelessEventBus implements IDispatch {
protected processEvent(ctx: InterceptorContext, e: Event) {
const iceps = this.handlers[<any>e[0]];
if (!iceps) {
console.warn(`missing handler for event type: ${e[0].toString()}`);
LOGGER.warn(`missing handler for event type: ${e[0].toString()}`);
return;
}
const n = iceps.length - 1;
if (!this.processForward(ctx, iceps, e)) {
return;
}
this.processReverse(ctx, iceps, e);
}

protected processForward(
ctx: InterceptorContext,
iceps: Interceptor[],
e: Event
) {
let hasPost = false;
for (let i = 0; i <= n && !ctx[FX_CANCEL]; i++) {
for (let i = 0, n = iceps.length; i < n && !ctx[FX_CANCEL]; i++) {
const icep = iceps[i];
if (icep.pre) {
this.mergeEffects(ctx, icep.pre(ctx[FX_STATE], e, this, ctx));
}
hasPost = hasPost || !!icep.post;
}
if (!hasPost) {
return;
}
for (let i = n; i >= 0 && !ctx[FX_CANCEL]; i--) {
return hasPost;
}

protected processReverse(
ctx: InterceptorContext,
iceps: Interceptor[],
e: Event
) {
for (let i = iceps.length; --i >= 0 && !ctx[FX_CANCEL]; ) {
const icep = iceps[i];
if (icep.post) {
this.mergeEffects(ctx, icep.post(ctx[FX_STATE], e, this, ctx));
Expand All @@ -459,16 +471,23 @@ export class StatelessEventBus implements IDispatch {
for (let p of this.priorities) {
const id = p[0];
const val = ctx[id];
if (val !== undefined) {
const fn = effects[id];
if (id !== FX_STATE) {
for (let v of val) {
fn(v, this, ctx);
}
} else {
fn(val, this, ctx);
}
val !== undefined && this.processEffect(ctx, effects, id, val);
}
}

protected processEffect(
ctx: InterceptorContext,
effects: IObjectOf<SideEffect>,
id: string,
val: any
) {
const fn = effects[id];
if (id !== FX_STATE) {
for (let v of val) {
fn(v, this, ctx);
}
} else {
fn(val, this, ctx);
}
}

Expand Down Expand Up @@ -543,6 +562,14 @@ export class StatelessEventBus implements IDispatch {
}
}
}

protected interceptorsFromSpec(spec: EventDef) {
return isArray(spec)
? (<any>spec).map(asInterceptor)
: isFunction(spec)
? [{ pre: spec }]
: [spec];
}
}

/**
Expand Down Expand Up @@ -726,7 +753,7 @@ const undoHandler = (action: string): InterceptorFn => (
bus,
ctx
) => {
let id = ev ? ev[0] : "history";
const id = ev ? ev[0] : "history";
if (implementsFunction(ctx[id], action)) {
const ok = ctx[id][action]();
return {
Expand All @@ -738,6 +765,6 @@ const undoHandler = (action: string): InterceptorFn => (
: undefined
};
} else {
console.warn("no history in context");
LOGGER.warn("no history in context");
}
};

0 comments on commit b7455ab

Please sign in to comment.