Class: Vizcore::DSL::FileWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/dsl/file_watcher.rb

Overview

Watches one scene file and invokes a callback when it changes.

Constant Summary collapse

DEFAULT_POLL_INTERVAL =

Default polling interval in seconds when ‘listen` is unavailable.

0.25

Instance Method Summary collapse

Constructor Details

#initialize(path:, poll_interval: DEFAULT_POLL_INTERVAL, listener_factory: nil) {|changed_path| ... } ⇒ FileWatcher

Returns a new instance of FileWatcher.

Parameters:

  • path (String, Pathname)
  • poll_interval (Float) (defaults to: DEFAULT_POLL_INTERVAL)
  • listener_factory (#call, nil) (defaults to: nil)

Yield Parameters:

  • changed_path (Pathname)


16
17
18
19
20
21
22
23
24
# File 'lib/vizcore/dsl/file_watcher.rb', line 16

def initialize(path:, poll_interval: DEFAULT_POLL_INTERVAL, listener_factory: nil, &on_change)
  @path = Pathname.new(path.to_s).expand_path
  @poll_interval = Float(poll_interval)
  @listener_factory = listener_factory
  @on_change = on_change
  @running = false
  @listener = nil
  @thread = nil
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/vizcore/dsl/file_watcher.rb', line 52

def running?
  @running
end

#startBoolean

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'lib/vizcore/dsl/file_watcher.rb', line 27

def start
  return if running?

  @running = true
  start_with_listener || start_with_polling
end

#stop(timeout: 1.0) ⇒ void

This method returns an undefined value.

Parameters:

  • timeout (Float) (defaults to: 1.0)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vizcore/dsl/file_watcher.rb', line 36

def stop(timeout: 1.0)
  return unless running?

  @running = false
  @listener&.stop
  @listener = nil

  thread = @thread
  @thread = nil
  return unless thread
  return if thread == Thread.current

  thread.join(timeout)
end