UNPKG

@rs-box/ez-flow

Version:
74 lines (73 loc) 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConditionalFlow = void 0; const lib_util_1 = require("../utils/lib-util"); const no_op_work_1 = require("../work/no-op-work"); const work_report_predicate_1 = require("../work/work-report-predicate"); const abstract_work_flow_1 = require("./abstract-work-flow"); class ConditionalFlow extends abstract_work_flow_1.AbstractWorkFlow { constructor(name, toExecute, nextOnTrue, nextOnFalse, predicate) { super(name); this.toExecute = toExecute; this.nextOnTrue = nextOnTrue; this.nextOnFalse = nextOnFalse; this.predicate = predicate; } async call(workContext) { let returnReport; returnReport = await this.toExecute.call(workContext); if (!this.predicate) { this.predicate = new work_report_predicate_1.WorkReportPredicate(); } const predicateVal = await this.predicate.apply(returnReport); if (predicateVal) { returnReport = await this.nextOnTrue.call(workContext); } else { if (!(this.nextOnFalse instanceof no_op_work_1.NoOpWork)) { returnReport = await this.nextOnFalse.call(workContext); } } return returnReport; } } exports.ConditionalFlow = ConditionalFlow; ConditionalFlow.Builder = class { constructor() { this.name = lib_util_1.LibUtil.getUUID(); this.toExecute = new no_op_work_1.NoOpWork(); this.nextOnTrue = new no_op_work_1.NoOpWork(); this.nextOnFalse = new no_op_work_1.NoOpWork(); } static newFlow() { return new ConditionalFlow.Builder(); } withName(name) { this.name = name; return this; } withWork(work) { this.toExecute = work; return this; } then(work) { this.nextOnTrue = work; return this; } otherwise(work) { this.nextOnFalse = work; return this; } when(predicate) { this.predicate = predicate; return this; } build() { if (this.predicate) { return new ConditionalFlow(this.name, this.toExecute, this.nextOnTrue, this.nextOnFalse, this.predicate); } else { return new ConditionalFlow(this.name, this.toExecute, this.nextOnTrue, this.nextOnFalse); } } };