# @state-designer/core

> State management with state-charts.

Latest version **3.0.0** (published 2021-11-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @state-designer/core
pnpm add @state-designer/core
yarn add @state-designer/core
bun add @state-designer/core
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2021-11-14 |
| First published | 2020-05-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 247.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 670 |
| Author | @steveruizok |
| Maintainers | steveruizok, state-designer |
| Keywords | react, state, statechart |

## Links

- npm: https://www.npmjs.com/package/@state-designer/core
- Repository: https://github.com/steveruizok/state-designer/core
- npm.io page: https://npm.io/package/@state-designer/core

## Dependencies (2)

- [immer](https://npm.io/package/immer.md) ^9.0.6
- [nanoid](https://npm.io/package/nanoid.md) ^3.1.30

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2021-11-14
- 3.0.1-alpha.0 — 2021-11-14
- 2.0.3 — 2021-07-12
- 2.0.1 — 2021-07-12
- 2.0.0 — 2021-07-12
- 1.7.41 — 2021-07-04
- 1.7.4 — 2021-07-04
- 1.7.32 — 2021-06-18
- 1.7.31 — 2021-06-06
- 1.7.3 — 2021-06-06
- 1.7.2 — 2021-06-06
- 1.7.1 — 2021-04-10
- 1.7.0 — 2021-04-10
- 1.6.93 — 2021-03-29
- 1.6.92 — 2021-03-29
- … 87 more at https://npm.io/package/@state-designer/core/versions

## README

# State Designer / Core

This package includes the core functions of [State Designer](https://statedesigner.com), a JavaScript state management library. [Click here](https://statedesigner.com) to learn more.

You can use this package in any JavaScript or TypeScript project.

For React projects, see [@state-designer/react](https://github.com/@state-designer/react).

## Installation

```bash
npm install @state-designer/core

# or

yarn add @state-designer/core
```

## Usage

Using State Designer involves three steps:

1. Create a state with a configuration object.
2. Subscribe to the state's updates.
3. Send events to the state.

### 1. Creating a State

To create a new **state**, call the `createState` function and pass it a **configuration object**.

A configuration object defines everything about the state:

- what **data** it controls;
- how it should respond to **events** ; and
- how its **child states** are organized

```js
const state = createState({
  data: { items: 0 },
  on: {
    ADDED_ITEM: {
      unless: (data) => data.items >= 10,
      do: (data) => void data.items++,
    },
    RESET: (data) => void (data.items = 0),
  },
})
```

The example below is just the start of what you can add to a state's configuration object. [Click here](https://statedesigner.com) to learn more.

### 2. Subscribing to Updates

To subscribe to a state's **updates**, call the state's `onUpdate` method and pass it a callback function to call with each new update.

An update includes:

- The state's current **data**
- An array of the state's **active** states
- The full **state tree**

```js
state.onUpdate(({ data }) => {
  document.getElementById('itemsCount').textContent = data.items.toString()
})
```

### 3. Sending Events

To send an **event** to the state, call the state's `send` method.

The send method takes two arguments:

- The name of the event as a string
- A payload of any type (optional)

```js
document.getElementById('reset_button').onclick = () => {
  state.send('RESET')
}

document.getElementById('plus_two_button').onclick = () => {
  state.send('ADDED_ITEMS', 2)
}
```

## Example

[![Edit state-designer-vanilla-example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/adoring-nightingale-g7ch1?fontsize=14&hidenavigation=1&theme=dark)

```js
import { createState } from '@state-designer/core'

// Create state
const state = createState({
  data: { items: 0 },
  on: {
    ADDED_ITEMS: {
      unless: (data, payload) => data.items + payload > 10,
      do: (data, payload) => void (data.items += payload),
    },
    RESET: (data) => void (data.items = 0),
  },
})

// Subscribe to updates
state.onUpdate(({ data }) => {
  document.getElementById('itemsCount').textContent = data.items.toString()
})

// Send events
document.getElementById('reset_button').onclick = () => {
  state.send('RESET')
}

document.getElementById('plus_two_button').onclick = () => {
  state.send('ADDED_ITEMS', 2)
}
```

## API

### `createState`

Creates a new state from a configuration object.

```ts
const state = createState({
  data: { items: 0 },
  on: {
    CLICKED_PLUS: 'increment',
  },
  actions: {
    increment: (data) => void data.count++,
  },
})
```

### `createDesign`

> **Note:** This function only exists to provide additional type support in TypeScript projects and in testing where multiple states need to use the same configuration object.

A helper function that creates a configuration object. The configuration provides several additional helpers for creating type-checked actions, events, and other parts of the object.

```ts
const config = createDesign({
  data: { items: 0 },
  on: {
    CLICKED_PLUS: 'increment',
  },
  actions: {
    increment: (data) => void data.count++,
  },
})

const state = createState(config)
```

| Helper                         | Description                               |
| ------------------------------ | ----------------------------------------- |
| createEventHandlerConfig       | Creates an event handler config.          |
| createEventHandlerItemConfig   | Creates an event handler item config.     |
| createAsyncEventHandlerConfig  | Creates an async event handler config.    |
| createRepeatEventHandlerConfig | Creates a repeating event handler config. |
| createStateConfig              | Creates a state config.                   |
| createActionConfig             | Creates an action config.                 |
| createConditionConfig          | Creates a condition config.               |
| createResultConfig             | Creates a result config.                  |
| createTimeConfig               | Creates a time config.                    |

---
_Source: https://npm.io/package/@state-designer/core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
