2.0.3 โ€ข Published 8 months ago

@rook2pawn/picostate v2.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

๐Ÿญ Picostate

https://github.com/rook2pawn/picostate

published on npm as https://www.npmjs.com/package/@rook2pawn/picostate

Tiny finite state machine with optional guards and side effects โ€” ideal for driving AI conversations, UI state, async workflows, and beyond.

โœจ Features

  • Tiny API surface
  • Clean state transition logic
  • Optional guard conditions before entering a state
  • Optional on(state, fn) hooks to trigger effects
  • Parallel FSM support (like bold, italic, underline)
  • Fully synchronous and testable
  • Zero dependencies

๐Ÿš€ Install

npm install picostate

๐Ÿง  Example

import { PicoState } from 'picostate';

const fsm = new PicoState('idle', {
  idle: { activate: 'listening' },
  listening: { got_transcript: 'thinking' },
  thinking: { got_response: 'speaking' },
  speaking: { done: 'idle' }
});

fsm.on('speaking', () => {
  console.log("Now speaking...");
});

fsm.guard('activate', () => Date.now() % 2 === 0); // random entry condition

fsm.emit('activate');
console.log(fsm.state); // => maybe 'listening' or stays 'idle' if blocked

๐Ÿงช Testing

npm test

Uses tape for simple test definitions. See tests/test.js.

๐Ÿ“š API

new PicoState(initialState, transitions)

Creates a finite state machine.

const fsm = new PicoState('idle',{
  idle: { activate: 'listening' },
  listening: { got_transcript: 'thinking' }
});
.emit(event: string)
// Trigger a transition event. If invalid, it throws.

.state
// Get the current state.

.on(state: string, fn: () => void)
// Trigger a callback when the machine enters a state.

.onchange(fn: (state, prevState) => void)
// Run a callback any time the state changes.

.guard(event: string, fn: () => boolean)
// Prevent a transition unless the guard condition passes.

๐Ÿ—ƒ License

MIT ยฉ 2025 @rook2pawn

2.0.3

8 months ago

2.0.2

8 months ago