开发者社区> 镜像站> NPM> @platform/state
@platform/state
A small, simple, strongly typed, [rx/observable] state-machine.
Last updated 8 days ago by philcockfield .
MIT · Original npm · Tarball · package.json
$ cnpm install @platform/state 
SYNC missed versions from official npm registry.

Module License: MIT NPM banner

A small, simple rx/observable based state-machine.
For applying to UI see the react bindings at @platform/state.react

 

Install

yarn add @platform/state

To work abstractly with a state library use the isolated type library:

yarn add @platform/state.types

 

Getting Started

Define your model and mutation events:

type IMyModel = {
  count: number;
};

type MyEvent = IIncrementEvent | IDecrementEvent;
type IIncrementEvent = { type: 'TEST/increment'; payload: { by: number } };
type IDecrementEvent = { type: 'TEST/decrement'; payload: { by: number } };
type IStatustEvent = { type: 'TEST/status'; payload: { status: string } };

 

Create a new state-machine:

import { Store } from '@platform/state';

const initial: IMyModel = { count: 0, status: string };
const store = Store.create<IMyModel, MyEvent>({ initial });

 

Define a listener that mutates the state based on a specific event type (equivalent to a "reducer"):

store.on<ITestIncrementEvent>('TEST/increment').subscribe((e) => {
  const count = e.state.count + e.payload.by;
  const next = { ...e.state, count };
  e.change(next); // UPDATE: New copy of state applied.
});

alternatively you can use "mutation like" syntax by passing a change function:

store
  .on<ITestIncrementEvent>('TEST/increment')
  .subscribe(e => {
    e.change(draft => {
      draft.count += e.payload.by; // UPDATE: New "structurally shared" immutable changes applied.
    }));
  });

This safely modifies an immutable clone of the state using "structural sharing" for efficiency (via immer).

 

Dispatch events to change state:

store.state; // => count === 0
store.dispatch({ type: 'TEST/increment', payload: { by: 1 } });
store.state; // => count === 1

 

Listen for changes to the state and react accordingly, for instance updating UI that may be rendering the state.:

store.changed$.subscribe((e) => {
  // ...
});

 

Add logic that reacts to events asynchronously and dispatches new update events (equivalent to an "epic"):

store
  .on<IIncrementEvent>('TEST/increment')
  .pipe(debounceTime(300))
  .subscribe(async (e) => {
    const status = await getNetworkStatus();
    e.dispatch({ type: 'TEST/status', payload: { status } });
  });

 

 

Current Tags

  • 0.7.22                                ...           latest (8 days ago)

