Skip to content

notdotscott/mini-state-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mini-state-manager

Init a new Store, declaring state, updates and actions up-front:

import Store from 'mini-state-manager';

const store = new Store({
    state: {
        count: 0
    },
    updates: {
        increment: state => state.count++,
        decrement: state => state.count--,
        incrementBy: (state, payload) => state.count += payload.value,
        decrementBy: (state, payload) => state.count -= payload.value
    },
    actions: {
        incrementAsync: ({update}) => {
            setTimeout(() => update('increment'), 100);
        },
        decrementAsync: ({update}) => {
            setTimeout(() => update('decrement'), 100);
        },
        incrementByAsync: ({update}) => {
            setTimeout(() => update('incrementBy', { value: 5 }), 100);
        },
        decrementByAsync: ({update}) => {
            setTimeout(() => update('decrementBy', { value: 5 }), 100);
        }
    }
});

Registering update callbacks:

const myCallback = () => console.log('State updated. Count is now: ', store.state.count);
const myOtherCallback = () => console.log('blah blah blah');
store.register(myCallback);
store.register(myOtherCallback);

Deregistering update callbacks:

store.deregister(myOtherCallback);

Updating state:

console.log(store.state.count); //0
store.update('increment'); //State updated. Count is now: 1
store.update('incrementBy', { value: 5 }); //State updated. Count is now: 6

Batch updating:

store.batchUpdate([
    {type: 'increment'},
    {type: 'decrementBy', payload: { value: 5 }},
    {type: 'decrement'}
]); //State updated. Count is now: 1

Dispatching actions:

store.dispatchAction('incrementAsync');
//...async call, waiting...
//State updated. Count is now: 2

store.dispatchAction('incrementByAsync', { value: 5 });
//...async call, waiting...
//State updated. Count is now: 7

Destroying:

store.destroy();

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published