@ainc/store

Let's do something nice with @ainc/store!

Usage no npm install needed!

<script type="module">
  import aincStore from 'https://cdn.skypack.dev/@ainc/store';
</script>

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

store(state: T): Store

create state store;

store.$watch(id: K, handler: (curr: T[K], old: T[K]) => void): () => void;

watch state change by state id, return unwatch handler;

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

subscribe state change, return the unsubscribe handler;

store.$nextTick(callback?: (state: Store) => R): Promise<R | void>;

subscribe next tick, return the Promise which update completed;