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
| Name | Description |
|---|---|
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
| Name | Description |
|---|---|
Base | A base class that can be used to easily create AsyncReducer instances. |
InstanceImpl | The default AsyncReducer.Impl implementation. |
InvalidCombineShapeError | undocumented |
ReducerClosedError | undocumented |
ReducerHaltedError | undocumented |
ReducerNotInitializedError | undocumented |
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.
combineAsyncReducer 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
| Name | Description |
|---|---|
| T | the input value type for all the reducers |
| S | the desired result shape type |
Parameters
| Name | Type | Description |
|---|---|---|
shape | S & 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.
containsSliceAsyncReducer 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
| Name | Description |
|---|---|
| T |
Parameters
| Name | Type | Description |
|---|---|---|
slice | AsyncStreamSource<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:
createAsyncReducer 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
| Name | Description |
|---|---|
| I | the input value type |
| O | the output value type |
| S | the internal state type |
Parameters
| Name | Type | Description |
|---|---|---|
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.
createMonoAsyncReducer 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
| Name | Description |
|---|---|
| T | the overall value type |
Parameters
| Name | Type | Description |
|---|---|---|
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.
createOutputAsyncReducer 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
| Name | Description |
|---|---|
| I | the input value type |
| O | the output value type |
Parameters
| Name | Type | Description |
|---|---|---|
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.
endsWithSliceAsyncReducer 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
| Name | Description |
|---|---|
| T |
Parameters
| Name | Type | Description |
|---|---|---|
slice | AsyncStreamSource<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.
equalsAsyncReducer 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
| Name | Description |
|---|---|
| T | the element type |
Parameters
| Name | Type | Description |
|---|---|---|
other | AsyncStreamSource<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.
everyAsyncReducer 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
| Name | Description |
|---|---|
| T | the element type |
Parameters
| Name | Type | Description |
|---|---|---|
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.
foldAsyncReducer 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
| Name | Description |
|---|---|
| T | the input type |
| R | the output type |
Parameters
| Name | Type | Description |
|---|---|---|
init | AsyncOptLazy<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.
fromAsyncReducer from a given Reducer or AsyncReducer instance.Definition
function from<I, O>(reducer: AsyncReducer.Accept<I, O>): AsyncReducer<I, O>;
Type parameters
| Name | Description |
|---|---|
| I | the input element type |
| O | the output element type |
Parameters
| Name | Type | Description |
|---|---|---|
reducer | AsyncReducer.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.
someAsyncReducer 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
| Name | Description |
|---|---|
| T | the element type |
Parameters
| Name | Type | Description |
|---|---|---|
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.
startsWithSliceAsyncReducer 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
| Name | Description |
|---|---|
| T |
Parameters
| Name | Type | Description |
|---|---|---|
slice | AsyncStreamSource<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
| Name | Description |
|---|---|
| first | Returns an AsyncReducer that remembers the first input value. |
| groupBy | Returns 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[]>. |
| isEmpty | An AsyncReducer that outputs true if no input values are received, false otherwise. |
| last | Returns an AsyncReducer that remembers the last input value. |
| max | Returns a Reducer that remembers the maximum value of the numberic inputs. |
| maxBy | Returns a Reducer that remembers the maximum value of the inputs using the given compFun to compare input values |
| min | Returns a Reducer that remembers the minimum value of the numberic inputs. |
| minBy | Returns a Reducer that remembers the minimum value of the inputs using the given compFun to compare input values |
| nonEmpty | An AsyncReducer that outputs true if one or more input values are received, false otherwise. |
| partition | Returns 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. |
| pipe | Returns an AsyncReducer instance that first applies this reducer, and then applies the given next reducer to each output produced by the previous reducer. |
| race | Returns 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. |
| single | Returns an AsyncReducer that only produces an output value when having receives exactly one input value, otherwise will return the otherwise value or undefined. |