store-machine
v1.0.1
Published
Simple store that uses event emitter
Downloads
6
Maintainers
Readme
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'));