README
@ainc/store
A tool for create reactive store!
Install
$ yarn add @ainc/store
Usage
import store from '@ainc/store';
// create store
const ob = store({
a: 1,
b: 2,
get c() {
return this.a + this.b;
}
});
// get state
console.log(ob.a); // 1
console.log(ob.b); // 2
console.log(ob.b); // 3
// watch change
ob.$watch('a', (val, old) => console.log(val, old));
// subscribe state and change
ob.$subscribe(state => console.log(state));
// subscribe next tick
ob.$nextTick(state => console.log(state));
// change state
ob.a = 3;
// get state
console.log(ob.a); // 3
console.log(ob.b); // 2
console.log(ob.c); // 5
API
create state store;
(id: K, handler: (curr: T[K], old: T[K]) => void): () => void;
store.$watchwatch state change by state id, return unwatch handler;
(handler: (state: Store) => void): () => void;
store.$subscribesubscribe state change, return the unsubscribe handler;
(callback?: (state: Store) => R): Promise<R | void>;
store.$nextTicksubscribe next tick, return the Promise which update completed;