Documentation
    Preparing search index...

    Class StatefulSubject<Actions, State>

    Another form of the :class that reduces a piece of state with actions and queues changes to the stream.

    It's important to note that if the result of an action does not change the state then nothing is queued to the stream.

    // The State will be the type of data you want to manipulate.
    interface State {
    authors: string[]
    }

    // The Actions are a record of "action name" to a parameter.
    // If the action doesn't require a parameter, use `void`.
    type Actions = {
    'add author': string
    'action that doesnt need a parameter': void
    }

    // To get the best out of this API, pass the Actions type and State
    // during construction.
    const subject = new StatefulSubject<Actions, State>(
    {
    __INIT__: () => ({ authors: [] }),

    'add author': (state, author) => state.authors.includes(author) ? state : {
    ...state,
    authors: [...state.authors, author],
    },

    'action that doesnt need a parameter': (state) => state
    }
    )

    subject.fork().pipeTo(write(console.info))
    // { action: '__INIT__', state: { authors: [] } }

    subject.dispatch('add author', 'Jane Austin')
    // { action: 'add author', param: 'Jane Austin', state: { authors: ['Jane Austin'] } }

    subject.dispatch('add author', 'Jane Austin')
    // **Nothing will be queued as the state did not change.**

    Type Parameters

    • Actions extends Record<string, unknown>
    • State

    Hierarchy (View Summary)

    Index

    Constructors

    Accessors

    Methods

    • Parameters

      • OptionalunderlyingSource: UnderlyingDefaultSource<
            AccumulateStateReducerOutput<
                Actions,
                Readonly<State>,
                ListOf<keyof Actions>,
                { action: "__INIT__"; param: void; state: Readonly<State> },
            >,
        >
      • OptionalqueuingStrategy: QueuingStrategy<
            AccumulateStateReducerOutput<
                Actions,
                Readonly<State>,
                ListOf<keyof Actions>,
                { action: "__INIT__"; param: void; state: Readonly<State> },
            >,
        >

      Returns ReadableStream<
          AccumulateStateReducerOutput<
              Actions,
              Readonly<State>,
              ListOf<keyof Actions>,
              { action: "__INIT__"; param: void; state: Readonly<State> },
          >,
      >

    • Parameters

      • OptionalqueuingStrategy: QueuingStrategy<
            AccumulateStateReducerInput<
                Actions,
                ListOf<keyof Actions>,
                { action: "__INIT__"; param: void },
            >,
        >

      Returns WritableStream<
          AccumulateStateReducerInput<
              Actions,
              ListOf<keyof Actions>,
              { action: "__INIT__"; param: void },
          >,
      >