127 Versions

  • 0.7.22                                ...           8 days ago
  • 0.7.18                                ...           25 days ago
  • 0.7.14                                ...           2 months ago
  • 0.7.10                                ...           2 months ago
  • 0.7.9                                ...           2 months ago
  • 0.7.8                                ...           3 months ago
  • 0.7.7                                ...           3 months ago
  • 0.7.5                                ...           3 months ago
  • 0.7.4                                ...           3 months ago
  • 0.7.3                                ...           4 months ago
  • 0.7.2                                ...           4 months ago
  • 0.7.1                                ...           4 months ago
  • 0.7.0                                ...           4 months ago
  • 0.6.11                                ...           4 months ago
  • 0.6.10                                ...           4 months ago
  • 0.6.9                                ...           4 months ago
  • 0.6.8                                ...           4 months ago
  • 0.6.7                                ...           4 months ago
  • 0.6.6                                ...           4 months ago
  • 0.6.5                                ...           5 months ago
  • 0.6.4                                ...           5 months ago
  • 0.6.2                                ...           5 months ago
  • 0.6.1                                ...           5 months ago
  • 0.6.0                                ...           5 months ago
  • 0.5.6                                ...           5 months ago
  • 0.5.4                                ...           5 months ago
  • 0.5.3                                ...           5 months ago
  • 0.5.2                                ...           6 months ago
  • 0.5.1                                ...           6 months ago
  • 0.5.0                                ...           6 months ago
  • 0.4.64                                ...           6 months ago
  • 0.4.63                                ...           6 months ago
  • 0.4.62                                ...           6 months ago
  • 0.4.55                                ...           7 months ago
  • 0.4.54                                ...           9 months ago
  • 0.4.51                                ...           9 months ago
  • 0.4.48                                ...           9 months ago
  • 0.4.44                                ...           10 months ago
  • 0.4.43                                ...           10 months ago
  • 0.4.42                                ...           10 months ago
  • 0.4.41                                ...           10 months ago
  • 0.4.39                                ...           a year ago
  • 0.4.38                                ...           a year ago
  • 0.4.37                                ...           a year ago
  • 0.4.35                                ...           a year ago
  • 0.4.33                                ...           a year ago
  • 0.4.30                                ...           a year ago
  • 0.4.27                                ...           a year ago
  • 0.4.26                                ...           a year ago
  • 0.4.25                                ...           a year ago
  • 0.4.24                                ...           a year ago
  • 0.4.21                                ...           a year ago
  • 0.4.18                                ...           a year ago
  • 0.4.16                                ...           a year ago
  • 0.4.12                                ...           a year ago
  • 0.4.11                                ...           a year ago
  • 0.4.10                                ...           a year ago
  • 0.4.7                                ...           a year ago
  • 0.4.5                                ...           a year ago
  • 0.4.4                                ...           a year ago
  • 0.4.3                                ...           a year ago
  • 0.4.1                                ...           a year ago
  • 0.4.0                                ...           a year ago
  • 0.2.21                                ...           a year ago
  • 0.2.19                                ...           a year ago
  • 0.2.17                                ...           a year ago
  • 0.2.16                                ...           2 years ago
  • 0.2.15                                ...           2 years ago
  • 0.2.14                                ...           2 years ago
  • 0.2.13                                ...           2 years ago
  • 0.2.10                                ...           2 years ago
  • 0.2.9                                ...           2 years ago
  • 0.2.6                                ...           2 years ago
  • 0.2.5                                ...           2 years ago
  • 0.2.4                                ...           2 years ago
  • 0.2.3                                ...           2 years ago
  • 0.2.2                                ...           2 years ago
  • 0.2.1                                ...           2 years ago
  • 0.2.0                                ...           2 years ago
  • 0.1.12                                ...           2 years ago
  • 0.1.11                                ...           2 years ago
  • 0.1.10                                ...           2 years ago
  • 0.1.9                                ...           2 years ago
  • 0.1.8                                ...           2 years ago
  • 0.1.7                                ...           2 years ago
  • 0.1.6                                ...           2 years ago
  • 0.1.5                                ...           2 years ago
  • 0.1.4                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
  • 0.0.38                                ...           2 years ago
  • 0.0.37                                ...           2 years ago
  • 0.0.36                                ...           2 years ago
  • 0.0.35                                ...           2 years ago
  • 0.0.34                                ...           2 years ago
  • 0.0.33                                ...           2 years ago
  • 0.0.32                                ...           2 years ago
  • 0.0.31                                ...           2 years ago
  • 0.0.30                                ...           2 years ago
  • 0.0.29                                ...           2 years ago
  • 0.0.28                                ...           2 years ago
  • 0.0.27                                ...           2 years ago
  • 0.0.26                                ...           2 years ago
  • 0.0.25                                ...           2 years ago
  • 0.0.24                                ...           2 years ago
  • 0.0.23                                ...           2 years ago
  • 0.0.21                                ...           2 years ago
  • 0.0.20                                ...           2 years ago
  • 0.0.19                                ...           2 years ago
  • 0.0.17                                ...           2 years ago
  • 0.0.16                                ...           2 years ago
  • 0.0.15                                ...           2 years ago
  • 0.0.14                                ...           2 years ago
  • 0.0.13                                ...           2 years ago
  • 0.0.12                                ...           2 years ago
  • 0.0.11                                ...           2 years ago
  • 0.0.10                                ...           2 years ago
  • 0.0.9                                ...           2 years ago
  • 0.0.7                                ...           2 years ago
  • 0.0.6                                ...           2 years ago
  • 0.0.5                                ...           2 years ago
  • 0.0.4                                ...           2 years ago
  • 0.0.3                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
  • 0.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 6
This Month 6
Last Day 0
Last Week 13
Last Month 31
Dependencies (4)
Dev Dependencies (2)