UNPKG

flowed

Version:

A fast and reliable flow engine for orchestration and more uses in *Node.js*, *Deno* and the browser

65 lines 2.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TaskProcess = void 0; class TaskProcess { constructor(manager, id, task, taskResolverExecutor, context, automapParams, automapResults, flowId, debug, log) { this.manager = manager; this.id = id; this.task = task; this.taskResolverExecutor = taskResolverExecutor; this.context = context; this.automapParams = automapParams; this.automapResults = automapResults; this.flowId = flowId; this.debug = debug; this.log = log; this.pid = TaskProcess.nextPid; TaskProcess.nextPid = (TaskProcess.nextPid + 1) % Number.MAX_SAFE_INTEGER; } getParams() { return this.params; } run() { var _a; this.params = this.task.mapParamsForResolver(this.task.runStatus.solvedReqs.popAll(), this.automapParams, this.flowId, this.log); let resolverFn = this.taskResolverExecutor; let resolverThis = undefined; const isClassResolver = (_a = this.taskResolverExecutor.prototype) === null || _a === void 0 ? void 0 : _a.exec; if (isClassResolver) { const resolverInstance = new this.taskResolverExecutor(); resolverFn = resolverInstance.exec; resolverThis = resolverInstance; } return new Promise((resolve, reject) => { var _a; const onResolverSuccess = (resolverValue) => { this.task.runStatus.solvedResults = this.task.mapResultsFromResolver(resolverValue, this.automapResults, this.flowId, this.log); resolve(this.task.runStatus.solvedResults); }; const onResolverError = (error) => { reject(error); }; let resolverResult; try { resolverResult = resolverFn.call(resolverThis, this.params, this.context, this.task, this.debug, this.log); } catch (error) { onResolverError(error); } const resultIsObject = typeof resolverResult === 'object'; const resultIsPromise = ((_a = resolverResult === null || resolverResult === void 0 ? void 0 : resolverResult.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'Promise'; if (!resultIsObject) { throw new Error(`Expected resolver for task '${this.task.code}' to return an object or Promise that resolves to object. Returned value is of type '${typeof resolverResult}'.`); } if (resultIsPromise) { resolverResult.then(onResolverSuccess).catch(onResolverError); } else { onResolverSuccess(resolverResult); } }); } } exports.TaskProcess = TaskProcess; TaskProcess.nextPid = 1; //# sourceMappingURL=task-process.js.map