Essential state management
9 times out of 10 you will probably only need:
- set-state
- get-state
- subscribe
- unsubscribe
import status from 'status';
status.set('dragon:animation', 'start');
status.get('dragon:animation'); // 'start'
status.subscribe('dragon:animation', 'moat', ({value}) => animateMoat(value));
status.unsubscribe('dragon:animation', 'moat'); // Removes moat from dragon:animation
Ideally this should be handeled outside of state-machines.
Subscritions provide similar functionality but in a more granular way.
One store is the ideal goal. Multiple stores will have negligible difference to garbage collection. Denoted namespaces can solve complexity issues.
A lot can be achieved with one or two methods.
state.set('nav', 'open');
state.subscribe(() => <do something here>);
Status has another method status.mutate
which lets you directly overwrite the two core objects:
- store
- subscribers
It goes without saying, you should only touch this method if you know what you understand the risks. mutate
is ideal for storing state in local storage as well as modifying the state when being retrieved from local storage.