StateMachine
An immutable class for handling the state of things, where the design is a copy of Rust's sm crate, but with a few additions and changes.
-- Returns a function that constructs an object of the StateMachine class based on the control flow map provided.
local Lock = StateMachine {
TurnKey = {
Locked = 'Unlocked',
Unlocked = 'Locked',
},
Break = {
Locked = 'Broken',
Unlocked = 'Broken',
},
}
-- Starts the machine on the "Locked" state.
local lock = Lock('Locked')
lock = lock:transition('TurnKey')
assert(lock:State(), 'Unlocked')
assert(lock:Trigger():Unwrap(), 'TurnKey')
Functions
transition
Returns a new StateMachine with the post-transition state.
lock
Returns a new StateMachine where the event can no longer be triggered.
note
Locking and unlocking is layer-based, which means that locking twice results in 2 layers, thus, to actually unlock the event, you now have to unlock it 2 times.
unlock
Returns a new StateMachine where the event can be triggered, but only if no lock layer remains.
IsLocked
StateMachine:
IsLocked
(
eventName:Â
string
) →Â
boolean
Returns true if there are 1 or more layers of lock for the event.
State
StateMachine:
State
(
) →Â
string
Returns the state of the machine.
Trigger
StateMachine:
Trigger
(
) →Â
Option
<
string
>
Returns the last triggered event wrapped in an option or option.None if no event has been triggerd yet.
Option's API: https://sleitnick.github.io/RbxUtil/api/Option/
Can
StateMachine:
Can
(
eventName:Â
string
) →Â
boolean
Returns true if the event can be triggered based on the machine's current state.