@jswork/next-active-state
v1.0.14
Published
Mini state mananger based on proxy.
Downloads
10
Readme
next-active-state
Mini state mananger based on proxy.
installation
npm install -S @jswork/next-active-state
apis
| api | params | description | | ------- | ------ | ------------------------------------ | | get | - | Unwrap proxy data to pure js object. | | on | - | Watch changed and return destroy fn. | | one | - | Watch changed and return destroy fn. | | reset | - | Watch changed and return destroy fn. | | touched | - | Get state has touched. | | #use | - | Use object with listener. | | #toJS | - | Transform to pure js object. |
usage
import NxActiveState from '@jswork/next-active-state';
const data = { key: 1, value: 2 };
const instance = new NxActiveState(data);
const state = instance.state;
const res = instance.on('change', (arg) => {
console.log(arg);
});
// create
state.newKey = 'I am new.';
// update
state.key = 122;
// delete
delete state.value;
// destroy resource
res.destroy();
// simple use
const data = { key: 1, value: 2 };
const state = NxActiveState.use(data, (arg) => {
console.log(arg);
});
// create
state.newKey = 'I am new.';
// update
state.key = 122;
// delete
delete state.value;
// no need to destroy resource
license
Code released under the MIT license.