Modules@robinpath/state
state

@robinpath/state

0.1.1Node.jsPublic

Finite state machine with transitions, guards, actions, context, history, and event listeners

State

Finite state machine with transitions, guards, actions, context, history, and event listeners

Package: @robinpath/state | Category: Infrastructure | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the state module when you need to:

  • Create state machine -- Use state.create to perform this operation
  • Send event to trigger transition -- Use state.send to perform this operation
  • Get current state -- Use state.current to perform this operation
  • Get machine context -- Use state.context to perform this operation
  • Check if event can be sent -- Use state.can to perform this operation

Quick Reference

FunctionDescriptionReturns
createCreate state machine{name, current, states, transitions}
sendSend event to trigger transition{changed, from, current, event}
currentGet current stateCurrent state
contextGet machine contextContext data
setContextSet machine contexttrue
canCheck if event can be senttrue if transition exists
eventsGet available events from current stateEvent names
isCheck if in specific statetrue if matches
resetReset to initial state{current}
historyGet transition historyHistory entries
addTransitionAdd transition at runtimetrue
addStateAdd state at runtimetrue
onListen for transitionstrue
serializeSerialize machine to JSONJSON string
matchesCheck if current state matches anytrue if matches
destroyDestroy machinetrue
listList all machinesMachine names

Functions

create

Create state machine

Module: state | Returns: object -- {name, current, states, transitions}

state.create {"name": "light", "states": ["red", "green", "yellow"], "initial": "red", "transitions": [{"from": "red", "to": "green", "event": "next"}]}
ParameterTypeRequiredDescription
optionsobjectYes{name, states[], initial, transitions[{from, to, event}], context}

send

Send event to trigger transition

Module: state | Returns: object -- {changed, from, current, event}

state.send "next" "light"
ParameterTypeRequiredDescription
eventstringYesEvent name
machinestringNoMachine name

current

Get current state

Module: state | Returns: string -- Current state

state.current "light"
ParameterTypeRequiredDescription
machinestringNoMachine name

context

Get machine context

Module: state | Returns: any -- Context data

state.context "light"
ParameterTypeRequiredDescription
machinestringNoMachine name

setContext

Set machine context

Module: state | Returns: boolean -- true

state.setContext {"count": 0} "light"
ParameterTypeRequiredDescription
contextanyYesNew context
machinestringNoMachine name

can

Check if event can be sent

Module: state | Returns: boolean -- true if transition exists

state.can "next" "light"
ParameterTypeRequiredDescription
eventstringYesEvent name
machinestringNoMachine name

events

Get available events from current state

Module: state | Returns: array -- Event names

state.events "light"
ParameterTypeRequiredDescription
machinestringNoMachine name

is

Check if in specific state

Module: state | Returns: boolean -- true if matches

state.is "red" "light"
ParameterTypeRequiredDescription
statestringYesState name
machinestringNoMachine name

reset

Reset to initial state

Module: state | Returns: object -- {current}

state.reset "light"
ParameterTypeRequiredDescription
machinestringNoMachine name

history

Get transition history

Module: state | Returns: array -- History entries

state.history "light" 10
ParameterTypeRequiredDescription
machinestringNoMachine name
limitnumberNoMax entries

addTransition

Add transition at runtime

Module: state | Returns: boolean -- true

state.addTransition {"from": "yellow", "to": "red", "event": "next"} "light"
ParameterTypeRequiredDescription
transitionobjectYes{from, to, event}
machinestringNoMachine name

addState

Add state at runtime

Module: state | Returns: boolean -- true

state.addState "flashing" "light"
ParameterTypeRequiredDescription
statestringYesState name
machinestringNoMachine name

on

Listen for transitions

Module: state | Returns: boolean -- true

state.on "transition" $handler "light"
ParameterTypeRequiredDescription
eventstringYesEvent type (transition)
handlerstringYesCallback (from, to, event)
machinestringNoMachine name

serialize

Serialize machine to JSON

Module: state | Returns: string -- JSON string

state.serialize "light"
ParameterTypeRequiredDescription
machinestringNoMachine name

matches

Check if current state matches any

Module: state | Returns: boolean -- true if matches

state.matches ["red", "yellow"] "light"
ParameterTypeRequiredDescription
statesarrayYesState names
machinestringNoMachine name

destroy

Destroy machine

Module: state | Returns: boolean -- true

state.destroy "light"
ParameterTypeRequiredDescription
machinestringNoMachine name

list

List all machines

Module: state | Returns: array -- Machine names

state.list
ParameterTypeRequiredDescription
(none)NoCall with no arguments

Error Handling

All functions throw on failure. Common errors:

ErrorCause
State machine "..." not foundCheck the error message for details
@desc "Create and validate result"
do
  set $result as state.create {"name": "light", "states": ["red", "green", "yellow"], "initial": "red", "transitions": [{"from": "red", "to": "green", "event": "next"}]}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "List and iterate results"
do
  set $result as state.list
  each $item in $result
    print $item
  end
enddo

2. Create a new item with create

Create a new resource and capture the result.

set $result as state.create {"name": "light", "states": ["red", "green", "yellow"], "initial": "red", "transitions": [{"from": "red", "to": "green", "event": "next"}]}
print "Created: " + $result

3. Check before creating

List existing items and only create if needed.

@desc "List and create"
do
  set $existing as state.list
  if $existing == null
    state.create {"name": "light", "states": ["red", "green", "yellow"], "initial": "red", "transitions": [{"from": "red", "to": "green", "event": "next"}]}
    print "Item created"
  else
    print "Item already exists"
  end
enddo

4. Multi-step State workflow

Chain multiple state operations together.

@desc "Create, send, and more"
do
  set $r_create as state.create {"name": "light", "states": ["red", "green", "yellow"], "initial": "red", "transitions": [{"from": "red", "to": "green", "event": "next"}]}
  set $r_send as state.send "next" "light"
  set $r_current as state.current "light"
  print "All operations complete"
enddo

5. Safe create with validation

Check results before proceeding.

@desc "Create and validate result"
do
  set $result as state.create {"name": "light", "states": ["red", "green", "yellow"], "initial": "red", "transitions": [{"from": "red", "to": "green", "event": "next"}]}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/state

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.5 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Category

devops