Skip to main content

namespace AsyncReducer

An AsyncReducer is a stand-alone asynchronous calculation that takes input values of type I, and, when requested, produces an output value of type O.

Companion type: AsyncReducer<I,O>

Interfaces

NameDescription
AsyncReducer.Impl<I,O,S>The AsyncReducer implementation interface defining the required methods.
AsyncReducer.Instance<I,O>An async reducer instance that manages its own state based on the reducer definition that was used to create this instance.

Classes

NameDescription
BaseA base class that can be used to easily create AsyncReducer instances.
InstanceImplThe default AsyncReducer.Impl implementation.
InvalidCombineShapeErrorundocumented
ReducerClosedErrorundocumented
ReducerHaltedErrorundocumented
ReducerNotInitializedErrorundocumented

Functions

combine

Returns an AsyncReducer that combines multiple input reducers according to the given "shape" by providing input values to all of them and collecting the outputs in the shape.

Definition

function combine<T, const S extends AsyncReducer.CombineShape<T>>(shape: S & AsyncReducer.CombineShape<T>): AsyncReducer<T, AsyncReducer.CombineResult<S>>;

Type parameters
NameDescription
Tthe input value type for all the reducers
Sthe desired result shape type

Parameters

NameTypeDescription
shapeS & AsyncReducer.CombineShape<T>a shape defining where reducer outputs will be located in the result. It can consist of a single reducer, an array of shapes, or an object with string keys and shapes as values.

containsSlice

Returns an AsyncReducer that returns true if the input values contain the given slice sequence amount times. Otherwise, returns false.

Definition

function containsSlice<T>(slice: AsyncStreamSource<T>, options?: {
    eq?: Eq<T> | undefined;
    amount?: number | undefined;
  }): AsyncReducer<T, boolean>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
sliceAsyncStreamSource<T>a async sequence of elements to match against
options{
    eq?: Eq<T> | undefined;
    amount?: number | undefined;
  }
(optional) an object containing the following properties:
- amount: (detaulf: 1) the amount of elements to find - eq: (default: Eq.objectIs) the Eq instance to use to compare elements

create

Returns an AsyncReducer with the given options:

Definition

function create<I, O = I, S = O>(init: (initHalt: () => void) => MaybePromise<S>, next: (current: S, next: I, index: number, halt: () => void) => MaybePromise<S>, stateToResult: (state: S, index: number, halted: boolean) => MaybePromise<O>, onClose?: (state: S, error?: unknown) => MaybePromise<void>): AsyncReducer<I, O>;

Type parameters
NameDescription
Ithe input value type
Othe output value type
Sthe internal state type

Parameters

NameTypeDescription
init(initHalt: () => void) => MaybePromise<S>the optionally lazy and/or promised initial state value
next(current: S, next: I, index: number, halt: () => void) => MaybePromise<S>returns (potentially asynchronously) the next state value based on the given inputs:
- current: the current state
- next: the current input value
- index: the input index value
- halt: function that, when called, ensures no more elements are passed to the reducer
stateToResult(state: S, index: number, halted: boolean) => MaybePromise<O>a potentially asynchronous function that converts the current state to an output value
onClose(state: S, error?: unknown) => MaybePromise<void>(optional) a function that will be called when the reducer will no longer receive values

createMono

Returns an AsyncReducer of which the input, state, and output types are the same.

Definition

function createMono<T>(init: (initHalt: () => void) => MaybePromise<T>, next: (current: T, next: T, index: number, halt: () => void) => MaybePromise<T>, stateToResult?: (state: T, index: number, halted: boolean) => MaybePromise<T>, onClose?: (state: T, error?: unknown) => MaybePromise<void>): AsyncReducer<T>;

Type parameters
NameDescription
Tthe overall value type

Parameters

NameTypeDescription
init(initHalt: () => void) => MaybePromise<T>the optionally lazy and/or promised initial state value
next(current: T, next: T, index: number, halt: () => void) => MaybePromise<T>returns (potentially asynchronously) the next state value based on the given inputs:
- current: the current state
- next: the current input value
- index: the input index value
- halt: function that, when called, ensures no more elements are passed to the reducer
stateToResult(state: T, index: number, halted: boolean) => MaybePromise<T>a potentially asynchronous function that converts the current state to an output value
onClose(state: T, error?: unknown) => MaybePromise<void>(optional) a function that will be called when the reducer will no longer receive values

createOutput

Returns an AsyncReducer of which the state and output types are the same.

Definition

function createOutput<I, O = I>(init: (initHalt: () => void) => MaybePromise<O>, next: (current: O, next: I, index: number, halt: () => void) => MaybePromise<O>, stateToResult?: (state: O, index: number, halted: boolean) => MaybePromise<O>, onClose?: (state: O, error?: unknown) => MaybePromise<void>): AsyncReducer<I, O>;

Type parameters
NameDescription
Ithe input value type
Othe output value type

Parameters

NameTypeDescription
init(initHalt: () => void) => MaybePromise<O>the optionally lazy and/or promised initial state value
next(current: O, next: I, index: number, halt: () => void) => MaybePromise<O>returns (potentially asynchronously) the next state value based on the given inputs:
- current: the current state
- next: the current input value
- index: the input index value
- halt: function that, when called, ensures no more elements are passed to the reducer
stateToResult(state: O, index: number, halted: boolean) => MaybePromise<O>a potentially asynchronous function that converts the current state to an output value
onClose(state: O, error?: unknown) => MaybePromise<void>(optional) a function that will be called when the reducer will no longer receive values

endsWithSlice

Returns an AsyncReducer that returns true if the last input values match the given slice values repeated amount times. Otherwise, returns false.

Definition

