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.



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

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

Instance Method Details

#pauseObject



62
63
64
65
# File 'lib/listen/event/loop.rb', line 62

def pause
  # TODO: works?
  # fail NotImplementedError
end

#startObject



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

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:

  • (Boolean)


37
38
39
# File 'lib/listen/event/loop.rb', line 37

def started?
  state == :started
end

#stopObject



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

def stop
  transition! :stopped

  @wait_thread&.join
  @wait_thread = nil
end

#stopped?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/listen/event/loop.rb', line 74

def stopped?
  state == :stopped
end

#wakeup_on_eventObject



31
32
33
34
35
# File 'lib/listen/event/loop.rb', line 31

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