UNPKG

@rarible/action

Version:

Action is almost like async function, but actions can be divided into steps. It gives more control over action execution.

68 lines (67 loc) 1.63 kB
import CallableInstance from "callable-instance"; export class ActionBuilder extends CallableInstance { constructor() { super("instanceMethod"); } instanceMethod(ctx, input) { return this.start(ctx, input).result; } start(ctx, input) { return this.build(ctx).start(input); } before(map) { return new BeforeActionBuilder(this, map); } after(map) { return new AfterActionBuilder(this, map); } thenActionBuilder(ab) { return new ThenActionBuilder(this, ab); } static create(build) { return new SimpleActionBuilder(build); } } class BeforeActionBuilder extends ActionBuilder { constructor(source, map) { super(); this.source = source; this.map = map; } build(ctx) { const action = this.source.build(ctx); return action.before(this.map); } } class AfterActionBuilder extends ActionBuilder { constructor(source, map) { super(); this.source = source; this.map = map; } build(ctx) { const action = this.source.build(ctx); return action.after(this.map); } } class ThenActionBuilder extends ActionBuilder { constructor(ab1, ab2) { super(); this.ab1 = ab1; this.ab2 = ab2; } build(ctx) { const a1 = this.ab1.build(ctx); const a2 = this.ab2.build(ctx); return a1.thenAction(a2); } } class SimpleActionBuilder extends ActionBuilder { constructor(f) { super(); this.f = f; } build(ctx) { return this.f(ctx); } }