function endsWithSlice<T>(slice: AsyncStreamSource<T>, options?: {
    eq?: Eq<T> | undefined;
    amount?: number;
  }): AsyncReducer<T, boolean>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
sliceAsyncStreamSource<T>a async sequence of elements to match against
options{
    eq?: Eq<T> | undefined;
    amount?: number;
  }
(optional) an object containing the following properties:
- amount: (detaulf: 1) the amount of elements to find - eq: (default: Eq.objectIs) the Eq instance to use to compare elements

equals

Returns an AsyncReducer that ouputs true when the received elements match the given other async stream source according to the eq instance, false otherwise.

Definition

function equals<T>(other: AsyncStreamSource<T>, options?: {
    eq?: Eq<T> | undefined;
    negate?: boolean | undefined;
  }): AsyncReducer<T, boolean>;

Type parameters
NameDescription
Tthe element type

Parameters

NameTypeDescription
otherAsyncStreamSource<T>an async stream source containg elements to match against
options{
    eq?: Eq<T> | undefined;
    negate?: boolean | undefined;
  }
(optional) an object containing the following properties:
- eq: (default: Eq.objectIs) the Eq instance to use to compare elements - negate: (default: false) when true will invert the given predicate

every

Returns an AsyncReducer that ouputs true as long as all input values satisfy the given pred, false otherwise.

Definition

function every<T>(pred: (value: T, index: number) => MaybePromise<boolean>, options?: {
    negate?: boolean | undefined;
  }): AsyncReducer<T, boolean>;

Type parameters
NameDescription
Tthe element type

Parameters

NameTypeDescription
pred(value: T, index: number) => MaybePromise<boolean>a potentially async function taking an input value and its index, and returning true if the value satisfies the predicate
options{
    negate?: boolean | undefined;
  }
(optional) an object containing the following properties:
- negate: (default: false) when true will invert the given predicate

fold

Returns an AsyncReducer that uses the given init and next values to fold the input values into result values.

Definition

function fold<T, R>(init: AsyncOptLazy<R>, next: (current: R, value: T, index: number, halt: () => void) => MaybePromise<R>): AsyncReducer<T, R>;

Type parameters
NameDescription
Tthe input type
Rthe output type

Parameters

NameTypeDescription
initAsyncOptLazy<R>an (optionally lazy) initial result value
next(current: R, value: T, index: number, halt: () => void) => MaybePromise<R>a (potentially async) function taking the following arguments:
- current - the current result value
- value - the next input value
- index: the input index value
- halt: function that, when called, ensures no more elements are passed to the reducer

from

Returns an AsyncReducer from a given Reducer or AsyncReducer instance.

Definition

function from<I, O>(reducer: AsyncReducer.Accept<I, O>): AsyncReducer<I, O>;

Type parameters
NameDescription
Ithe input element type
Othe output element type

Parameters

NameTypeDescription
reducerAsyncReducer.Accept<I, O>the input reducer to convert

some

Returns an AsyncReducer that ouputs false as long as no input value satisfies given pred, true otherwise.

Definition

function some<T>(pred: (value: T, index: number) => MaybePromise<boolean>, options?: {
    negate?: boolean | undefined;
  }): AsyncReducer<T, boolean>;

Type parameters
NameDescription
Tthe element type

Parameters

NameTypeDescription
pred(value: T, index: number) => MaybePromise<boolean>a potentiall async function taking an input value and its index, and returning true if the value satisfies the predicate
options{
    negate?: boolean | undefined;
  }
(optional) an object containing the following properties:
- negate: (default: false) when true will invert the given predicate

startsWithSlice

Returns a AsyncReducer that returns true if the first input values match the given slice values repeated amount times. Otherwise, returns false.

Definition

function startsWithSlice<T>(slice: AsyncStreamSource<T>, options?: {
    eq?: Eq<T> | undefined;
    amount?: number;
  }): AsyncReducer<T, boolean>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
sliceAsyncStreamSource<T>a async sequence of elements to match against
options{
    eq?: Eq<T> | undefined;
    amount?: number;
  }
(optional) an object containing the following properties:
- amount: (detaulf: 1) the amount of elements to find - eq: (default: Eq.objectIs) the Eq instance to use to compare elements

Constants

NameDescription
firstReturns an AsyncReducer that remembers the first input value.
groupByReturns an AsyncReducer that uses the valueToKey function to calculate a key for each value, and feeds the tuple of the key and the value to the collector reducer. Finally, it returns the output of the collector. If no collector is given, the default collector will return a JS multimap of the type Map<K, V[]>.
isEmptyAn AsyncReducer that outputs true if no input values are received, false otherwise.
lastReturns an AsyncReducer that remembers the last input value.
maxReturns a Reducer that remembers the maximum value of the numberic inputs.
maxByReturns a Reducer that remembers the maximum value of the inputs using the given compFun to compare input values
minReturns a Reducer that remembers the minimum value of the numberic inputs.
minByReturns a Reducer that remembers the minimum value of the inputs using the given compFun to compare input values
nonEmptyAn AsyncReducer that outputs true if one or more input values are received, false otherwise.
partitionReturns an AsyncReducer that splits the incoming values into two separate outputs based on the given pred predicate. Values for which the predicate is true are fed into the collectorTrue reducer, and other values are fed into the collectorFalse instance. If no collectors are provided the values are collected into arrays.
pipeReturns an AsyncReducer instance that first applies this reducer, and then applies the given next reducer to each output produced by the previous reducer.
raceReturns an AsyncReducer that feeds incoming values to all reducers in the provided reducers source, and halts when the first reducer in the array is halted and returns the output of that reducer. Returns the otherwise value if no reducer is yet halted.
singleReturns an AsyncReducer that only produces an output value when having receives exactly one input value, otherwise will return the otherwise value or undefined.