Module: Listen::FSM
- Included in:
- Event::Loop, Listener
- Defined in:
- lib/listen/fsm.rb
Defined Under Namespace
Modules: ClassMethods Classes: State
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Current state of the FSM, stored as a symbol.
Class Method Summary collapse
-
.included(klass) ⇒ Object
Included hook to extend class methods.
Instance Method Summary collapse
-
#initialize_fsm ⇒ Object
NOTE: including classes must call initialize_fsm from their initialize method.
-
#wait_for_state(*wait_for_states, timeout: nil) ⇒ Object
checks for one of the given states to wait for if not already, waits for a state change (up to timeout seconds–
nilmeans infinite) returns truthy iff the transition to one of the desired state has occurred.
Instance Attribute Details
#state ⇒ Object (readonly)
Current state of the FSM, stored as a symbol
48 49 50 |
# File 'lib/listen/fsm.rb', line 48 def state @state end |
Class Method Details
.included(klass) ⇒ Object
Included hook to extend class methods
8 9 10 |
# File 'lib/listen/fsm.rb', line 8 def self.included(klass) klass.send :extend, ClassMethods end |
Instance Method Details
#initialize_fsm ⇒ Object
NOTE: including classes must call initialize_fsm from their initialize method.
40 41 42 43 44 45 |
# File 'lib/listen/fsm.rb', line 40 def initialize_fsm @fsm_initialized = true @state = self.class.start_state @mutex = ::Mutex.new @state_changed = ::ConditionVariable.new end |
#wait_for_state(*wait_for_states, timeout: nil) ⇒ Object
checks for one of the given states to wait for if not already, waits for a state change (up to timeout seconds–nil means infinite) returns truthy iff the transition to one of the desired state has occurred
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/listen/fsm.rb', line 53 def wait_for_state(*wait_for_states, timeout: nil) wait_for_states.each do |state| state.is_a?(Symbol) or raise ArgumentError, "states must be symbols (got #{state.inspect})" end @mutex.synchronize do if !wait_for_states.include?(@state) @state_changed.wait(@mutex, timeout) end wait_for_states.include?(@state) end end |