0.0.103 • Published 3 years ago

@anic/store v0.0.103

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

@anic/store

Store

Install

$ yarn add @anic/store

Usage

import store from '@anic/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

store(state: T): Store

create state store;

store.$watch(id: string, handler: (curr: any, old: any) => any): () => void;

watch state change by state id, return unwatch handler;

store.$subscribe(handler: (state: State) => void): () => void;

subscribe state change, return the unsubscribe handler;

store.$nextTick<T extends (state: State) => any>(handler: T): Promise<ReturnType>;

subscribe next tick, return the Promise which update completed;

0.0.103

3 years ago

0.0.102

3 years ago

0.0.101

3 years ago