UNPKG

caminho

Version:

Tool for creating efficient data pipelines in a JavaScript environment

105 lines 5.32 kB
import { __assign, __awaiter, __generator } from "tslib"; import { from, lastValueFrom, tap } from 'rxjs'; import { wrapGenerator, wrapGeneratorWithBackPressure } from './operators/generator'; import { pipe } from './operators/pipe'; import { batch } from './operators/batch'; import { parallel } from './operators/parallel'; import { reduce } from './operators/reduce'; import { filter } from './operators/filter'; import { applyOperator, isBatch, } from './operators/helpers/operatorHelpers'; import { PendingDataControlInMemory } from './utils/PendingDataControl'; import { getOnStepFinished } from './utils/onStepFinished'; import { getOnStepStarted } from './utils/onStepStarted'; import { generateId } from './utils/generateId'; var Caminho = (function () { function Caminho(generatorParams, options) { this.options = options; this.operators = []; this.addOperatorApplier = this.addOperatorApplier.bind(this); this.getApplierForPipeOrBatch = this.getApplierForPipeOrBatch.bind(this); this.run = this.run.bind(this); if (options === null || options === void 0 ? void 0 : options.maxItemsFlowing) { this.pendingDataControl = new PendingDataControlInMemory(); } this.generator = this.getGenerator(generatorParams); } Caminho.prototype.getNumberOfItemsFlowing = function () { var _a; return (_a = this.pendingDataControl) === null || _a === void 0 ? void 0 : _a.size; }; Caminho.prototype.pipe = function (params) { var operatorApplier = this.getApplierForPipeOrBatch(params); this.addOperatorApplier(function () { return operatorApplier; }); return this; }; Caminho.prototype.parallel = function (params) { var operatorAppliers = params.map(this.getApplierForPipeOrBatch); var operatorApplier = parallel(params, operatorAppliers); this.addOperatorApplier(function () { return operatorApplier; }); return this; }; Caminho.prototype.filter = function (params) { var loggers = this.getLoggers(params); this.addOperatorApplier(filter(params.fn, loggers, this.pendingDataControl)); return this; }; Caminho.prototype.reduce = function (reduceParams) { var loggers = this.getLoggers(reduceParams); this.addOperatorApplier(reduce(reduceParams, loggers, this.pendingDataControl)); return this; }; Caminho.prototype.run = function (initialBag) { return __awaiter(this, void 0, void 0, function () { var runId, initial$, observable$, finalObservable$; var _this = this; var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: runId = generateId(); initial$ = from(this.generator(__assign({}, initialBag), runId)); observable$ = this.operators.reduce(function (acc, operator) { return applyOperator(acc, operator, runId); }, initial$); finalObservable$ = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.maxItemsFlowing) ? observable$.pipe(tap(function () { return _this.pendingDataControl.decrement(runId); })) : observable$; _c.label = 1; case 1: _c.trys.push([1, , 3, 4]); return [4, lastValueFrom(finalObservable$, { defaultValue: initialBag })]; case 2: return [2, _c.sent()]; case 3: (_b = this.pendingDataControl) === null || _b === void 0 ? void 0 : _b.destroyBucket(runId); return [7]; case 4: return [2]; } }); }); }; Caminho.prototype.getGenerator = function (generatorParams) { var _a; var loggers = this.getLoggers(generatorParams); if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.maxItemsFlowing) { var pendingDataControl = this.pendingDataControl; return wrapGeneratorWithBackPressure(generatorParams, this.options.maxItemsFlowing, pendingDataControl, loggers); } return wrapGenerator(generatorParams, loggers); }; Caminho.prototype.addOperatorApplier = function (operatorApplier) { this.operators.push(operatorApplier); }; Caminho.prototype.getApplierForPipeOrBatch = function (params) { return isBatch(params) ? batch(params, this.getLoggers(params)) : pipe(params, this.getLoggers(params)); }; Caminho.prototype.getLoggers = function (params) { var _a, _b, _c; var stepName = (_a = params.name) !== null && _a !== void 0 ? _a : params.fn.name; var onStepStarted = getOnStepStarted(stepName, (_b = this.options) === null || _b === void 0 ? void 0 : _b.onStepStarted); var onStepFinished = getOnStepFinished(stepName, (_c = this.options) === null || _c === void 0 ? void 0 : _c.onStepFinished); return { onStepFinished: onStepFinished, onStepStarted: onStepStarted }; }; return Caminho; }()); export { Caminho }; //# sourceMappingURL=Caminho.js.map