Robot Documentation
Build finite state machines in a simple and flexible way. At just 1KB, Robot brings declarative state management to your applications.
TINY_SIZE
Just 1KB of pure robotic efficiency. No bloat, all bot!
FUNCTIONAL
Built with functions, not options objects. Easy composition and reuse.
DECLARATIVE
Eliminate invalid states and prevent entire categories of bugs.
Getting Started
Install Robot via npm or yarn:
npm install robot3
yarn add robot3
Basic Example
Create your first state machine:
import { createMachine, state, transition } from 'robot3';
const machine = createMachine({
inactive: state(
transition('toggle', 'active')
),
active: state(
transition('toggle', 'inactive')
)
});
export default machine;
Use with React/Preact
Robot has integrations for all major frameworks:
import { h } from 'preact';
import { useMachine } from 'preact-robot';
import machine from './machine.js';
function Counter() {
const [current, send] = useMachine(machine);
const state = current.name;
return (
<>
<div>State: {state}</div>
<button onClick={() => send('toggle')}>
Toggle
</button>
</>
);
}
Advanced Features
Handle async operations, context, and complex state logic:
import { createMachine, invoke, reduce, state, transition } from 'robot3';
const context = () => ({ users: [] });
async function loadUsers() {
return [
{ id: 1, name: 'Wilbur' },
{ id: 2, name: 'Matthew' },
{ id: 3, name: 'Anne' }
];
}
const machine = createMachine({
idle: state(
transition('fetch', 'loading')
),
loading: invoke(loadUsers,
transition('done', 'loaded',
reduce((ctx, ev) => ({ ...ctx, users: ev.data }))
)
),
loaded: state()
}, context);
Why Finite State Machines?
Robot brings the declarative revolution to application state. Instead of managing multiple booleans and imperative state updates, define your states declaratively and eliminate invalid states entirely.
With Robot, your states and transitions are validated when the machine is created, preventing an entire category of bugs before they happen.
✅ With Robot
- • Declarative state definitions
- • Impossible states eliminated
- • Clear transitions and events
- • Built-in validation
❌ Without Robot
- • Multiple boolean flags
- • Invalid state combinations
- • Imperative state management
- • Hard to debug state bugs
Ready to Build?
Explore the documentation to learn about Robot's powerful features and integrations.