Caplin Trader 4.7.1

Class: module:caplin/trading/statemachine/StateMachine

module:caplin/trading/statemachine/StateMachine

Constructor

new module:caplin/trading/statemachine/StateMachine(oStateModel)

Constructs a StateMachine that uses the specified StateModel to determine which transitions are possible from the current state. A StateMachine tracks state and manages transitions between them.
Parameters:
Name Type Description
oStateModel module:caplin/trading/statemachine/StateModel The statemodel for this statemachine. May not be null.

Methods

addStateChangedListener(stateChangedListener)

Adds a listener that will be notified any time the state of this statemachine changes.
Parameters:
Name Type Description
stateChangedListener module:caplin/trading/statemachine/StateChangedListener The listener that wishes to be notified of state changes. May not be null or undefined.
Throws:
if the listener is not a non-null instance of StateChangedListener.
Type
module:caplin/core/Error

addTriggerActiveChangedListener(listener, eventName)

Adds a listener that will be notified any time the supplied trigger goes from active to inactive or inactive to active. A trigger is considered active if there is an event that would trigger a transition from the current state given the supplied context.
Parameters:
Name Type Description
listener module:caplin/trading/statemachine/TriggerActiveChangedListener The listener that wishes to be notified of possible state changes. May not be null or undefined.
eventName String The Event that we're interested in. Must be a non-null string.
Throws:
if the listener is not a non-null instance of StateChangedListener.
Type
module:caplin/core/Error

getCurrentState() → {module:caplin/trading/statemachine/State}

Gets the current state that the state machine is in.
Returns:
The current module:caplin/trading/statemachine/State of this Statemachine. Will not be null.
Type
module:caplin/trading/statemachine/State

getName() → {String}

Returns the name that this state machine represents.
Returns:
The state machine name
Type
String

getStateModel() → {String}

Returns the state model the state machine is an instance of.
Returns:
The state model
Type
String

processEvent(sEvent, oContext) → {module:caplin/trading/validation/ValidationResult}

Attempts to change the state to a new state, following a transition defined as being activated by the supplied Event. If the validators pass, it will change state. If they do not pass, the state will remain the same.
Parameters:
Name Type Description
sEvent String The event that triggers a transition to a new state.
oContext Object The data to be validated - indicating whether or not the transition is allowed.
Throws:
if the Event is not a non-null string, If context is null or undefined, if the Event doesn't lead to a state from the current state, or if any of the validators throws an exception.
Type
module:caplin/core/Error
Returns:
The result of checking the validations for the transition associated with this Event.
Type
module:caplin/trading/validation/ValidationResult

removeStateChangedListener(stateChangedListener)

Removes a StateChangedListener.
Parameters:
Name Type Description
stateChangedListener module:caplin/trading/statemachine/StateChangedListener The listener that no longer wishes to be notified of state changes. May not be null or undefined.
Throws:
if the listener is not a non-null instance of StateChangedListener.
Type
module:caplin/core/Error

removeTriggerActiveChangedListener(listener, sEvent)

Removes the specified listener from the list of those to be notified when the specified event triggers a transition from one state to another.
Parameters:
Name Type Description
listener module:caplin/trading/statemachine/TriggerActiveChangedListener The listener that no longer wants to be notified of possible state changes. May not be null or undefined.
sEvent String The Event that we're no longer interested in. Must be a non-null string.
Throws:
if the listener is not a non-null instance of StateChangedListener.
Type
module:caplin/core/Error

testContext(oContext)

Tests a specific context against the current state. If this new context would change the result of calling any validators - i.e. some transitions that under the previously tested context could not be followed can, under the supplied context be followed (or vice versa) - then all the TriggerActiveChangedListeners associated with the Event that would give a different result to before are notified. A concrete example is an execute button. It wants to be told any time the situation of the Execute trigger changes.
Parameters:
Name Type Description
oContext Object the data context. May not be null or undefined.