Skip to main content
Module

x/rimbu/mod.ts>AsyncReducer.Impl

Rimbu is a TypeScript library focused on immutable, performant, and type-safe collections and other tools.
Latest
interface AsyncReducer.Impl
import { type AsyncReducer } from "https://deno.land/x/rimbu@1.2.1/mod.ts";
const { Impl } = AsyncReducer;

The AsyncReducer implementation interface defining the required methods.

Properties

readonly
init: (initHalt: () => void) => MaybePromise<S>

The initial state value for the reducer algorithm.

optional
onClose: ((state: S, error?: unknown) => MaybePromise<void>) | undefined

An optional function that is called when the reducer will no longer receive values.

mapInput: <I2>(mapFun: (value: I2, index: number) => MaybePromise<I>) => AsyncReducer<I2, O>

Returns an AsyncReducer instance that converts its input values using given mapFun before passing them to the reducer.

Methods

next(
state: S,
elem: I,
index: number,
halt: () => void,
): MaybePromise<S>

Returns the next state based on the given input values

stateToResult(
state: S,
index: number,
halted: boolean,
): MaybePromise<O>

Returns the output value based on the given state

filterInput<IF extends I>(pred: (
value: I,
index: number,
halt: () => void,
) => value is IF
, options?: { negate?: false | undefined; }
): AsyncReducer<IF, O>

Returns an AsyncReducer instance that only passes values to the reducer that satisy the given pred predicate.

filterInput<IF extends I>(pred: (
value: I,
index: number,
halt: () => void,
) => value is IF
, options: { negate: true; }
): AsyncReducer<Exclude<I, IF>, O>
filterInput(pred: (
value: I,
index: number,
halt: () => void,
) => MaybePromise<boolean>
, options?: { negate?: boolean | undefined; }
): AsyncReducer<I, O>
flatMapInput<I2>(flatMapFun: (value: I2, index: number) => MaybePromise<AsyncStreamSource<I>>): AsyncReducer<I2, O>

Returns an AsyncReducer instance that converts its input values using given flatMapFun before passing them to the reducer.

collectInput<I2>(collectFun: AsyncCollectFun<I2, I>): AsyncReducer<I2, O>

Returns an AsyncReducer instance that converts or filters its input values using given collectFun before passing them to the reducer.

mapOutput<O2>(mapFun: (
value: O,
index: number,
halted: boolean,
) => MaybePromise<O2>
): AsyncReducer<I, O2>

Returns an AsyncReducer instance that converts its output values using given mapFun.

takeInput(amount: number): AsyncReducer<I, O>

Returns an AsyncReducer instance that takes at most the given amount of input elements, and will ignore subsequent elements.

dropInput(amount: number): AsyncReducer<I, O>

Returns an AsyncReducer instance that skips the first given amount of input elements, and will process subsequent elements.

sliceInput(from?: number | undefined, amount?: number | undefined): AsyncReducer<I, O>

Returns an AsyncReducer instance that takes given amount of elements starting at given from index, and ignores other elements.

takeOutput(amount: number): AsyncReducer<I, O>

Returns an 'AsyncReducerinstance that produces at mostamount` values.

takeOutputUntil(pred: (value: O, index: number) => MaybePromise<boolean>, options?: { negate?: boolean | undefined; }): AsyncReducer<I, O>

Returns an 'AsyncReducerinstance that produces until the givenpred` predicate returns true for the output value.

chain<O2 extends O>(nextReducers: AsyncStreamSource<AsyncOptLazy<AsyncReducer.Accept<I, O2>, [O2]>>): AsyncReducer<I, O2>

Returns a reducer that applies the given nextReducers sequentially after this reducer has halted, and moving on to the next provided reducer until it is halted. Optionally, it provides the last output value of the previous reducer.

compile(): Promise<AsyncReducer.Instance<I, O>>

Returns a promise that resolves to a 'runnable' instance of the current reducer specification. This instance maintains its own state and indices, so that the instance only needs to be provided the input values, and output values can be retrieved when needed. The state is kept private.

Properties

readonly
init: (initHalt: () => void) => MaybePromise<S>

The initial state value for the reducer algorithm.

optional
onClose: ((state: S, error?: unknown) => MaybePromise<void>) | undefined

An optional function that is called when the reducer will no longer receive values.

mapInput: <I2>(mapFun: (value: I2, index: number) => MaybePromise<I>) => AsyncReducer<I2, O>

Returns an AsyncReducer instance that converts its input values using given mapFun before passing them to the reducer.

Methods

next(
state: S,
elem: I,
index: number,
halt: () => void,
): MaybePromise<S>

Returns the next state based on the given input values

stateToResult(
state: S,
index: number,
halted: boolean,
): MaybePromise<O>

Returns the output value based on the given state

filterInput<IF extends I>(pred: (
value: I,
index: number,
halt: () => void,
) => value is IF
, options?: { negate?: false | undefined; }
): AsyncReducer<IF, O>

Returns an AsyncReducer instance that only passes values to the reducer that satisy the given pred predicate.

filterInput<IF extends I>(pred: (
value: I,
index: number,
halt: () => void,
) => value is IF
, options: { negate: true; }
): AsyncReducer<Exclude<I, IF>, O>
filterInput(pred: (
value: I,
index: number,
halt: () => void,
) => MaybePromise<boolean>
, options?: { negate?: boolean | undefined; }
): AsyncReducer<I, O>
flatMapInput<I2>(flatMapFun: (value: I2, index: number) => MaybePromise<AsyncStreamSource<I>>): AsyncReducer<I2, O>

Returns an AsyncReducer instance that converts its input values using given flatMapFun before passing them to the reducer.

collectInput<I2>(collectFun: AsyncCollectFun<I2, I>): AsyncReducer<I2, O>

Returns an AsyncReducer instance that converts or filters its input values using given collectFun before passing them to the reducer.

mapOutput<O2>(mapFun: (
value: O,
index: number,
halted: boolean,
) => MaybePromise<O2>
): AsyncReducer<I, O2>

Returns an AsyncReducer instance that converts its output values using given mapFun.

takeInput(amount: number): AsyncReducer<I, O>

Returns an AsyncReducer instance that takes at most the given amount of input elements, and will ignore subsequent elements.

dropInput(amount: number): AsyncReducer<I, O>

Returns an AsyncReducer instance that skips the first given amount of input elements, and will process subsequent elements.

sliceInput(from?: number | undefined, amount?: number | undefined): AsyncReducer<I, O>

Returns an AsyncReducer instance that takes given amount of elements starting at given from index, and ignores other elements.

takeOutput(amount: number): AsyncReducer<I, O>

Returns an 'AsyncReducerinstance that produces at mostamount` values.

takeOutputUntil(pred: (value: O, index: number) => MaybePromise<boolean>, options?: { negate?: boolean | undefined; }): AsyncReducer<I, O>

Returns an 'AsyncReducerinstance that produces until the givenpred` predicate returns true for the output value.

chain<O2 extends O>(nextReducers: AsyncStreamSource<AsyncOptLazy<AsyncReducer.Accept<I, O2>, [O2]>>): AsyncReducer<I, O2>

Returns a reducer that applies the given nextReducers sequentially after this reducer has halted, and moving on to the next provided reducer until it is halted. Optionally, it provides the last output value of the previous reducer.

compile(): Promise<AsyncReducer.Instance<I, O>>

Returns a promise that resolves to a 'runnable' instance of the current reducer specification. This instance maintains its own state and indices, so that the instance only needs to be provided the input values, and output values can be retrieved when needed. The state is kept private.