Class Store<T>Abstract

Abstract Store base class. By extending this Store base class and decorating the extending class with the Stateful decorator, the resulting Store will become a functional facade implementing only the dispatch and well-known Symbol.observable methods. This resulting facade provides convenient access to the current and upcoming States of the Store and its dispatch method, while, behind the facade, interactions with the BusHandler to provide an Observable of the State changes and the StateHandler to dispatch any Actions will be handled transparently.

The same functionality can be achieved by manually supplying a Store to the StateHandler and subscribing to the changes of that Store through the BusHandler while any Actions also have to be passed manually to the StateHandler. But the Stateful decorator should be preferred out of convenience and because invoking the constructor of the Store class throws a TypeError.

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 Action through the ExampleStore facade:

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

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

Type Parameters

Constructors

Properties

Methods

Constructors

Properties

[observable]: (() => Subscribable<Store.State<T>>)

Well-known Symbol.observable method returning a Subscribable. The returned Subscribable emits all States this Store traverses, i.e., all States that result from dispatching Actions on this Store.

Type declaration

Returns

A Subscribable emitting State changes.

Throws

An ReferenceError when not called Stateful.

Example

Subscribe to the ExampleStore:

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

const store = new ExampleStore();
from(store).subscribe(console.log);

Methods

Generated using TypeDoc