flowed
Version:
A fast and reliable flow engine for orchestration and more uses in *Node.js*, *Deno* and the browser
103 lines • 4.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Task = void 0;
const value_queue_manager_1 = require("./value-queue-manager");
const ST = require('flowed-st');
class Task {
constructor(code, spec) {
this.code = code;
this.spec = spec;
this.parseSpec();
}
getResolverName() {
var _a;
return ((_a = this.spec.resolver) !== null && _a !== void 0 ? _a : { name: 'flowed::Noop' }).name;
}
getSerializableState() {
const result = JSON.parse(JSON.stringify(this.runStatus));
result.solvedReqs = this.runStatus.solvedReqs.toSerializable();
return result;
}
setSerializableState(runStatus) {
this.runStatus = JSON.parse(JSON.stringify(runStatus));
this.runStatus.solvedReqs = value_queue_manager_1.ValueQueueManager.fromSerializable(runStatus.solvedReqs);
}
resetRunStatus() {
var _a;
const reqs = [...((_a = this.spec.requires) !== null && _a !== void 0 ? _a : [])];
this.runStatus = {
solvedReqs: new value_queue_manager_1.ValueQueueManager(reqs),
solvedResults: {},
};
}
isReadyToRun() {
return this.runStatus.solvedReqs.allHaveContent();
}
getResults() {
return this.runStatus.solvedResults;
}
supplyReq(reqName, value) {
var _a;
const reqIndex = ((_a = this.spec.requires) !== null && _a !== void 0 ? _a : []).indexOf(reqName);
if (reqIndex === -1) {
throw new Error(`Requirement '${reqName}' for task '${this.code}' is not valid.`);
}
this.runStatus.solvedReqs.push(reqName, value);
}
supplyReqs(reqsMap) {
for (const [reqName, req] of Object.entries(reqsMap)) {
this.supplyReq(reqName, req);
}
}
mapParamsForResolver(solvedReqs, automap, flowId, log) {
var _a, _b, _c;
const params = {};
let resolverParams = (_b = (_a = this.spec.resolver) === null || _a === void 0 ? void 0 : _a.params) !== null && _b !== void 0 ? _b : {};
if (automap) {
const requires = (_c = this.spec.requires) !== null && _c !== void 0 ? _c : [];
const autoMappedParams = requires.map(req => ({ [req]: req })).reduce((accum, peer) => Object.assign(accum, peer), {});
log({ n: flowId, m: ` ⓘ Auto-mapped resolver params in task '${this.code}': %O`, mp: autoMappedParams, l: 'd' });
resolverParams = Object.assign(autoMappedParams, resolverParams);
}
let paramValue;
for (const [resolverParamName, paramSolvingInfo] of Object.entries(resolverParams)) {
if (typeof paramSolvingInfo === 'string') {
paramValue = solvedReqs[paramSolvingInfo];
}
else if (Object.prototype.hasOwnProperty.call(paramSolvingInfo, 'value')) {
paramValue = paramSolvingInfo.value;
}
else {
const template = paramSolvingInfo.transform;
paramValue = ST.select(solvedReqs).transformWith(template).root();
}
params[resolverParamName] = paramValue;
}
return params;
}
mapResultsFromResolver(solvedResults, automap, flowId, log) {
var _a, _b, _c;
if (typeof solvedResults !== 'object') {
throw new Error(`Expected resolver for task '${this.code}' to return an object or Promise that resolves to object. Returned value is of type '${typeof solvedResults}'.`);
}
const results = {};
let resolverResults = (_b = (_a = this.spec.resolver) === null || _a === void 0 ? void 0 : _a.results) !== null && _b !== void 0 ? _b : {};
if (automap) {
const provides = (_c = this.spec.provides) !== null && _c !== void 0 ? _c : [];
const autoMappedResults = provides.reduce((acc, prov) => Object.assign(acc, { [prov]: prov }), {});
log({ n: flowId, m: ` ⓘ Auto-mapped resolver results in task '${this.code}': %O`, mp: autoMappedResults, l: 'd' });
resolverResults = Object.assign(autoMappedResults, resolverResults);
}
for (const [resolverResultName, taskResultName] of Object.entries(resolverResults)) {
if (Object.prototype.hasOwnProperty.call(solvedResults, resolverResultName)) {
results[taskResultName] = solvedResults[resolverResultName];
}
}
return results;
}
parseSpec() {
this.resetRunStatus();
}
}
exports.Task = Task;
//# sourceMappingURL=task.js.map