Class: Listen::Adapter
- Inherits:
-
Object
- Object
- Listen::Adapter
- Defined in:
- lib/listen/adapter.rb
Direct Known Subclasses
Listen::Adapters::Darwin, Listen::Adapters::Linux, Listen::Adapters::Polling, Listen::Adapters::Windows
Constant Summary collapse
- DEFAULT_LATENCY =
The default delay between checking for changes.
0.1
- POLLING_FALLBACK_MESSAGE =
The default warning message when falling back to polling adapter.
"WARNING: Listen has fallen back to polling, learn more at https://github.com/guard/listen#fallback."
Instance Attribute Summary collapse
-
#directories ⇒ Object
Returns the value of attribute directories.
-
#latency ⇒ Object
Returns the value of attribute latency.
-
#paused ⇒ Object
Returns the value of attribute paused.
Class Method Summary collapse
-
.select_and_initialize(directories, options = {}) {|changed_dirs, options| ... } ⇒ Listen::Adapter
Selects the appropriate adapter implementation for the current OS and initializes it.
-
.usable_and_works?(directories, options = {}) ⇒ Boolean
Checks if the adapter is usable and works on the current OS.
-
.works?(directory, options = {}) ⇒ Boolean
Runs a tests to determine if the adapter can actually pick up changes in a given directory and returns the result.
Instance Method Summary collapse
-
#initialize(directories, options = {}) {|changed_dirs, options| ... } ⇒ Listen::Adapter
constructor
Initializes the adapter.
-
#start(blocking = true) ⇒ Object
Starts the adapter.
-
#stop ⇒ Object
Stops the adapter.
-
#wait_for_callback ⇒ Object
Blocks the main thread until the poll thread calls the callback.
Constructor Details
#initialize(directories, options = {}) {|changed_dirs, options| ... } ⇒ Listen::Adapter
Initializes the adapter.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/listen/adapter.rb', line 60 def initialize(directories, = {}, &callback) @directories = Array(directories) @callback = callback @latency ||= DEFAULT_LATENCY @latency = [:latency] if [:latency] @paused = false @mutex = Mutex.new @changed_dirs = Set.new @turnstile = Turnstile.new end |
Instance Attribute Details
#directories ⇒ Object
Returns the value of attribute directories.
8 9 10 |
# File 'lib/listen/adapter.rb', line 8 def directories @directories end |
#latency ⇒ Object
Returns the value of attribute latency.
8 9 10 |
# File 'lib/listen/adapter.rb', line 8 def latency @latency end |
#paused ⇒ Object
Returns the value of attribute paused.
8 9 10 |
# File 'lib/listen/adapter.rb', line 8 def paused @paused end |
Class Method Details
.select_and_initialize(directories, options = {}) {|changed_dirs, options| ... } ⇒ Listen::Adapter
Selects the appropriate adapter implementation for the current OS and initializes it.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/listen/adapter.rb', line 31 def self.select_and_initialize(directories, = {}, &callback) return Adapters::Polling.new(directories, , &callback) if .delete(:force_polling) if Adapters::Darwin.usable_and_works?(directories, ) Adapters::Darwin.new(directories, , &callback) elsif Adapters::Linux.usable_and_works?(directories, ) Adapters::Linux.new(directories, , &callback) elsif Adapters::Windows.usable_and_works?(directories, ) Adapters::Windows.new(directories, , &callback) else unless [:polling_fallback_message] == false Kernel.warn([:polling_fallback_message] || POLLING_FALLBACK_MESSAGE) end Adapters::Polling.new(directories, , &callback) end end |
.usable_and_works?(directories, options = {}) ⇒ Boolean
Checks if the adapter is usable and works on the current OS.
101 102 103 |
# File 'lib/listen/adapter.rb', line 101 def self.usable_and_works?(directories, = {}) usable? && Array(directories).all? { |d| works?(d, ) } end |
.works?(directory, options = {}) ⇒ Boolean
This test takes some time depending the adapter latency.
Runs a tests to determine if the adapter can actually pick up changes in a given directory and returns the result.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/listen/adapter.rb', line 116 def self.works?(directory, = {}) work = false test_file = "#{directory}/.listen_test" callback = lambda { |changed_dirs, | work = true } adapter = self.new(directory, , &callback) adapter.start(false) FileUtils.touch(test_file) t = Thread.new { sleep(adapter.latency * 5); adapter.stop } adapter.wait_for_callback work ensure Thread.kill(t) if t FileUtils.rm(test_file) if File.exists?(test_file) adapter.stop end |
Instance Method Details
#start(blocking = true) ⇒ Object
Starts the adapter.
75 76 77 |
# File 'lib/listen/adapter.rb', line 75 def start(blocking = true) @stop = false end |
#stop ⇒ Object
Stops the adapter.
81 82 83 84 |
# File 'lib/listen/adapter.rb', line 81 def stop @stop = true @turnstile.signal # ensure no thread is blocked end |
#wait_for_callback ⇒ Object
Blocks the main thread until the poll thread calls the callback.
89 90 91 |
# File 'lib/listen/adapter.rb', line 89 def wait_for_callback @turnstile.wait unless @paused end |