UNPKG

overmind

Version:
211 lines (209 loc) • 6.71 kB
import { IS_OPERATOR, getFunctionName } from "./utils.js"; import { action, createContext, createMutationOperator, createNextPath, createOperator, operatorStarted, operatorStopped } from "./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[IS_OPERATOR] ? operator : action(operator) : next; try { operatorToRun(operatorErr, operatorContext, run, final); } catch (operatorError) { operatorToRun(operatorErr, operatorContext, run, final); } }; run(null, context); } }; instance[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[IS_OPERATOR] ? 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[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) { operatorStopped(context, results); next(null, createContext(lastContext, results, lastContext.execution.path && lastContext.execution.path.slice(0, lastContext.execution.path.length - 1))); } }; operatorStarted("parallel", "", context); operators.forEach((operator, index) => { lastContext = createContext(lastContext || context, context.value, context.execution.path && context.execution.path.concat(String(index))); const nextWithPath = createNextPath(evaluate.bind(void 0, index)); (operator[IS_OPERATOR] ? operator : action(operator))(null, lastContext, nextWithPath); }); } }; instance[IS_OPERATOR] = true; return instance; } function noop() { return createOperator("noop", "", (err, context, value, next) => { if (err) next(err, value); else next(null, value); }); } function filter(operation) { return createOperator("filter", 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 createMutationOperator("catchError", 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) => { operatorStopped(context, context.value); next(err$1, createContext(catchContext, context.value)); }; const evaluateTry = (err$1, tryContext) => { if (err$1) { const newContext$1 = createContext(tryContext, err$1, context.execution.path && context.execution.path.concat("catch")); const nextWithPath$1 = createNextPath(evaluateCatch); (paths.try[IS_OPERATOR] ? paths.catch : action(paths.catch))(null, newContext$1, nextWithPath$1); } else { operatorStopped(context, context.value); next(null, createContext(tryContext, context.value)); } }; operatorStarted("tryCatch", "", context); const newContext = createContext(context, context.value, context.execution.path && context.execution.path.concat("try")); const nextWithPath = createNextPath(evaluateTry); (paths.try[IS_OPERATOR] ? paths.try : action(paths.try))(null, newContext, nextWithPath); } }; instance[IS_OPERATOR] = true; return instance; } function fork(key, paths) { return 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 createOperator("when", 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 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 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 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 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 export { branch, catchError, debounce, filter, fork, noop, parallel, pipe, throttle, tryCatch, wait, waitUntil, when }; //# sourceMappingURL=operators.js.map