state()
The State module is exported as a function named state
. This is used either (1) to apply a working state implementation to any owner object; or (2) to define a state expression that declares the content for a State
.
SYNTAX 1
SYNTAX 2
PARAMETERS
owner
opt : objectattributes
opt : string-
expression
opt : ( objectStateExpression
)
RETURNS
-
If an arbitrary
owner
object is provided,state()
bestowsowner
with a new state implementation based on the suppliedexpression
andattributes
, and returns the owner’s initialState
. -
If no
owner
is provided,state()
creates and returns a formalStateExpression
based on the contents ofexpression
andattributes
.
EXAMPLE
DISCUSSION
Inside the definition of a StateExpression
, the following uses of state
are effectively equivalent, each defining a nested empty StateExpression
:
The latter case (C
) is the preferred usage.
SEE ALSO
Utility functions
bind
Causes a function to be contextually bound to the State
in which it acts, providing the means to reliably reference the superstate from within that function.
SYNTAX
PARAMETERS
fn
: function
RETURNS
An object that boxes fn
, marked with a type
of state-bound-function
.
EXAMPLE
DISCUSSION
Normally a state method or event listener will be invoked in the context of the owner object. However, certain patterns may require a function to have a static reference to the State
for which it acts: for example, if a state method wishes to inherit more generic behavior from an implementation located higher in the state tree.
Note that the expression
this.state().superstate
does not provide a lexical reference to the targeted superstate. Because the function may be inherited by a substate, the meaning ofthis.state()
is dependent on the identity of the inheritor, and is therefore dynamic along the superstate axis.
To achieve this, the function must be wrapped in a call to state.bind
, which boxes the function inside a specially typed object. Thenceforth whenever State needs to use this function, it will be recognized in its boxed form as state-bound, and then automatically unboxed and invoked in the context of the prevailing State
.
The owner object, meanwhile, although no longer referenced directly as this
, is still reliably available as this.owner
.
If a state-bound method, event listener, etc. is inherited from a protostate, then the prevailing State
will be the inheriting epistate. To capture a reference to the precise State
in which a function is defined, it must be wrapped with state.fix
.
SEE ALSO
fix
Causes a function to be decorated with fixed bindings that indicate the precise State
in which the function is defined. This provides a reliable means to reference the lexical protostate relative to that function’s definition.
SYNTAX
PARAMETERS
fn
: (autostate
,protostate
opt ) → function
RETURNS
An object that boxes the decorated function returned by fn
, marked with a type
of state-fixed-function
.
EXAMPLE
DISCUSSION
For a function to reliably access either the State
in which it is defined, or important related State
s such as its protostate, the function must be lexically bound to its host State
by enclosing it within a decorator, and wrapping this in a call to state.fix
.
The decorator is provided as a function that defines parameters autostate
and optionally protostate
, and returns the function fn
that is to be fixed. Calling fix
then boxes the decorator inside a specially typed object. Thenceforth whenever State implements this function as a method, event listener, etc. for a State
, it will recognize the object as a state-fixed function, which will be automatically unboxed and partially applied with the host State
as autostate
, and its immediate protostate as protostate
.
The fixed, enclosed fn
is thusly bestowed with full lexical awareness of the particular State
environment in which it exists.
SEE ALSO
own
Ensures that, for a given owner
, the State
returned by a queried selector
is both real and not an inherited protostate.
SYNTAX
PARAMETERS
owner
: objectselector
: string-
expr
opt : ( objectStateExpression
)
RETURNS
Either the new real epistate, or null
if selector
does not identify a State
that is heritable by owner
.
DESCRIPTION
Causes the inherited protostate or virtual epistate identified by selector
to be realized, if necessary, within the state tree of owner
. If a realization does occur, the new epistate can be augmented by the optional expr
.
EXAMPLE
- Unexpected: the
enter
event listener is added to stateA
ofp
, noto
. The incipient instanceo
inherits its entire state tree fromp
, soo.state('A')
is equal top.state('A')
.
- Calling
state.own(o,'A')
instead ofo.state('A')
ensures that the returnedState
is real (not virtual) and that itsowner
iso
(not a prototype). The event listener will be held in the state tree ofo
, as expected.
SEE ALSO
extend
Defines a state expression that declares one or more paths to parastates from which the expressed State
will inherit.
SYNTAX
PARAMETERS
parastates
: string — Comma separated list of selector paths to parastates.attributes
opt : string-
expression
opt : ( objectStateExpression
)
RETURNS
A StateExpression
with a corresponding parastates
property.
EXAMPLE
SEE ALSO
State
A State
models a set of behaviors on behalf of an owner object. The owner may undergo transitions that change its current state from one to another, and in so doing adopt a different set of behaviors.
Distinct behaviors are modeled in each state by defining method overrides, to which calls made on the owner will be redirected so long as a state remains current.
States are structured as a rooted tree, where substates inherit from a single superstate. While a substate is current, it and all of its ancestor superstates are active.
In addition, a state also recognizes the owner object’s prototypal inheritance, identifying an identically named and positioned state in the prototype as its protostate. Stateful behavior is inherited from protostates first, then from superstates.
SEE ALSO
Properties
name
The string name of this
state. For the unique case of a RootState
, this value will be the empty string ''
.
SYNTAX
EXAMPLE
SEE ALSO
owner
References the object that is the owner of this
state.
SYNTAX
EXAMPLE
SEE ALSO
root
References the RootState
of the state tree to which this
state belongs.
SYNTAX
EXAMPLE
SEE ALSO
superstate
References the State
that is this
state’s immediate superstate.
SYNTAX
EXAMPLE
SEE ALSO
protostate
References the State
that is this
state’s immediate protostate.
SYNTAX
EXAMPLE
SEE ALSO
Attributes
State attributes are added to a state expression by preceding the expression
argument of a call to state()
with a space-delimited string argument that names the attributes to be applied.
EXAMPLE
SEE ALSO
mutable
By default, states are weakly immutable — their data, methods, guards, substates, and transitions cannot be altered once the state has been constructed. Applying the mutable
attribute lifts the restriction of immutability, allowing the use of methods such as mutate
, addMethod
, addSubstate
, etc., which can be used to alter the contents of the state.
NOTES
The mutable
attribute is inherited from both superstates and protostates, unless any also bear the immutable
attribute.
EXAMPLE
- Without a
mutable
attribute, the states ofMover.prototype
are weakly immutable, so their content cannot be altered, and the attempt to callmutate
has no effect.
- Because the
mover
instance’s root state was declaredmutable
, content can be added later to that state or to any state that inherits from it.
- Substates, including virtual states and any state added later, automatically inherit the
mutable
attribute assigned to their superstates.
SEE ALSO
finite
Declaring a state finite
guarantees its hierarchical structure by disabling its addSubstate
and removeSubstate
methods after the state has been constructed.
NOTES
The finite
attribute is inherited from both superstates and protostates, and is imposed with higher precedence than mutable
.
EXAMPLE
- The
mover
instance may define new states of its own, but its states will also inherit thefinite
attribute fromMover.prototype
.
- Even though
mover
’sRunning
state is declaredmutable
, it is subject to the restrictions of its inheritedfinite
attribute, sincefinite
is more powerful thanmutable
. Therefore an attempt to add a substate later will fail.
SEE ALSO
immutable
Adding immutable
makes a state strongly immutable, whereupon immutability is permanent and absolute: immutable
contradicts and overrules mutable
, and implies finite
, irrespective of whether any of the attributes are literal or inherited.
NOTES
The immutable
attribute is inherited from both superstates and protostates, and has top precedence over mutable
and finite
.
An inheriting owner object may still extend the state implementation of its prototype with states that are new, or that extend protostates, but any of these that inherit from an immutable
state will implicitly also bear the immutable
attribute themselves.
EXAMPLE
- The
mover
instance was declaredmutable
, but its prototype isimmutable
, so applyingmutable
on the inheriting root state here is superfluous:mutable
is negated andimmutable
is inherited.
- The call to
state
does allowmover
to extend its prototype’simmutable
state, although the newStationary
state will also inheritimmutable
.
- Attempts to alter the state’s contents after it has been constructed will have no effect.
SEE ALSO
abstract
A state that is abstract
cannot itself be current. Consequently a transition that targets an abstract state will be forcibly redirected to the appropriate concrete
descendant of the abstract state.
The redirection target of an abstract
state is determined by seeking its first-iterated substate marked default
. If no default
substate exists, the first substate is targeted. If the redirection target is itself abstract, then the process is repeated until a concrete descendant is found. If an abstract state has no concrete descendants, currency is directed as deep as possible via the first substate at each level.
As object keys are not strictly ordered, it is a best practice to ensure that, whether literally or via inheritance, exactly one substate of an
abstract
state is always named as itsdefault
.
NOTES
The abstract
attribute is inherited from protostates.
EXAMPLE
SEE ALSO
concrete
Including the concrete
attribute will override the abstract
attribute that would otherwise have been inherited from an abstract protostate.
Any state that is not abstract is by definition concrete, even if not literally attributed as such, and will return true
in a call to isConcrete
.
NOTES
The concrete
attribute is inherited from protostates.
EXAMPLE
SEE ALSO
default
Marking a state default
designates it as the intended redirection target for any transition that has targeted its abstract
superstate.
NOTES
The default
attribute is inherited from protostates.
EXAMPLE
SEE ALSO
initial
Marking a state initial
specifies which state is to be assumed immediately following the state()
application. No transition or any enter
or arrive
events result from this initialization.
For an object that inherits its entire state implementation from its prototype, the inheritor’s initial state will be set to the prototype’s current state.
NOTES
The initial
attribute is inherited from protostates.
EXAMPLE
SEE ALSO
conclusive
Once a conclusive
state is entered, it cannot be exited, although transitions may still freely traverse within its substates.
NOTES
The conclusive
attribute is inherited from protostates.
EXAMPLE
SEE ALSO
final
Once an object’s currency arrives at a final
state, no further transitions are allowed.
A final
state is not necessarily conclusive
: a transition may enter a final
state on its way to a descendant, and still exit from it later, so long as it never arrives at the final
state.
NOTES
The final
attribute is inherited from protostates.
EXAMPLE
SEE ALSO
Methods
realize
Transforms this
virtual state into a “real” state that can bear content of its own.
SYNTAX
PARAMETERS
-
expression
opt : ( objectStateExpression
)
RETURNS
this
EXAMPLES
If this
is already real instead of virtual, then calling realize
has no effect.
If this
is both virtual
and mutable
, then calling any of its add*
methods necessarily uses realize
to transform into a real state before content is added.
SEE ALSO
Protostates and epistates Virtual epistates
State realize
State::realize
destroy
Attempts to cleanly destroy this
state and all of its descendant states.
SYNTAX
RETURNS
true
if this
state is successfully destroyed, or false
otherwise.
NOTES
A destroy
event is issued by each state as it is destroyed.
If the root state is destroyed, the owner is given back any methods it bore prior to its state implementation.
SEE ALSO
express
Produces an object containing an expression of the contents of this
state, such as would be sufficient to create a new State
structurally identical to this
.
SYNTAX
PARAMETERS
typed = false
: boolean
RETURNS
The generated plain-object, or equivalent StateExpression
if typed
is true
.
EXAMPLE
SEE ALSO
mutate
Transactionally mutates this
state by adding, updating, or removing items as implied by the contents of expression
.
SYNTAX
PARAMETERS
-
expression
: (StateExpression
object )
RETURNS
this
NOTES
Property removal is indicated with a value equal to the unique O.NIL
reference.
If the transaction causes a mutation, this
emits a mutate
event.
EXAMPLE
SEE ALSO
substate
Identifies a named substate of this
.
SYNTAX
PARAMETERS
stateName
: stringvia = VIA_PROTO
: boolean
RETURNS
The substate of this
state named stateName
. If no such substate exists locally within this
, and the VIA_PROTO
bit of via
is set, then the nearest identically named substate held on a protostate will be returned.
SEE ALSO
substates
Generates a collection of the immediate substates of this
.
SYNTAX
PARAMETERS
virtual = false
: boolean
RETURNS
An Array
of this
state’s substates.
NOTES
If virtual
is true
, the returned array may include any active virtual states held by an owner object that is inheriting currency from a prototype.
SEE ALSO
descendants
Generates a depth-first flattened collection of this
state’s substates, their respective substates, etc.
SYNTAX
PARAMETERS
virtual = false
: boolean
RETURNS
An Array
that includes all of this
state’s descendant states.
NOTES
If virtual
is true
, the returned array may include any active virtual states held by an owner object that is inheriting currency from a prototype.
SEE ALSO
addSubstate
Creates a State
based on the provided stateExpression
, adds it as a substate of this
state.
SYNTAX
PARAMETERS
stateName
: string-
stateExpression
: (StateExpression
object State
)
RETURNS
The new State
.
NOTES
If a substate with the same stateName
already exists, it is first destroyed and then replaced.
SEE ALSO
removeSubstate
Removes the substate named by stateName
from this
state, if possible.
SYNTAX
PARAMETERS
stateName
: string
RETURNS
The removed State
.
NOTES
If the owner object is in the midst of a transition involving the state targeted for removal, then the removal will fail, returning false
.
SEE ALSO
derivation
Describes the superstate chain of this
as an array.
SYNTAX
PARAMETERS
byName = false
: boolean
RETURNS
An Array
containing each State
from the root state to this
state, starting with the immediate substate from the root.
If byName
is true
, the returned Array
contains the string names of each state, rather than the State
s themselves.
EXAMPLE
SEE ALSO
path
Describes the superstate chain of this
as a dot-delimited string.
SYNTAX
RETURNS
A string that matches the absolute selector referencing this
state.
EXAMPLE
SEE ALSO
depth
Quantifies the height of the superstate chain of this
.
SYNTAX
RETURNS
The number of superstates separating this
state from its root state.
EXAMPLE
SEE ALSO
common
Establishes the hierarchical relation between this
and another State
.
SYNTAX
PARAMETERS
-
other
: (State
string )
RETURNS
The State
that is the nearest common ancestor of both this
state and the provided other
state.
EXAMPLE
SEE ALSO
is
Asserts identity.
SYNTAX
PARAMETERS
-
other
: (State
string )
RETURNS
A boolean indicating whether this
state is the provided other
state.
EXAMPLE
SEE ALSO
isIn
Asserts descendant familiarity.
SYNTAX
PARAMETERS
-
other
: (State
string )
RETURNS
A boolean indicating whether this
state is or is a substate of the provided other
state.
EXAMPLE
SEE ALSO
hasSubstate
Asserts ancestral familiarity.
SYNTAX
PARAMETERS
-
other
: (State
string )
RETURNS
A boolean indicating whether this
state is or is a superstate of the provided other
state.
EXAMPLE
SEE ALSO
isSuperstateOf
Asserts hierarchical ancestry.
SYNTAX
PARAMETERS
-
other
: (State
string )
RETURNS
A boolean indicating whether this
state is a superstate of the provided other
state.
EXAMPLE
SEE ALSO
defaultSubstate
Resolves the proper concretion for an abstract state.
SYNTAX
PARAMETERS
via = VIA_PROTO
: number
RETURNS
The State
that is this
state’s first substate bearing the default
attribute, or just the first substate if none are found.
EXAMPLE
Moving
is explicitly markeddefault
.
- Since
Moving
, which is itself abstract, has no descendant states markeddefault
, its first substateWalking
serves as its default state.
- A transition targeting the root state will fall through to
Walking
, since both the root and its default stateMoving
are abstract.
SEE ALSO
initialSubstate
SYNTAX
RETURNS
The State
that is this
state’s most deeply nested state bearing the initial
attribute, by way of its greatest initial
descendant state.
SEE ALSO
getProtostate
Performs the lookup to identify the State
analogous to this
that is owned by a prototype of the owner
.
SYNTAX
RETURNS
The protostate of this
: that State
which both has a derivation path
identical to the path
of this
, and whose owner
is the nearest possible prototype of the owner
of this
.
Returns undefined
if no protostate exists anywhere in the owner’s prototype chain.
EXAMPLE
SEE ALSO
isProtostateOf
Asserts prototypal ancestry of an other
State
relative to this
; i.e., whether an other
State
has an identical path
to this
and the owner
of other
is a prototype of the owner
of this
.
SYNTAX
PARAMETERS
-
other
: (State
string )
RETURNS
A boolean indicating whether this
state is a protostate of the provided other
state.
EXAMPLE
SEE ALSO
query
Matches a selector
string with the state or states it represents in the context of this
state.
SYNTAX
PARAMETERS
selector
: stringagainst
opt :State
via = VIA_ALL
: number
The via
parameter is a bit-field integer comprised of one or more of the TRAVERSAL_FLAGS
constants: [VIA_SUB
, VIA_SUPER
, VIA_PROTO
].
By default via
is VIA_ALL
(~0
), which implies each of the flags’ bits are set, and consequently that the query
operation will be recursed over the substates, superstates, and protostates, in order, of this
. Providing a via
argument that zeroes any of the VIA_SUB
, VIA_SUPER
, or VIA_PROTO
bits will disable recursion through the substates, superstates, or protostates, respectively, of this
.
RETURNS
The nearest matching State
, or if a non-specific selector
is provided, an Array
containing the set of matched states. If a state to be tested against
is provided, then a boolean is returned, indicating whether against
is the matched state itself or is included in the matching set.
NOTES
Calling an owner object’s accessor method with a selector string invokes query
on the owner’s current state.
SEE ALSO
$
Convenience method that mimics the behavior of the owner’s accessor method.
SYNTAX
PARAMETERS
selector
: string
RETURNS
If the first argument is a transition arrow selector string, the call is aliased to change
. If passed a plain selector string, the call is aliased to query
.
EXAMPLES
Aliases to change
, instigating a transition to the Awake
state.
Aliases to query
, returning the State
named 'Awake'
.
current
Returns the current State
of this
state’s owner.
SYNTAX
RETURNS
State
.
isCurrent
Indicates whether this
state is the owner’s current state.
SYNTAX
RETURNS
Boolean.
isActive
Indicates whether this
state or one of its substates is the owner’s current state.
SYNTAX
RETURNS
Boolean.
change
Attempts to execute a state transition.
ALIASES
go, be
SYNTAX
PARAMETERS
-
target
: (State
string ) options
opt : object
The target
parameter may be either a State
object within the purview of this controller, or a string that resolves to a likewise targetable State
when evaluated from the context of the most recently current state.
The options
parameter is an optional map that may include:
args
:Array
— arguments to be passed to a transition’saction
function.success
: function — callback to be executed upon successful completion of the transition.failure
: function — callback to be executed if the transition attempt is blocked by a guard.
DISCUSSION
Handles asynchronous transitions, generation of appropriate events, and construction of any necessary temporary virtual states. Respects guards supplied in both the origin and target
states.
isVirtual
Indicates whether this
state bears the virtual
attribute.
SYNTAX
RETURNS
Boolean.
DISCUSSION
A virtual state is a lightweight inheritor of a protostate located higher in the owner object’s prototype chain. Notably, as virtual states are created automatically, no modifier keyword exists for the virtual
attribute.
EXAMPLE
- The
mover
instance, which inherits its stateful implementation fromMover.prototype
, does not have a realStationary
state of its own, so a virtualStationary
state is created automatically and adopted asmover
’s initial state.
- Root states are never virtualized. Even an object that inherits all statefulness from its prototypes is given a real root state.
SEE ALSO
isMutable
Indicates whether this
state bears the mutable
attribute.
SYNTAX
RETURNS
Boolean.
DISCUSSION
By default, states are weakly immutable; i.e., once a State
has been constructed, its declared data, methods, guards, substates, and transitions cannot be altered. By including the mutable
attribute in the state’s expression, this restriction is lifted. Mutability is also inherited from any of a state’s superstates or protostates.
SEE ALSO
isFinite
Indicates whether this
state bears the finite
attribute.
SYNTAX
RETURNS
Boolean.
DISCUSSION
If a state is declared finite
, no substates or descendant states may be added, nor may any be removed without also destroying the state itself.
SEE ALSO
isImmutable
Indicates whether this
state bears the immutable
attribute.
SYNTAX
RETURNS
Boolean.
DISCUSSION
A literal or inherited immutable
attribute causes a state to become strongly immutable, wherein it guarantees immutability absolutely, throughout all inheriting states. The immutable
attribute also implies finite
, and contradicts and overrides any literal or inherited mutable
attribute.
SEE ALSO
isAbstract
Indicates whether this
state is abstract
.
SYNTAX
RETURNS
Boolean.
DISCUSSION
An abstract
state is used only as a source of inheritance, and cannot itself be current. A transition that directly targets an abstract state will be automatically redirected to one of its substates.
SEE ALSO
isConcrete
Indicates whether this
state is concrete
.
SYNTAX
RETURNS
Boolean.
DISCUSSION
All non-abstract states are concrete. Marking a state with the concrete
attribute in a state expression will override any abstract
attribute, particularly such as would otherwise be inherited from a protostate.
SEE ALSO
isDefault
Indicates whether this
state bears the default
attribute.
SYNTAX
RETURNS
Boolean.
DISCUSSION
Marking a state default
designates it as the specific redirection target for any transition that targets its abstract superstate.
SEE ALSO
isInitial
Indicates whether this
state bears the initial
attribute.
SYNTAX
RETURNS
Boolean.
DISCUSSION
Marking a state initial
specifies which state a newly stateful object should assume.
Objects inheriting from a stateful prototype will have their initial state set to the prototype’s current state.
SEE ALSO
isConclusive
Indicates whether this
state bears the conclusive
attribute.
SYNTAX
RETURNS
Boolean.
DISCUSSION
Once a state marked conclusive
is entered, it cannot be exited, although transitions may still freely traverse within its substates.
SEE ALSO
isFinal
Indicates whether this
state bears the final
attribute.
SYNTAX
RETURNS
Boolean.
DISCUSSION
Once a state marked final
is entered, no further outbound transitions within its local region are allowed.
SEE ALSO
data (read)
Reads a composite of the data
assigned to this
state.
SYNTAX
PARAMETERS
via = VIA_ALL
: number
RETURNS
An object clone of the data attached to this
state, including any data inherited from protostates and superstates, unless specified otherwise by zeroing the VIA_SUPER
and VIA_PROTO
bits of via
.
SEE ALSO
data (write)
Adds, updates, and/or removes data
properties on this
state.
SYNTAX
PARAMETERS
edit
: object
RETURNS
this
.
NOTES
For any keys in edit
whose values are set to the O.NIL
directive, the matching properties are deleted from this
state’s data.
If the operation results in a change to this
state’s data, a mutate
event is emitted.
SEE ALSO
has
Determines whether a data
property with the given key
exists on this
state, or is inherited from a protostate or superstate.
SYNTAX
PARAMETERS
key
: stringvia = VIA_ALL
: number
NOTES
Supports long.key.path
lookups for deeply nested properties.
SEE ALSO
get
Reads a data
item on this
state.
SYNTAX
PARAMETERS
key
: stringvia = VIA_ALL
: number
RETURNS
The value of the data
property with the given key
on this
state, or one inherited from the nearest protostate, or the nearest superstate.
NOTES
Supports long.key.path
lookups for deeply nested properties. Returns undefined
if key
cannot be resolved.
let
Writes a data
item on this
state.
SYNTAX
PARAMETERS
key
: stringvalue
: var
RETURNS
If successful, the assigned value
.
NOTES
Creates a new data property or updates an existing data property on this
state.
Succeeds only if this
state is mutable
.
Supports long.key.path
assignments to deeply nested properties.
SEE ALSO
set
Writes or updates an existing data
item on either this
state or a superstate.
SYNTAX
PARAMETERS
key
: stringvalue
: var
RETURNS
The assigned value
.
DISCUSSION
If the property is inherited from a mutable
superstate, then the property is updated in place, equivalent to calling let
on that superstate. If the data property does not yet exist in the superstate chain, it is created on this
. Properties inherited from protostates are not affected.
NOTES
Supports long.key.path
assignments to deeply nested properties.
SEE ALSO
delete
Deletes an existing data
property on this
state.
SYNTAX
PARAMETERS
key
: string
RETURNS
Boolean true
if the deletion was successful or unnecessary, or false
otherwise, in the same manner as the native delete
operator.
NOTES
Supports long.key.path
lookups for deeply nested properties.
SEE ALSO
method
SYNTAX
PARAMETERS
methodName
: stringvia = VIA_ALL
: numberout
opt : object
RETURNS
The function that is the method held on this
state whose name is methodName
.
DISCUSSION
If the named method does not exist on this
state, then it will be inherited, in order, first from protostates of this
(if the VIA_PROTO
bit of via
is set), and if no such method exists there, then from superstates of this
(if the VIA_SUPER
bit of via
is set).
If an out
object is supplied, then the returned function
is attached to out.method
, and the State
context to which the method will be bound when invoked with this.apply
or this.call
is attached to out.context
.
SEE ALSO
methodNames
SYNTAX
RETURNS
An Array
of names of methods defined locally on this
state.
SEE ALSO
addMethod
Adds fn
as a method named methodName
to this
state, which will be callable directly from the owner, but with its context bound to this
.
SYNTAX
PARAMETERS
methodName
: stringfn
: function
RETURNS
fn
.
SEE ALSO
removeMethod
Dissociates the method named methodName
from this
state and returns its function.
SYNTAX
PARAMETERS
methodName
: string
SEE ALSO
hasMethod
Indicates whether this
state possesses or inherits a method named methodName
.
SYNTAX
PARAMETERS
methodName
: string
RETURNS
Boolean.
SEE ALSO
hasOwnMethod
Indicates whether this
state directly possesses a method named methodName
.
SYNTAX
PARAMETERS
methodName
: string
RETURNS
Boolean.
SEE ALSO
apply
Invokes a state method, passing an array of arguments.
SYNTAX
PARAMETERS
methodName
: stringargs
opt :Array
RETURNS
The value returned by the invocation of the named method, or undefined
if no such method can be invoked.
DISCUSSION
If the state method named by methodName
exists locally or can be inherited via protostate or superstate, that function is applied with the provided args
in the appropriate context, and its result is returned.
By default the context will be the owner. If the method was defined using state.bind, the context will be either the precise State
in which the method is defined, or if the method is inherited from a protostate, the corresponding epistate belonging to the inheriting owner.
If the named method does not exist and cannot be inherited, a noSuchMethod
event is emitted and the call returns undefined
.
SEE ALSO
call
The variadic companion to apply
, where a state method is invoked with individually provided arguments.
SYNTAX
PARAMETERS
methodName
: stringargs...
: individual arguments
SEE ALSO
event
SYNTAX
PARAMETERS
eventType
: string-
id
opt : ( stringnumber function )
RETURNS
A registered event listener function, or the number of listeners registered, for a given eventType
.
If an id
as returned by addEvent
is provided, the event listener associated with that id
is returned. If no id
is provided, the number of event listeners registered to eventType
is returned.
SEE ALSO
addEvent
Binds an event listener fn
to the specified eventType
.
ALIASES
on, bind
SYNTAX
PARAMETERS
eventType
: stringfn
: functioncontext = this
: object
RETURNS
A unique identifier for the listener.
SEE ALSO
removeEvent
Unbinds the event listener with either the specified id
that was supplied by addEvent
, or a direct reference to that function.
ALIASES
off, unbind
SYNTAX
PARAMETERS
eventType
: string-
id
: ( stringnumber function )
SEE ALSO
emit
Invokes all listeners bound to the given eventType
.
ALIASES
trigger
SYNTAX
PARAMETERS
eventType
: stringargs = []
:Array
context = this
: objectvia = VIA_ALL
: number
Arguments for the listeners can be passed as an array to the args
parameter.
NOTES
Listeners are invoked in the context of this
state, or as specified by context
.
Listeners bound to superstates and protostates of this
are also invoked, unless otherwise directed by zeroing the VIA_SUPER
or VIA_PROTO
bits of via
.
SEE ALSO
guard
Describes the guards in effect for this
state.
SYNTAX
PARAMETERS
guardType
: string
RETURNS
An object containing the guard predicates and/or expressions for the specified guardType
held on this
state.
DISCUSSION
A guard is a map of functions or values that will be evaluated as either a predicate or boolean expression, respectively, to provide a determination of whether the owner’s currency will be admitted into or released from the state to which the guard is applied.
Valid guardType
s include admit
and release
.
NOTES
Guards are inherited from protostates, but not from superstates.
SEE ALSO
addGuard
Adds a guard to this
state, or augments an existing guard with additional entries.
SYNTAX
PARAMETERS
guardType
: stringguard
: object
SEE ALSO
removeGuard
Removes a guard from this
state, or removes specific entries from an existing guard.
SYNTAX
PARAMETERS
guardType
: string-
keys
opt : (Array
string )
SEE ALSO
transition
SYNTAX
PARAMETERS
transitionName
: string
RETURNS
The transition expression named by transitionName
registered to this
state.
transitions
SYNTAX
RETURNS
An object containing all of the transition expressions registered to this
state.
addTransition
Registers a transition expression to this
state.
SYNTAX
PARAMETERS
transitionName
: string-
transitionExpression
: (TransitionExpression
object )
removeTransition
Removes a registered transition expression from this
state.
SYNTAX
PARAMETERS
transitionName
: string
Events
construct
SYNTAX
PARAMETERS
-
expression
: (StateExpression
object )
DESCRIPTION
Immediately after a State
instance has been fully constructed, it emits a construct
event.
Listeners receive the expression
object from which the state was constructed.
Since construction is not complete until the state’s substates have themselves been constructed, the full construct
event sequence of a state tree proceeds bottom-up.
EXAMPLE
SEE ALSO
destroy
SYNTAX
DESCRIPTION
A state is properly deallocated with a call to the destroy
method of either itself or a superstate. The destroy
event is emitted immediately prior to the state and its contents being cleared.
Listeners of destroy
are called with no arguments.
SEE ALSO
depart
SYNTAX
PARAMETERS
transition
:Transition
DESCRIPTION
At the beginning of a transition, exactly one depart
event is always emitted by the state from which the transition originates.
Listeners receive a reference to the involved transition
.
SEE ALSO
exit
SYNTAX
PARAMETERS
transition
:Transition
DESCRIPTION
During the ascending phase of a transition, an exit
event is emitted by the origin state and any of its superstates that will no longer be active as a result of the transition.
Listeners receive a reference to the involved transition
.
SEE ALSO
enter
SYNTAX
PARAMETERS
transition
:Transition
DESCRIPTION
During the descending phase of a transition, an enter
event is emitted by each state that will become newly active, including the target state.
Listeners receive a reference to the involved transition
.
SEE ALSO
arrive
SYNTAX
PARAMETERS
transition
:Transition
DESCRIPTION
At the end of a transition, exactly one arrive
event is always emitted by the transition’s target state.
Listeners receive a reference to the involved transition
.
SEE ALSO
mutate
SYNTAX
PARAMETERS
mutation
: objectresidue
: objectbefore
: objectafter
: object
DESCRIPTION
When a state’s contents are altered, it emits a mutate
event containing the changes made relative to its immediately prior condition.
Listeners receive the contents of the mutation
experienced by the state, the residue
containing the contents displaced by the mutation, and a full expression of the state’s contents both before
and after
the mutation.
SEE ALSO
noSuchMethod
SYNTAX
PARAMETERS
methodName
: stringargs
:Array
DESCRIPTION
When a method is called on an object for which no implementation exists given its current state, a noSuchMethod
event is emitted.
Listeners receive the methodName
of the method that was called, and an args
array of the arguments that were passed to the call.
SEE ALSO
noSuchMethod:name
SYNTAX
PARAMETERS
args...
:var
DESCRIPTION
A generic noSuchMethod
event is immediately followed by the emission of a specific noSuchMethod:name
event, where name
specifies the method that was called.
Listeners receive the arguments as they were passed to the call.
SEE ALSO
Transition
A Transition
is a transient State
temporarily adopted as the owner object’s current state as it changes from one of its proper State
s to another.
A transition acts within the domain of the least common ancestor between its origin and target states. During this time it behaves as if it were a substate of that domain state, inheriting method calls and propagating events in the familiar fashion.
Properties
owner
References the State
to which this
transition is currently attached during its traversal from origin
through domain
toward target
.
root
References the State
to which this
transition is currently attached during its traversal from origin
to target
.
superstate
References the State
to which this
transition is currently attached during its traversal from origin
through domain
toward target
.
origin
References the owner
’s most recently current State
that is not itself a Transition
— i.e., the state from which this
transition originally emerged.
source
References the State
or Transition
that was current immediately prior to this
transition.
target
References the intended destination State
for this
transition.
Methods
wasAborted
Returns false
if this
transition has reached its target
and completed successfully.
Returns true
if this
was aborted prior to reaching target
.
Returns undefined
if this
has not yet been completed or aborted.
start
Automatically invoked as a result of a call to State.change
.
end
Used to signal the end of this
transition’s action
.
Upon reaching the top of its domain, a transition will invoke its action function, which is responsible for calling end()
to signal that it is finished and ready for the transition to begin the descending phase of the traversal to target
.
Events
enter
Immediately after currency is transferred to the transition from the source state, the transition emits an enter
event.
start
args...
:var
In the change
call that instigated a transition, if the options
argument included an args
array, then these elements of options.args
are passed as arguments to the transition’s start
method, which relays them to listeners of the start
event.
end
args...
:var
A transition’s action phase is ended by calling its end
method, and any arguments it receives are relayed to listeners of the transition’s end
event.
If the transition bears no action, then arguments relayed to listeners of the transition’s start
event are also relayed to listeners of the end
event, which is emitted immediately after start
.
exit
Just before currency is transferred from a transition to its target state, an exit
event is emitted by the transition.