La referencia de la API está disponible solo en inglés por ahora.

Reducer<S, EM extends EventMapBase = EventMapBase>

class
reducer/Reducer.ts:49

Thin wrapper around a pure reducer function (stateful event consumer): given a state S and an event (from `EventUnion<EM>`), returns the next state S.

- The reducer function is expected to be **pure** and **side-effect free**. - Use this class when you want to pass a reducer around as a value, or to unify the reducer interface across the core API.

Type parameters

NameTypeDescription
SState shape handled by this reducer.
EMextends EventMapBaseEvent map describing the valid event keys and payload types.

Constructor

constructor

Creates a new Reducer from a pure reducer function.

signature
new<S, EM>(reduce): Reducer<S, EM>

Creates a new Reducer from a pure reducer function.

Type parameters

NameTypeDescription
SState shape handled by this reducer.
EMextends EventMapBaseEvent map describing the valid event keys and payload types.

Parameters

NameTypeDescription
reduceReducerFunction<S, EM>A function (state, event) => nextState that implements your update logic.

Returns

Reducer<S, EM>

Example

example
const reducer = new Reducer<MyState, MyEM>((state, event) => {
  // implement your transitions here
  return state;
});

Methods

reduce

Applies the reducer to produce the next state.

signature
reduce(state, event): S

Applies the reducer to produce the next state.

Parameters

NameTypeDescription
stateSCurrent state.
NameTypeDescription
eventEventUnion<EM>An event drawn from `EventUnion<EM>`.

Returns

SThe next state produced by the underlying reducer function.

Example

example
const next = reducer.reduce(curr, someEvent as EventUnion<MyEM>);