1.1.1 • Published 5 years ago

statemgr v1.1.1

Weekly downloads
18
License
-
Repository
github
Last release
5 years ago

Logo

STATEMGR

Just a simple state manager for small JS projects.

Usage

npm i statemgr

API doc

Statemgr

set

set(property, value) Stores a property into the state manager

get

get(property) Returns a stored property

setReducer

setReducer(dispatcher) Set a reducer (a method that will be called for complex set). The reducer will be called with 2 arguments: the parameters passed to the reducer call & the set method of the state manager.

reduce

reduce(...params) Call the reducer.

Example

Set & get a value:

import Statemgr from "statemgr";

Statemgr.set('foo', 'bar');
console.log(Statemgr.get('foo')); //bar

Subscribe to some changes:

import Statemgr from "statemgr";
import {Observer} from "obsrvble";

Statemgr.subscribe(new Observer(
    'foo',
    params => {
        // Do something here
        console.log(params[0]); // bar
    }
));
Statemgr.set('foo', 'bar');

Call the reducer:

import Statemgr from "statemgr";
import {Observer} from "obsrvble";

Statemgr.subscribe(new Observer(
    'total',
    params => {
        // Do something here
        console.log(params[0]);
    }
));

Statemgr.set('total', 0);
Statemgr.setReducer((params, set) => {
    if (params[0] === 'add') {
        set('total', Statemgr.get('total') + 1);
    }
    if (params[0] === 'sub') {
        set('total', Statemgr.get('total') - 1);
    }
});

Statemgr.reduce('add'); //1
Statemgr.reduce('add'); //2
Statemgr.reduce('sub'); //1
1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago