caminho
Version:
Tool for creating efficient data pipelines in a JavaScript environment
93 lines • 4.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Caminho = void 0;
const rxjs_1 = require("rxjs");
const generator_1 = require("./operators/generator");
const pipe_1 = require("./operators/pipe");
const batch_1 = require("./operators/batch");
const parallel_1 = require("./operators/parallel");
const reduce_1 = require("./operators/reduce");
const filter_1 = require("./operators/filter");
const operatorHelpers_1 = require("./operators/helpers/operatorHelpers");
const PendingDataControl_1 = require("./utils/PendingDataControl");
const onStepFinished_1 = require("./utils/onStepFinished");
const onStepStarted_1 = require("./utils/onStepStarted");
const generateId_1 = require("./utils/generateId");
class Caminho {
options;
generator;
operators = [];
pendingDataControl;
constructor(generatorParams, options) {
this.options = options;
this.addOperatorApplier = this.addOperatorApplier.bind(this);
this.getApplierForPipeOrBatch = this.getApplierForPipeOrBatch.bind(this);
this.run = this.run.bind(this);
if (options?.maxItemsFlowing) {
this.pendingDataControl = new PendingDataControl_1.PendingDataControlInMemory();
}
this.generator = this.getGenerator(generatorParams);
}
getNumberOfItemsFlowing() {
return this.pendingDataControl?.size;
}
pipe(params) {
const operatorApplier = this.getApplierForPipeOrBatch(params);
this.addOperatorApplier(() => operatorApplier);
return this;
}
parallel(params) {
const operatorAppliers = params.map(this.getApplierForPipeOrBatch);
const operatorApplier = (0, parallel_1.parallel)(params, operatorAppliers);
this.addOperatorApplier(() => operatorApplier);
return this;
}
filter(params) {
const loggers = this.getLoggers(params);
this.addOperatorApplier((0, filter_1.filter)(params.fn, loggers, this.pendingDataControl));
return this;
}
reduce(reduceParams) {
const loggers = this.getLoggers(reduceParams);
this.addOperatorApplier((0, reduce_1.reduce)(reduceParams, loggers, this.pendingDataControl));
return this;
}
async run(initialBag) {
const runId = (0, generateId_1.generateId)();
const initial$ = (0, rxjs_1.from)(this.generator({ ...initialBag }, runId));
const observable$ = this.operators.reduce((acc, operator) => (0, operatorHelpers_1.applyOperator)(acc, operator, runId), initial$);
const finalObservable$ = this.options?.maxItemsFlowing
? observable$.pipe((0, rxjs_1.tap)(() => this.pendingDataControl.decrement(runId)))
: observable$;
try {
return await (0, rxjs_1.lastValueFrom)(finalObservable$, { defaultValue: initialBag });
}
finally {
this.pendingDataControl?.destroyBucket(runId);
}
}
getGenerator(generatorParams) {
const loggers = this.getLoggers(generatorParams);
if (this.options?.maxItemsFlowing) {
const pendingDataControl = this.pendingDataControl;
return (0, generator_1.wrapGeneratorWithBackPressure)(generatorParams, this.options.maxItemsFlowing, pendingDataControl, loggers);
}
return (0, generator_1.wrapGenerator)(generatorParams, loggers);
}
addOperatorApplier(operatorApplier) {
this.operators.push(operatorApplier);
}
getApplierForPipeOrBatch(params) {
return (0, operatorHelpers_1.isBatch)(params)
? (0, batch_1.batch)(params, this.getLoggers(params))
: (0, pipe_1.pipe)(params, this.getLoggers(params));
}
getLoggers(params) {
const stepName = params.name ?? params.fn.name;
const onStepStarted = (0, onStepStarted_1.getOnStepStarted)(stepName, this.options?.onStepStarted);
const onStepFinished = (0, onStepFinished_1.getOnStepFinished)(stepName, this.options?.onStepFinished);
return { onStepFinished, onStepStarted };
}
}
exports.Caminho = Caminho;
//# sourceMappingURL=Caminho.js.map