import { NFError } from "../error"; import { NFEventBus } from "../eventbus"; import { INFWaitState, NFNodeCfg, NFNodeType, NFWaitKind } from "../types"; import { NFNode } from "./node"; /** * 等待节点:定时 / 消息 / 信号。 * * 到达后挂起,由流程实例的 `message`、`signal` 或 `resumeWaits` 唤醒后续顺序流。 */ export class NFWaitNode extends NFNode{ waitKind:string; delay?:number; dueTime?:number; messageName?:string; signalName?:string; /** * 创建等待节点。 * * @param cfg - 需含 `waitKind`;消息 / 信号还须 `messageName` / `signalName` * @param process - 所属流程 * @throws `1101` 当消息名或信号名缺失 */ constructor(cfg:NFNodeCfg,process){ super(cfg,process); this.type = NFNodeType.WAIT; this.waitKind = (cfg.waitKind || NFWaitKind.TIMER) as string; this.delay = cfg.delay; this.dueTime = cfg.dueTime; this.messageName = cfg.messageName; this.signalName = cfg.signalName; if(this.waitKind === NFWaitKind.MESSAGE && !this.messageName){ throw new NFError("1101",[this.name || this.id]); } if(this.waitKind === NFWaitKind.SIGNAL && !this.signalName){ throw new NFError("1101",[this.name || this.id]); } } /** * 登记等待态后挂起,不推进出口。 */ async run(){ await super.run(); const state:INFWaitState = { kind: this.waitKind, nodeId: this.id }; if(this.waitKind === NFWaitKind.TIMER){ state.dueTime = this.dueTime != null ? this.dueTime : (Date.now() + (this.delay || 0)); }else if(this.waitKind === NFWaitKind.MESSAGE){ state.messageName = this.messageName; }else if(this.waitKind === NFWaitKind.SIGNAL){ state.signalName = this.signalName; } await this.process.addWait(state); await NFEventBus.emit('node.leave',{ process:this.process, node:this, waiting:true }); // 挂起,不推进出口 } /** * 被唤醒后移除等待态并继续出口顺序流。 */ async resume(){ await this.process.removeWait(this.id); const seq = this.process.nodeManager.getSequenceNode(this.id); if(seq){ await seq.run(); } } }