UNPKG

overmind

Version:
279 lines • 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.waitUntil = exports.throttle = exports.debounce = exports.wait = exports.when = exports.fork = exports.tryCatch = exports.catchError = exports.filter = exports.noop = exports.parallel = exports.branch = exports.pipe = void 0; const tslib_1 = require("tslib"); const operator_1 = require("./operator"); const utils = tslib_1.__importStar(require("./utils")); 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[utils.IS_OPERATOR] ? operator : (0, operator_1.action)(operator) : next; try { operatorToRun(operatorErr, operatorContext, run, final); } catch (operatorError) { operatorToRun(operatorErr, operatorContext, run, final); } }; run(null, context); } }; instance[utils.IS_OPERATOR] = true; return instance; } exports.pipe = pipe; 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[utils.IS_OPERATOR] ? operator : (0, operator_1.action)(operator) : (err, finalContext, finalNext, finalFinal) => { next(err, Object.assign(Object.assign({}, finalContext), { value: context.value }), finalNext, finalFinal); }; try { operatorToRun(operatorErr, operatorContext, run, final); } catch (operatorError) { operatorToRun(operatorErr, operatorContext, run, final); } }; run(null, context); } }; instance[utils.IS_OPERATOR] = true; return instance; } exports.branch = branch; 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, newContext) => { if (hasErrored) { return; } if (err) { hasErrored = true; return next(err, lastContext); } results[index] = newContext.value; evaluatingCount--; if (!evaluatingCount) { (0, operator_1.operatorStopped)(context, results); next(null, (0, operator_1.createContext)(lastContext, results, lastContext.execution.path && lastContext.execution.path.slice(0, lastContext.execution.path.length - 1))); } }; (0, operator_1.operatorStarted)('parallel', '', context); operators.forEach((operator, index) => { lastContext = (0, operator_1.createContext)(lastContext || context, context.value, context.execution.path && context.execution.path.concat(String(index))); const nextWithPath = (0, operator_1.createNextPath)(evaluate.bind(undefined, index)); const operatorToRun = operator[utils.IS_OPERATOR] ? operator : (0, operator_1.action)(operator); // @ts-ignore operatorToRun(null, lastContext, nextWithPath); }); } }; instance[utils.IS_OPERATOR] = true; return instance; } exports.parallel = parallel; function noop() { return (0, operator_1.createOperator)('noop', '', (err, context, value, next) => { if (err) next(err, value); else next(null, value); }); } exports.noop = noop; function filter(operation) { return (0, operator_1.createOperator)('filter', 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); }); } exports.filter = filter; function catchError(operation) { return (0, operator_1.createMutationOperator)('catchError', utils.getFunctionName(operation), (err, context, value, next) => { if (err) next(null, operation(context, err)); else next(null, value, { isSkipped: true, }); }); } exports.catchError = catchError; function tryCatch(paths) { const instance = (err, context, next) => { if (err) next(err, context); else { const evaluateCatch = (err, catchContext) => { (0, operator_1.operatorStopped)(context, context.value); next(err, (0, operator_1.createContext)(catchContext, context.value)); }; const evaluateTry = (err, tryContext) => { if (err) { const newContext = (0, operator_1.createContext)(tryContext, err, context.execution.path && context.execution.path.concat('catch')); const nextWithPath = (0, operator_1.createNextPath)(evaluateCatch); const operatorToRun = paths.try[utils.IS_OPERATOR] ? paths.catch : (0, operator_1.action)(paths.catch); // @ts-ignore operatorToRun(null, newContext, nextWithPath); } else { (0, operator_1.operatorStopped)(context, context.value); next(null, (0, operator_1.createContext)(tryContext, context.value)); } }; (0, operator_1.operatorStarted)('tryCatch', '', context); const newContext = (0, operator_1.createContext)(context, context.value, context.execution.path && context.execution.path.concat('try')); const nextWithPath = (0, operator_1.createNextPath)(evaluateTry); const operatorToRun = paths.try[utils.IS_OPERATOR] ? paths.try : (0, operator_1.action)(paths.try); // @ts-ignore operatorToRun(null, newContext, nextWithPath); } }; instance[utils.IS_OPERATOR] = true; return instance; } exports.tryCatch = tryCatch; function fork(key, paths) { return (0, operator_1.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]], }, }); } }); } exports.fork = fork; function when(operation, paths) { return (0, operator_1.createOperator)('when', 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, }, }); }); } exports.when = when; function wait(ms) { return (0, operator_1.createOperator)('wait', String(ms), (err, context, value, next) => { if (err) next(err, value); else setTimeout(() => next(null, value), ms); }); } exports.wait = wait; function debounce(ms) { let timeout; let previousFinal; return (0, operator_1.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); } }); } exports.debounce = debounce; function throttle(ms) { let timeout; let previousFinal; let currentNext; return (0, operator_1.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; } }); } exports.throttle = throttle; function waitUntil(operation) { return (0, operator_1.createOperator)('waitUntil', operation.name, (err, context, value, next) => { if (err) next(err, value); else { const tree = context.execution.getTrackStateTree(); const test = () => { if (operation(tree.state)) { tree.dispose(); next(null, value); } }; tree.trackScope(test, test); } }); } exports.waitUntil = waitUntil; //# sourceMappingURL=operators.js.map