iom is Esperanto for āa littleā š
Tiny, simple, over-powered state management
Itās a good question ā why did I need to roll my own state management library?
First of all, I didnāt need to and other options may very well better suit your needs
But, since you asked:
- š£ itās tiny (~ā kB last time I checked)
- š¤ itās simple (one class whose instances surface only two methods)
- š itās op af (caveat being that thereās no hand-holding whatsoever)
If youāre wondering how state management can be over-poweredā¦
Import iom
import Store from 'iom';
Describe the initial stateā¦ may as well save it somewhere, right?
const INITIAL_STATE = { on: false };
Create the store by passing the initial state into the constructor
const store = new Store(INITIAL_STATE);
Now you have a store
Weāll just send state updates to the console for now
const subscriber = console.log;
Passing a subscriber to the store returns a function which cancels the subscriptionā¦ letās hang onto that
const cancel = store.subscribe(subscriber);
// => { on: false }
All subscribers receive the current state when added to the store
This is where iom likely diverges from state managers youāve used before
You may notice that we havenāt talked at all about updating state yet
Thatās because you can update state in anyway that you like at any point because actions are simply functions passed to the store which take the current state as their argument and return the new state
So letās create an action
const toggleOn = state => ({ ...state, on: !state.on });
And perform an update
store.update(toggleOn);
// => { on: true }
And thatās iom!
You can add and remove subscribers at any point
So letās cancel our subscription now that weāre done
cancel();
Purity and stack safety are up to you
If youāre using a mutable data structure for your state, be mindful and donāt mutate it
Donāt call actions from within subscribers or other actions or you are very likely to encounter infinite loops
iom is MIT licensed.