import { ExecutionContext, IExecutionContextFacade, IFlowNodeInstanceService, IProcessModelFacade, IProcessTokenFacade, Model, NextFlowNodeInfo, Runtime, } from '@process-engine/process_engine_contracts'; import {FlowNodeHandler} from './index'; export class ScriptTaskHandler extends FlowNodeHandler { private _flowNodeInstanceService: IFlowNodeInstanceService = undefined; constructor(flowNodeInstanceService: IFlowNodeInstanceService) { super(); this._flowNodeInstanceService = flowNodeInstanceService; } private get flowNodeInstanceService(): IFlowNodeInstanceService { return this._flowNodeInstanceService; } protected async executeInternally(scriptTask: Model.Activities.ScriptTask, token: Runtime.Types.ProcessToken, processTokenFacade: IProcessTokenFacade, processModelFacade: IProcessModelFacade, executionContextFacade: IExecutionContextFacade): Promise { await this.flowNodeInstanceService.persistOnEnter(scriptTask.id, this.flowNodeInstanceId, token); const script: string = scriptTask.script; const context: ExecutionContext = executionContextFacade.getExecutionContext(); if (!script) { return undefined; } const tokenData: any = await processTokenFacade.getOldTokenFormat(); let result: any; try { const scriptFunction: Function = new Function('token', 'context', script); result = await scriptFunction.call(this, tokenData, context); result = result === undefined ? null : result; } catch (error) { await this.flowNodeInstanceService.persistOnError(scriptTask.id, this.flowNodeInstanceId, token, error); throw error; } const nextFlowNode: Model.Base.FlowNode = await processModelFacade.getNextFlowNodeFor(scriptTask); await processTokenFacade.addResultForFlowNode(scriptTask.id, result); token.payload = result; await this.flowNodeInstanceService.persistOnExit(scriptTask.id, this.flowNodeInstanceId, token); return new NextFlowNodeInfo(nextFlowNode, token, processTokenFacade); } }