@deebloo/state-container
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

StateContainer CircleCI

A state container is created by defining an initial state and a reducer

import { StateContainer, Action } from '@deebloo/state-container';

const enum CounterTodoType { Increment, Decrement }

class Increment implements Action<CounterTodoType> {
  readonly type = CounterTodoType.Increment;
}

class Decrement implements Action<CounterTodoType> {
  readonly type = CounterTodoType.Decrement;
}

const container = new StateContainer<number, Increment | Decrement>(0, (state, action) => {
  switch(action.type) {
    case CounterTodoType.Increment:
      return state + 1;

    case CounterTodoType.Increment:
      return state + 1;
  }

  return state;
});

Get state by subscribing to the value

// A raw action
container.value.subscribe(console.log);

Update State by passing it a StateResult which is either:

// A raw action
container.update(new Increment());
// A function that returns an action
container.update(() => new Increment());
// A function that returns a promise that resolves to an action
container.update(() => fetch('/my-api').then(() => new Increment()));
// A function that returns an Observable that resolves to an action
container.update(() => of('Hello').pipe(map(() => new Increment())));

Readme

Keywords

none

Package Sidebar

Install

npm i @deebloo/state-container

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

39.6 kB

Total Files

20

Last publish

Collaborators

  • deebloo