Module: Cruise

Defined in:
lib/cruise.rb,
lib/cruise/event.rb,
lib/cruise/version.rb,
lib/cruise/watcher.rb,
ext/cruise/cruise.c

Defined Under Namespace

Classes: Event, Watcher

Constant Summary collapse

DEFAULT_DEBOUNCE =
0.1
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.watch(*paths, glob: nil, debounce: DEFAULT_DEBOUNCE, only: nil, callback: nil, &block) ⇒ Object

Watch one or more paths and yield a Cruise::Event for each change.

Blocks until interrupted. Waiting happens on the watcher's pipe via IO#wait_readable, so this releases the GVL for other threads and, when a Fiber scheduler is set (e.g. inside Async), yields to other fibers instead of blocking the reactor.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cruise.rb', line 31

def watch(*paths, glob: nil, debounce: DEFAULT_DEBOUNCE, only: nil, callback: nil, &block)
  callback = block || callback

  raise ArgumentError, "Cruise.watch requires a block or callback" unless callback

  watcher = Watcher.new(*paths, glob: glob, debounce: debounce, only: only)
  io = watcher.io

  begin
    loop do
      io.wait_readable
      closed = drain_wakeups(io)

      while (event = watcher.poll)
        callback.call(event)
      end

      break if closed
    end
  ensure
    watcher.close
  end
end