Store

The main structure of Foxstore

How to use?

You can create your own Store just by extending ProtoStore class which provides base functional of Store.

Simply, Store is just a state-stream and an event-stream. You have method patch() to patch state, listen() to listen events from event bus. To get updates from the State you can use select() and get an Observable of some entity from the State.

const store = new ProtoStore(initState);

const items: Item[] = await someService.getItems();

// Patch the State by items as 'items'
store.patch({ items });

// Listen for an Event = subscribe on it
store.listen('ItemsHandled').subscribe(() => console.log('Success!')

// Dispatch an Event with payload
store.dispatch('ItemsHandled', items);

store.on('ItemsHandled', items => store.patch({ items }));

Remember to use ProtoStore.destroy() when you do not need any subscriptions to the state of events.

Event Scheme

By the options you can setup your Store using some default features, for example - by passing EventScheme - a scheme of your events and handlers for it. It allows you to have an autocompletion and type-check when you are dispatching an Event.

Autocompletion and type checking is powerful, but not the most significant mean of EventScheme. the main idea is that you can in one place set the Scheme of every logic in your Store. It is easy to read, easy to modify, easy to reuse some parts.

Last updated