UNPKG

caminho

Version:

Tool for creating efficient data pipelines in a JavaScript environment

39 lines 1.49 kB
import { bufferTime, filter, mergeAll, mergeMap } from 'rxjs'; import { getNewValueBag } from '../utils/valueBag'; export function batch(params, loggers) { const getBag = params.provides ? valueBagGetterBatchProvides(params.provides) : valueBagGetterBatchNoProvides(); async function wrappedStep(valueBag) { loggers.onStepStarted(valueBag); const startTime = new Date(); try { const values = await params.fn([...valueBag]); const newValueBags = getBag(valueBag, values); loggers.onStepFinished(newValueBags, startTime); return newValueBags; } catch (err) { loggers.onStepFinished(valueBag, startTime, err); throw err; } } return function operatorApplier(observable) { return observable .pipe(bufferTime(params.batch.timeoutMs, undefined, params.batch.maxSize)) .pipe(filter((buffer) => buffer.length > 0)) .pipe(mergeMap(wrappedStep, params.maxConcurrency)) .pipe(mergeAll()); }; } export function valueBagGetterBatchNoProvides() { return function getValueBag(valueBag) { return valueBag; }; } export function valueBagGetterBatchProvides(provides) { return function getValueBagWithProvides(valueBags, values) { return valueBags.map((valueBag, index) => getNewValueBag(valueBag, provides, values[index])); }; } //# sourceMappingURL=batch.js.map