UNPKG

overmind

Version:
223 lines (221 loc) • 7.46 kB
const require_utils = require('./utils.js'); const require_operator = require('./operator.js'); //#region src/operators.ts function pipe(...operators) { const instance = (err, context, next, final = next) => { if (err) next(err, context); else { let operatorIndex = 0; const run = (operatorErr, operatorContext) => { const operator = operators[operatorIndex++]; const operatorToRun = operator ? operator[require_utils.IS_OPERATOR] ? operator : require_operator.action(operator) : next; try { operatorToRun(operatorErr, operatorContext, run, final); } catch (operatorError) { operatorToRun(operatorErr, operatorContext, run, final); } }; run(null, context); } }; instance[require_utils.IS_OPERATOR] = true; return instance; } function branch(...operators) { const instance = (err, context, next, final = next) => { if (err) next(err, context); else { let operatorIndex = 0; const run = (operatorErr, operatorContext) => { const operator = operators[operatorIndex++]; const operatorToRun = operator ? operator[require_utils.IS_OPERATOR] ? operator : require_operator.action(operator) : (err$1, finalContext, finalNext, finalFinal) => { next(err$1, { ...finalContext, value: context.value }, finalNext, finalFinal); }; try { operatorToRun(operatorErr, operatorContext, run, final); } catch (operatorError) { operatorToRun(operatorErr, operatorContext, run, final); } }; run(null, context); } }; instance[require_utils.IS_OPERATOR] = true; return instance; } function parallel(...operators) { const instance = (err, context, next) => { if (err) next(err, context); else { let evaluatingCount = operators.length; let lastContext; let hasErrored = false; const results = []; const evaluate = (index, err$1, newContext) => { if (hasErrored) return; if (err$1) { hasErrored = true; return next(err$1, lastContext); } results[index] = newContext.value; evaluatingCount--; if (!evaluatingCount) { require_operator.operatorStopped(context, results); next(null, require_operator.createContext(lastContext, results, lastContext.execution.path && lastContext.execution.path.slice(0, lastContext.execution.path.length - 1))); } }; require_operator.operatorStarted("parallel", "", context); operators.forEach((operator, index) => { lastContext = require_operator.createContext(lastContext || context, context.value, context.execution.path && context.execution.path.concat(String(index))); const nextWithPath = require_operator.createNextPath(evaluate.bind(void 0, index)); (operator[require_utils.IS_OPERATOR] ? operator : require_operator.action(operator))(null, lastContext, nextWithPath); }); } }; instance[require_utils.IS_OPERATOR] = true; return instance; } function noop() { return require_operator.createOperator("noop", "", (err, context, value, next) => { if (err) next(err, value); else next(null, value); }); } function filter(operation) { return require_operator.createOperator("filter", require_utils.getFunctionName(operation), (err, context, value, next, final) => { if (err) next(err, value); else if (operation(context, value)) next(null, value); else final(null, value); }); } function catchError(operation) { return require_operator.createMutationOperator("catchError", require_utils.getFunctionName(operation), (err, context, value, next) => { if (err) next(null, operation(context, err)); else next(null, value, { isSkipped: true }); }); } function tryCatch(paths) { const instance = (err, context, next) => { if (err) next(err, context); else { const evaluateCatch = (err$1, catchContext) => { require_operator.operatorStopped(context, context.value); next(err$1, require_operator.createContext(catchContext, context.value)); }; const evaluateTry = (err$1, tryContext) => { if (err$1) { const newContext$1 = require_operator.createContext(tryContext, err$1, context.execution.path && context.execution.path.concat("catch")); const nextWithPath$1 = require_operator.createNextPath(evaluateCatch); (paths.try[require_utils.IS_OPERATOR] ? paths.catch : require_operator.action(paths.catch))(null, newContext$1, nextWithPath$1); } else { require_operator.operatorStopped(context, context.value); next(null, require_operator.createContext(tryContext, context.value)); } }; require_operator.operatorStarted("tryCatch", "", context); const newContext = require_operator.createContext(context, context.value, context.execution.path && context.execution.path.concat("try")); const nextWithPath = require_operator.createNextPath(evaluateTry); (paths.try[require_utils.IS_OPERATOR] ? paths.try : require_operator.action(paths.try))(null, newContext, nextWithPath); } }; instance[require_utils.IS_OPERATOR] = true; return instance; } function fork(key, paths) { return require_operator.createOperator("fork", String(key), (err, context, value, next) => { if (err) next(err, value); else next(null, value, { path: { name: String(key), operator: paths[value[key]] } }); }); } function when(operation, paths) { return require_operator.createOperator("when", require_utils.getFunctionName(operation), (err, context, value, next) => { if (err) next(err, value); else if (operation(context, value)) next(null, value, { path: { name: "true", operator: paths.true } }); else next(null, value, { path: { name: "false", operator: paths.false } }); }); } function wait(ms) { return require_operator.createOperator("wait", String(ms), (err, context, value, next) => { if (err) next(err, value); else setTimeout(() => next(null, value), ms); }); } function debounce(ms) { let timeout; let previousFinal; return require_operator.createOperator("debounce", String(ms), (err, context, value, next, final) => { if (err) next(err, value); else { if (timeout) { clearTimeout(timeout); previousFinal(null, value); } previousFinal = final; timeout = setTimeout(() => { timeout = null; next(null, value); }, ms); } }); } function throttle(ms) { let timeout; let previousFinal; let currentNext; return require_operator.createOperator("throttle", String(ms), (err, context, value, next, final) => { if (err) next(err, value); else { if (timeout) { previousFinal(null, value); currentNext = next; } else timeout = setTimeout(() => { timeout = null; currentNext(null, value); }, ms); previousFinal = final; currentNext = next; } }); } function waitUntil(operation) { return require_operator.createOperator("waitUntil", operation.name, (err, context, value, next) => { if (err) next(err, value); else { const tree = context.execution.getTrackStateTree(); let disposer; const test = () => { disposer?.(); if (operation(tree.state)) next(null, value); else disposer = tree.subscribe(test); }; tree.trackScope(test); } }); } //#endregion exports.branch = branch; exports.catchError = catchError; exports.debounce = debounce; exports.filter = filter; exports.fork = fork; exports.noop = noop; exports.parallel = parallel; exports.pipe = pipe; exports.throttle = throttle; exports.tryCatch = tryCatch; exports.wait = wait; exports.waitUntil = waitUntil; exports.when = when; //# sourceMappingURL=operators.js.map