iom

2.1.1Ā ā€¢Ā PublicĀ ā€¢Ā Published

iom

Travis bundlephobia Node NPM

What

iom is Esperanto for ā€œa littleā€ šŸ˜‰

Tiny, simple, over-powered state management

Why

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)

How

A Quick Example

If youā€™re wondering how state management can be over-poweredā€¦

A Quick Example

1. Letā€™s create a new storeā€¦

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

2. And subscribe to the 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

3. And update the stateā€¦

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!

4. And clean up

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

Purity and stack safety are up to you

Purity

If youā€™re using a mutable data structure for your state, be mindful and donā€™t mutate it

Stack Safety

Donā€™t call actions from within subscribers or other actions or you are very likely to encounter infinite loops


iom is MIT licensed.

Readme

Keywords

none

Package Sidebar

Install

npm i iom

Weekly Downloads

2

Version

2.1.1

License

MIT

Unpacked Size

416 kB

Total Files

6

Last publish

Collaborators

  • flipactual