3.0.0 • Published 2 years ago

@state-designer/core v3.0.0

Weekly downloads
322
License
MIT
Repository
github
Last release
2 years ago

State Designer / Core

This package includes the core functions of State Designer, a JavaScript state management library. Click here to learn more.

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

For React projects, see @state-designer/react.

Installation

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
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 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
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)
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

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.

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.

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

const state = createState(config)
HelperDescription
createEventHandlerConfigCreates an event handler config.
createEventHandlerItemConfigCreates an event handler item config.
createAsyncEventHandlerConfigCreates an async event handler config.
createRepeatEventHandlerConfigCreates a repeating event handler config.
createStateConfigCreates a state config.
createActionConfigCreates an action config.
createConditionConfigCreates a condition config.
createResultConfigCreates a result config.
createTimeConfigCreates a time config.
3.0.0

2 years ago

3.0.1-alpha.0

2 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.7.4

3 years ago

1.7.41

3 years ago

1.7.32

3 years ago

1.7.3

3 years ago

1.7.2

3 years ago

1.7.31

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.91

3 years ago

1.6.93

3 years ago

1.6.92

3 years ago

1.6.9

3 years ago

1.6.8

3 years ago

1.6.7

3 years ago

1.6.6

3 years ago

1.6.4

3 years ago

1.6.1

3 years ago

1.5.6

3 years ago

1.5.5

3 years ago

1.5.4

3 years ago

1.5.3

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.37

3 years ago

1.3.36

3 years ago

1.3.35

3 years ago

1.3.32

3 years ago

1.2.31

4 years ago

1.2.29

4 years ago

1.2.30

4 years ago

1.2.27

4 years ago

1.2.25

4 years ago

1.2.26

4 years ago

1.2.24

4 years ago

1.2.23

4 years ago

1.2.22

4 years ago

1.2.21

4 years ago

1.2.20

4 years ago

1.2.19

4 years ago

1.2.18

4 years ago

1.2.17

4 years ago

1.2.16

4 years ago

1.2.15

4 years ago

1.2.14

4 years ago

1.2.13

4 years ago

1.2.12

4 years ago

1.2.11

4 years ago

1.2.10

4 years ago

1.2.9

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.26

4 years ago

1.1.25

4 years ago

1.1.22

4 years ago

1.1.21

4 years ago

1.1.20

4 years ago

1.1.19

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.13-alpha.0

4 years ago

1.1.12

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.10-alpha.1

4 years ago

1.1.10-alpha.0

4 years ago

1.1.8-alpha.3

4 years ago

1.1.8-alpha.2

4 years ago

1.1.8-alpha.1

4 years ago

1.1.8-alpha.0

4 years ago

1.1.9

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.16

4 years ago

1.0.9

4 years ago