Provides an interface for finite-state machines. Machines have defined finite states with actions that transition it to other states. Implementations can leverage machines to handle state and subscribe to their events to respond to changes in state.
- Source:
- See:
Extends
Members
state :String
The current state.
Type:
- String
- Source:
Methods
(static) create(options, optionsopt) → {syngen.utility.machine}
Instantiates a new finite-state machine.
Parameters:
Name | Type | Attributes | Default | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object | ||||||||||||||||||
options |
Object |
<optional> |
{} |
Properties
|
- Source:
Returns:
change(state, dataopt)
Changes to state
with data
.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
state |
String | |||
data |
Object |
<optional> |
{} |
- Source:
Fires:
- syngen.utility.machine#event:enter
- syngen.utility.machine#event:enter-{state}
- syngen.utility.machine#event:exit
- syngen.utility.machine#event:exit-{state}
destroy()
Prepares the instance for garbage collection.
- Overrides:
- Source:
dispatch()
Calls the function defined for action
in the current state with data
.
- Source:
Fires:
- syngen.utility.machine#event:after
- syngen.utility.machine#event:after-{event}
- syngen.utility.machine#event:after-{state}-{event}
- syngen.utility.machine#event:before
- syngen.utility.machine#event:before-{event}
- syngen.utility.machine#event:before-{state}-{event}
emit(event)
Dispatches event
to all subscribers with optional ...data
.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
event |
String | ||
...data |
* |
<optional> <repeatable> |
- Inherited From:
- Source:
getState() → {String}
Returns the current state.
- Source:
Returns:
- Type
- String
is(state) → {Boolean}
Returns whether state
is the current state.
Parameters:
Name | Type | Description |
---|---|---|
state |
String |
- Source:
Returns:
- Type
- Boolean
off(eventopt, handleropt)
Unsubscribes handler
from the handlers listening for event
.
If no handler
is specified, then all handlers for event
are removed.
If no event
is specified, then all handlers for all events are removed.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
event |
String |
<optional> |
|
handler |
function |
<optional> |
- Inherited From:
- Source:
on(event, handler)
Subscribes handler
to listen for event
.
Parameters:
Name | Type | Description |
---|---|---|
event |
String | |
handler |
function |
- Inherited From:
- Source:
once(event, handler)
Subscribed handler
to listen for event
once.
The handler
is removed after its next dispatch.
Parameters:
Name | Type | Description |
---|---|---|
event |
String | |
handler |
function |
- Inherited From:
- Source:
setState(state) → {String}
Sets the current state to state
immediately.
Parameters:
Name | Type | Description |
---|---|---|
state |
String |
- Source:
Returns:
- Type
- String
Events
event:after
Fired after an event is dispatched.
Type:
- Object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
currentState |
String | ||
event |
String | ||
previousState |
String | ||
state |
Object | ||
...data |
* |
<repeatable> |
- Source:
event:after-{event}
Fired after a particular event is dispatched.
If the event is foo
, then the event is named before-foo
.
Type:
- Object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
currentState |
String | ||
event |
String | ||
previousState |
String | ||
state |
Object | ||
...data |
* |
<repeatable> |
- Source:
event:after-{state}-{event}
Fired after a particular event is dispatched in a particular state.
If the state is foo
and the event is bar
, then the event is named before-foo-bar
.
Type:
- Object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
currentState |
String | ||
event |
String | ||
previousState |
String | ||
state |
Object | ||
...data |
* |
<repeatable> |
- Source:
event:before
Fired before an event is dispatched.
Type:
- Object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
event |
String | ||
state |
Object | ||
...data |
* |
<repeatable> |
- Source:
event:before-{event}
Fired before a particular event is dispatched.
If the event is foo
, then the event is named before-foo
.
Type:
- Object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
event |
String | ||
state |
Object | ||
...data |
* |
<repeatable> |
- Source:
event:before-{state}-{event}
Fired before a particular event is dispatched in a particular state.
If the state is foo
and the event is bar
, then the event is named before-foo-bar
.
Type:
- Object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
event |
String | ||
state |
Object | ||
...data |
* |
<repeatable> |
- Source:
event:enter
Fired whenever states are entered.
Type:
- Object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
currentState |
String | ||
previousState |
String | ||
...data |
* |
<repeatable> |
- Source:
event:enter-{state}
Fired whenever a particular state is entered.
If the state is foo
, then the event is named enter-foo
.
Type:
- Object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
currentState |
String | ||
previousState |
String | ||
...data |
* |
<repeatable> |
- Source:
event:exit
Fired whenever states are exited.
Type:
- Object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
currentState |
String | ||
nextState |
String | ||
...data |
* |
<repeatable> |
- Source:
event:exit-{state}
Fired whenever a particular state is exited.
If the state is foo
, then the event is named exit-foo
.
Type:
- Object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
currentState |
String | ||
nextState |
String | ||
...data |
* |
<repeatable> |
- Source: