A constructor type extending the Store.Type.
The extending Store InstanceType.
The Bus.Handle representing the Store.
An initial Store.State for the Store.
Whether the Store is considered transient.
A class constructor decorator.
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() };
}
}
Subscribe to the ExampleStore facade:
import { ExampleStore } from './example-store';
const store = new ExampleStore();
from(store).subscribe(console.log);
// { property: 'default', timestamp: [...] }
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: [...] }
Generated using TypeDoc
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.observablemethods. 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 suppliedhandleusing the suppliedstateas an initial Store.State. If the Store is to be StateHandler.deployedtransiently, the suppliedstateis guaranteed to be used as initial Store.State. Otherwise, a previously persisted Store.State takes precedence over the suppliedstate.