store-machine
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

About

Simple store that uses event emitter

Install

npm i store-machine

Usage

const { createStore } = require('store-machine');
 
const initState = {
  id: 1,
  name: '...'
};
 
// basic api: on, all, change, getState
const userStore = createStore(initState);
 
// method, like redux "action + reducer"
function updateUserName(name) {
  return {
    type: 'update_user_name',
    data: { name },
    cb: (state, data) => ({ ...state, ...data }) 
  }
}
 
// get private state
let user = userStore.getState();
 
// like redux "subscribe"
userStore.all(() => console.log('getState =', userStore.getState()));
 
userStore.all(e => console.log('e =', e));
 
// subscribtion to the specific event
userStore.on('update_user_name', d => {
  console.log('d =', d);
});
 
// like redux "dispatch"
userStore.change(updateUserName('Timur'));

Package Sidebar

Install

npm i store-machine

Weekly Downloads

1

Version

1.0.1

License

ISC

Unpacked Size

2.59 kB

Total Files

5

Last publish

Collaborators

  • hollonify