@jswork/next-active-state

Mini state mananger based on proxy.

Usage no npm install needed!

<script type="module">
  import jsworkNextActiveState from 'https://cdn.skypack.dev/@jswork/next-active-state';
</script>

README

next-active-state

Mini state mananger based on proxy.

version license size download

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.