@ainb/store

store

Usage no npm install needed!

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

README

store

A state container for JavaScript.

Installation

$ yarn add @ainb/store

Usage

import createStore from '@ainb/store';

const store = createStore({ a: 1, b: 2 });

// set state or getter
store.$set('c', { d: 3 });
store.$get('add', () => store.a + store.b);

// watch change
store.$watch('add', (now, old) => {
    // do something;
});

// get state
console.log(store.c); // { d: 3 }
console.log(store.add); // 3

// update state
store.a = 2;
store.c = { f: 4 };

// wait updated
store.$nextTick(() => {
    console.log(store.c); // { f: 4 }
    console.log(store.add); // 4
});

Api

  • $set(key, value): set state
  • $get(key, handler): create getter
  • $delete(key): delete state
  • $watch(key, handler): watch change
  • $nextTick(callback): wait updated