Function Stateful

  • The Stateful decorator, when applied to classes extending the abstract Store base class, converts those extending classes into type-guarding Store facades implementing only the Store.dispatch and the well-known Symbol.observable methods. This resulting facade provides convenient access to the current and upcoming Store.States of the decorated Store and its Store.dispatch method. The decorated class is StateHandler.deployed under the supplied handle using the supplied state as an initial Store.State. If the Store is to be StateHandler.deployed transiently, the supplied state is guaranteed to be used as initial Store.State. Otherwise, a previously persisted Store.State takes precedence over the supplied state.

    Type Parameters

    Parameters

    Returns ((constructor) => T)

    A class constructor decorator.

      • (constructor): T
      • Parameters

        • constructor: T

        Returns T

    Example

    A simple ExampleStore facade:

    import { Stateful, Store } from '@sgrud/state';

    ⁠@Stateful('io.github.sgrud.store.example', {
    property: 'default',
    timestamp: Date.now()
    })
    export class ExampleStore extends Store<ExampleStore> {

    public readonly property!: string;

    public readonly timestamp!: number;

    public async action(property: string): Promise<Store.State<this>> {
    return { ...this, property, timestamp: Date.now() };
    }

    }

    Example

    Subscribe to the ExampleStore facade:

    import { ExampleStore } from './example-store';

    const store = new ExampleStore();
    from(store).subscribe(console.log);
    // { property: 'default', timestamp: [...] }

    Example

    Dispatch an Store.Action through the ExampleStore facade:

    import { ExampleStore } from './example-store';

    const store = new ExampleStore();
    store.dispatch('action', ['value']).subscribe(console.log);
    // { property: 'value', timestamp: [...] }

    See

Generated using TypeDoc