A stream model represents a typesafe state that can be derived from an event log. The projection also support optimistic events, which are events that are applied to the state before the server has confirmed the event was successfully appended to the state.

Note: Keep in mind that the events should be safe to JSON decode and encode. Only use primitives, don't use classes, functions, or other complex types as part of your events.

Type Parameters

  • State

Constructors

Properties

Methods

Constructors

Properties

eventDefinitions: EventDefinitions<State> = {}
initialState: State

Methods

  • Spawns a new stream from this model. This stream can be used to dispatch events and subscribe to changes in the state.

    Parameters

    Returns Stream<State>

  • Adds a new handler for when a specific type of event is dispatched.

    Type Parameters

    • Type extends string
    • PayloadSchema extends ZodType<any, any, any>

    Parameters

    • type: Type

      The identifier of the event. This must be unique for this event source. For example: 'increment'

    • payloadSchema: PayloadSchema

      The Zod schema to use for the payload of the event. For example z.number().int().min(1).max(5)

    • handler: ((state, dispatcher, payload) => void | State)

      The handler that will apply the payload to the state. This method receives the state, the dispatcher, and the payload. You can manipulate the state directly (made possible by Immer) or return a new state object.

    Returns PayloadSchema extends ZodUndefined
        ? (() => {
            payload: undefined;
            type: Type;
        })
        : ((payload) => {
            payload: TypeOf<PayloadSchema>;
            type: Type;
        })

    Returns a constructor for creating type safe events.