Class: Listen::Event::Loop

Inherits:
Object
  • Object
show all
Includes:
FSM
Defined in:
lib/listen/event/loop.rb

Constant Summary collapse

Error =
::Listen::Error
NotStarted =

for backward compatibility

::Listen::Error::NotStarted
MAX_STARTUP_SECONDS =
5.0

Instance Attribute Summary

Attributes included from FSM

#state

Instance Method Summary collapse

Methods included from FSM

included, #initialize_fsm, #wait_for_state

Constructor Details

#initialize(config) ⇒ Loop

Returns a new instance of Loop.



22
23
24
25
26
27
# File 'lib/listen/event/loop.rb', line 22

def initialize(config)
  @config = config
  @wait_thread = nil
  @reasons = ::Queue.new
  initialize_fsm
end

Instance Method Details

#pauseObject



60
61
62
63
# File 'lib/listen/event/loop.rb', line 60

def pause
  # TODO: works?
  # fail NotImplementedError
end

#startObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/listen/event/loop.rb', line 42

def start
  # TODO: use a Fiber instead?
  return unless state == :pre_start

  transition! :starting

  @wait_thread = Listen::Thread.new("wait_thread") do
    _process_changes
  end

  Listen.logger.debug("Waiting for processing to start...")

  wait_for_state(:started, timeout: MAX_STARTUP_SECONDS) or
    raise Error::NotStarted, "thread didn't start in #{MAX_STARTUP_SECONDS} seconds (in state: #{state.inspect})"

  Listen.logger.debug('Processing started.')
end

#started?Boolean

Returns:



35
36
37
# File 'lib/listen/event/loop.rb', line 35

def started?
  state == :started
end

#stopObject



65
66
67
68
69
70
# File 'lib/listen/event/loop.rb', line 65

def stop
  transition! :stopped

  @wait_thread&.join
  @wait_thread = nil
end

#stopped?Boolean

Returns:



72
73
74
# File 'lib/listen/event/loop.rb', line 72

def stopped?
  state == :stopped
end

#wakeup_on_eventObject



29
30
31
32
33
# File 'lib/listen/event/loop.rb', line 29

def wakeup_on_event
  if started? && @wait_thread&.alive?
    _wakeup(:event)
  end
end