All files action-handler-builder.ts

100% Statements 71/71
100% Branches 20/20
100% Functions 9/9
100% Lines 71/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87    1x 1x   1x   1x 13x 13x 13x 13x 13x   13x 13x 13x   13x 11x 11x 11x   13x 14x 3x 3x 3x 3x 14x 14x 14x   13x 13x   13x 8x 3x 3x 3x 3x 8x 8x 8x   13x 13x   13x 12x 12x 12x   13x 1x 1x 1x   13x 12x 2x 2x 12x 1x 1x   9x 9x 9x 9x 9x 9x 9x 12x 13x   1x 13x 13x 13x 13x 8x 8x 13x 13x  
import { ActionCreator } from './action';
import { ActionMap, ActionMapBasic } from './action-map';
import { ActionHandlerCallback, createHandler } from './action-handler';
import { FlowfulError } from './error';
 
const className = 'ActionHandlerBuilder';
 
export class ActionHandlerBuilder<AM extends ActionMapBasic = ActionMapBasic, Inject = any> {
  private _name?: string;
  private _in?: readonly ActionCreator[];
  private _out?: readonly ActionCreator[];
  private _callback?: ActionHandlerCallback<AM, Inject>;
  private _inject?: Inject;
 
  constructor(name?: string) {
    this._name = name;
  }
 
  name(name: string): this {
    this._name = name;
    return this;
  }
 
  inputs(...inputs: readonly ActionCreator[]): this {
    if (!inputs.length) {
      throw new FlowfulError(
        `[Flowful][${className}][inputs/ins/in] At least one input action creator must be specified.`
      );
    }
    this._in = [...(this._in || []), ...inputs];
    return this;
  }
 
  in = this.inputs;
  ins = this.inputs;
 
  outputs(...outputs: readonly ActionCreator[]): this {
    if (!outputs.length) {
      throw new FlowfulError(
        `[Flowful][${className}][outputs/outs/out] At least one output action creator must be specified.`
      );
    }
    this._out = [...(this._out || []), ...outputs];
    return this;
  }
 
  out = this.outputs;
  outs = this.outputs;
 
  run(callback: ActionHandlerCallback<AM, Inject>): this {
    this._callback = callback;
    return this;
  }
 
  inject<T>(inject: T): ActionHandlerBuilder<AM, T> {
    this._inject = inject as unknown as Inject;
    return this as unknown as ActionHandlerBuilder<AM, T>;
  }
 
  create() {
    if (!this._in) {
      throw new FlowfulError(`[Flowful][${className}][create] Inputs must be specified for ActionHandler.`);
    }
    if (!this._callback) {
      throw new FlowfulError(`[Flowful][${className}][create] Run callback must be specified for ActionHandler.`);
    }
 
    return createHandler<AM, Inject>({
      name: this._name,
      inputs: this._in,
      outputs: this._out,
      run: this._callback,
      inject: this._inject,
    });
  }
}
 
export function buildHandler<AM extends ActionMap = ActionMap, Inject = any>(
  name?: string
): ActionHandlerBuilder<AM, Inject> {
  const builder = new ActionHandlerBuilder<AM, Inject>();
  if (name) {
    builder.name(name);
  }
  return builder;
}