@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
54 lines (53 loc) • 1.97 kB
TypeScript
import { AbortableAsyncMapper, AsyncPredicate, CommonLogger, ErrorMode } from '@naturalcycles/js-lib';
import { TransformTyped } from '../stream.model';
export interface TransformMapOptions<IN = any, OUT = IN> {
/**
* Set true to support "multiMap" - possibility to return [] and emit 1 result for each item in the array.
*
* @default false
*/
flattenArrayOutput?: boolean;
/**
* Predicate to filter outgoing results (after mapper).
* Allows to not emit all results.
*
* Defaults to "pass everything" (including null, undefined, etc).
* Simpler way to exclude certain cases is to return SKIP symbol from the mapper.
*/
predicate?: AsyncPredicate<OUT>;
/**
* Number of concurrently pending promises returned by `mapper`.
*
* @default 16 (to match default highWatermark option for objectMode streams)
*/
concurrency?: number;
/**
* @default THROW_IMMEDIATELY
*/
errorMode?: ErrorMode;
/**
* If defined - will be called on every error happening in the stream.
* Called BEFORE observable will emit error (unless skipErrors is set to true).
*/
onError?: (err: Error, input: IN) => any;
/**
* Progress metric
*
* @default `stream`
*/
metric?: string;
logger?: CommonLogger;
}
/**
* Like pMap, but for streams.
* Inspired by `through2`.
* Main feature is concurrency control (implemented via `through2-concurrent`) and convenient options.
* Using this allows native stream .pipe() to work and use backpressure.
*
* Only works in objectMode (due to through2Concurrent).
*
* Concurrency defaults to 16.
*
* If an Array is returned by `mapper` - it will be flattened and multiple results will be emitted from it. Tested by Array.isArray().
*/
export declare function transformMap<IN = any, OUT = IN>(mapper: AbortableAsyncMapper<IN, OUT>, opt?: TransformMapOptions<IN, OUT>): TransformTyped<IN, OUT>;