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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stateObject (readonly)

Current state of the FSM, stored as a symbol


50
51
52
# File 'lib/listen/fsm.rb', line 50

def state
  @state
end

Class Method Details

.included(klass) ⇒ Object

Included hook to extend class methods


10
11
12
# File 'lib/listen/fsm.rb', line 10

def self.included(klass)
  klass.send :extend, ClassMethods
end

Instance Method Details

#initialize_fsmObject

Note: including classes must call initialize_fsm from their initialize method.


42
43
44
45
46
47
# File 'lib/listen/fsm.rb', line 42

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


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/listen/fsm.rb', line 55

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