All Projects → developit → State Machine Component

developit / State Machine Component

⚙️ State machine -powered components in 250 bytes

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to State Machine Component

xoid
Framework-agnostic state management library designed for simplicity and scalability ⚛
Stars: ✭ 96 (-46.07%)
Mutual labels:  state-management, preact, state-machine
statebot
Write more robust and understandable programs. Statebot hopes to make Finite State Machines a little more accessible.
Stars: ✭ 24 (-86.52%)
Mutual labels:  state-management, state-machine
xstate-cpp-generator
C++ State Machine generator for Xstate
Stars: ✭ 33 (-81.46%)
Mutual labels:  state-management, state-machine
Redux Machine
A tiny library (12 lines) for creating state machines in Redux apps
Stars: ✭ 338 (+89.89%)
Mutual labels:  state-machine, state-management
useStateMachine
The <1 kb state machine hook for React
Stars: ✭ 2,231 (+1153.37%)
Mutual labels:  state-management, state-machine
stoxy
Stoxy is a state management API for all modern Web Technologies
Stars: ✭ 73 (-58.99%)
Mutual labels:  state-management, preact
Beedle
A tiny library inspired by Redux & Vuex to help you manage state in your JavaScript apps
Stars: ✭ 329 (+84.83%)
Mutual labels:  state-machine, state-management
teaful
🍵 Tiny, easy and powerful React state management
Stars: ✭ 638 (+258.43%)
Mutual labels:  state-management, preact
Machinery
State machine thin layer for structs (+ GUI for Phoenix apps)
Stars: ✭ 367 (+106.18%)
Mutual labels:  state-machine, state-management
Little State Machine
📠 React custom hook for persist state management
Stars: ✭ 654 (+267.42%)
Mutual labels:  state-machine, state-management
React Context Hook
A React.js global state manager with Hooks
Stars: ✭ 50 (-71.91%)
Mutual labels:  state-machine, state-management
preact-urql
Preact bindings for urql
Stars: ✭ 28 (-84.27%)
Mutual labels:  state-management, preact
fs2-es
Event sourcing utilities for FS2
Stars: ✭ 75 (-57.87%)
Mutual labels:  state-management, state-machine
xstate-viz
Visualizer for XState machines
Stars: ✭ 274 (+53.93%)
Mutual labels:  state-management, state-machine
statebot-sh
Statebot for shell-scripts. Write more robust and understandable programs.
Stars: ✭ 14 (-92.13%)
Mutual labels:  state-management, state-machine
zedux
⚡ A high-level, declarative, composable form of Redux https://bowheart.github.io/zedux/
Stars: ✭ 43 (-75.84%)
Mutual labels:  state-management, state-machine
Zproc
Process on steroids
Stars: ✭ 112 (-37.08%)
Mutual labels:  state-machine, state-management
use-state-machine
Use Finite State Machines with React Hooks
Stars: ✭ 28 (-84.27%)
Mutual labels:  state-management, state-machine
kstatemachine
KStateMachine is a Kotlin DSL library for creating finite state machines (FSM) and hierarchical state machines (HSM).
Stars: ✭ 63 (-64.61%)
Mutual labels:  state-management, state-machine
Pvm
Build workflows, activities, BPMN like processes, or state machines with PVM.
Stars: ✭ 348 (+95.51%)
Mutual labels:  state-machine, state-management

state-machine-component

state-machine-component

npm

A tiny (~250 byte) utility to create state machine components using two pure functions.

🔥 JSFiddle Demo

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

npm install --save state-machine-component

Then with a module bundler like webpack or rollup, use as you would anything else:

import stateMachine from 'state-machine-component';

The UMD build is also available on unpkg:

<script src="//unpkg.com/state-machine-component/dist/state-machine-component.umd.js"></script>

The library will install itself globally as window.stateMachineComponent.

Usage

The API is a single function that accepts 2 pure functions as arguments:

stateMachineComponent(reduce, render)

The first function, reduce(), takes in the current state and applies an action to it, similar to a reducer in Redux:

// Reduce is a redux-style reducer
function reduce(state, action) {
	// actions are like Redux Standard Actions:
	let { type, data, props } = action

	return { }  // just return the new state
}

The second function, render(), is a pure functional component that gets passed the current state instead of props, and a second argument action() - a function that creates a bound dispatcher for the given action type:

// Render is a functional component with little twist
function render(state, action) {
	// action() creates a dispatcher for an action type:
	return <button onClick={ action('TYPE') } />
}

Simple Example: Counter

// Remember:
//  `state` is the current state.
//  `action` is a redux standard action.
function reduce(state, action) {
	switch (action.type) {
		case '@@INIT': return { count: 0 }
		case 'ADD': return { count: state.count+1 }
	}
}

function render(state, action) {
	return (
		<div class="counter">
			Current count: {state.count}
			<button onClick={action('ADD')}>Add 1</button>
		</div>
	)
}

stateMachineComponent(reduce, render)

Full Example: To-Do List

const ToDos = stateMachineComponent(
	// (state, action)
	({ todos, text }, { type, data, props }) => {
		switch (type) {
			case '@@INIT':return { todos: props.todos || [], text: '' };
			case 'ADD': return { todos: todos.concat(text), text: '' };
			case 'TEXT': return { text: data.target.value };
		}
	},
	// state, action(type)
	({ todos, text }, action) => (
		<div>
			<h2>State Machine ToDos</h2>
			<ul>{todos.map( todo => <li>{todo}</li> )}</ul>
			<form onSubmit={action('ADD')}>
				<input value={text} onInput={action('TEXT')} />
			</form>
		</div>
	)
);

License

MIT License © Jason Miller

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].