import { IActionCreator } from './action'; import { IActionMapBasic } from './action-map'; import { ActionHandlerCallback, createHandler } from './action-handler'; import { FlowfulError } from './error'; const className = 'ActionHandlerBuilder'; export class ActionHandlerBuilder { private _name?: string; private _in?: readonly IActionCreator[]; private _out?: readonly IActionCreator[]; private _callback?: ActionHandlerCallback; private _inject?: Inject; constructor(name?: string) { this._name = name; } name(name: string): this { this._name = name; return this; } inputs(...inputs: readonly IActionCreator[]): 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 IActionCreator[]): 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): this { this._callback = callback; return this; } inject(inject: T): ActionHandlerBuilder { this._inject = inject as unknown as Inject; return this as unknown as ActionHandlerBuilder; } 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({ name: this._name, inputs: this._in, outputs: this._out, run: this._callback, inject: this._inject, }); } } export function buildHandler( name?: string ): ActionHandlerBuilder { const builder = new ActionHandlerBuilder(); if (name) { builder.name(name); } return builder; }