0.2.0 • Published 1 year ago

val-state v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

val-state

Build Status npm-version Coverage Status minified-size

Commitizen friendly Conventional Commits code style: prettier

State management with value-enhancer.

Install

npm add val-state value-enhancer

Usage

import { createStore } from "val-state";

const store$ = createStore({
  count: 0,
});

store$.subscribe(state => {
  console.log(state);
});

Create store with actions:

import { createStore } from "val-state";

const store$ = createStore(
  {
    count: 0,
  },
  state$ => ({
    increment: (step = 1) => {
      state$.set({ count: state$.value.count + step });
    },
  })
);

store$.subscribe(state => {
  console.log(state);
});

store$.act.increment();

Assign actions to normal val:

import { val } from "value-enhancer";
import { assignActions } from "val-state";

const state$ = val({ count: 1 });

const store$ = assignActions(state$, {
  increment: (step = 1) => {
    state$.set({ count: state$.value.count + step });
  },
});

assert(store$ === state$);

store$.act.increment();