Class: Listen::Adapter
- Inherits:
-
Object
- Object
- Listen::Adapter
- Defined in:
- lib/listen/adapter.rb
Direct Known Subclasses
Listen::Adapters::BSD, 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.25
- MISSING_DEPENDENCY_MESSAGE =
The default warning message when there is a missing dependency.
<<-EOS.gsub(/^\s*/, '') For a better performance, it's recommended that you satisfy the missing dependency. EOS
- POLLING_FALLBACK_MESSAGE =
The default warning message when falling back to polling adapter.
<<-EOS.gsub(/^\s*/, '') Listen will be polling changes. Learn more at https://github.com/guard/listen#polling-fallback. EOS
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? ⇒ Boolean
Checks if the adapter is usable on the current OS.
-
.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.
-
#report_changes ⇒ Object
Runs the callback and passes it the changes if there are any.
-
#start(blocking = true) ⇒ Object
Starts the adapter.
-
#started? ⇒ Boolean
Returns whether the adapter is statred or not.
-
#stop ⇒ Object
Stops the adapter.
-
#wait_for_callback ⇒ Object
Blocks the main thread until the poll thread runs the callback.
-
#wait_for_changes(goal = 0) ⇒ Object
Blocks the main thread until N changes are detected.
Constructor Details
#initialize(directories, options = {}) {|changed_dirs, options| ... } ⇒ Listen::Adapter
Initializes the adapter.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/listen/adapter.rb', line 78 def initialize(directories, = {}, &callback) @directories = Array(directories) @callback = callback @paused = false @mutex = Mutex.new @changed_dirs = Set.new @turnstile = Turnstile.new @latency ||= DEFAULT_LATENCY @latency = [:latency] if [:latency] @report_changes = [:report_changes].nil? ? true : [:report_changes] 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.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/listen/adapter.rb', line 38 def self.select_and_initialize(directories, = {}, &callback) return Adapters::Polling.new(directories, , &callback) if .delete(:force_polling) warning = '' begin if Adapters::Darwin.usable_and_works?(directories, ) return Adapters::Darwin.new(directories, , &callback) elsif Adapters::Linux.usable_and_works?(directories, ) return Adapters::Linux.new(directories, , &callback) elsif Adapters::BSD.usable_and_works?(directories, ) return Adapters::BSD.new(directories, , &callback) elsif Adapters::Windows.usable_and_works?(directories, ) return Adapters::Windows.new(directories, , &callback) end rescue DependencyManager::Error => e warning += e. + "\n" + MISSING_DEPENDENCY_MESSAGE end unless [:polling_fallback_message] == false warning += [:polling_fallback_message] || POLLING_FALLBACK_MESSAGE Kernel.warn "[Listen warning]:\n" + warning.gsub(/^(.*)/, ' \1') end Adapters::Polling.new(directories, , &callback) end |
.usable? ⇒ Boolean
Checks if the adapter is usable on the current OS.
140 141 142 143 |
# File 'lib/listen/adapter.rb', line 140 def self.usable? load_depenencies dependencies_loaded? end |
.usable_and_works?(directories, options = {}) ⇒ Boolean
Checks if the adapter is usable and works on the current OS.
153 154 155 |
# File 'lib/listen/adapter.rb', line 153 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.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/listen/adapter.rb', line 168 def self.works?(directory, = {}) work = false test_file = "#{directory}/.listen_test" callback = lambda { |*| 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 if adapter && adapter.started? end |
Instance Method Details
#report_changes ⇒ Object
Runs the callback and passes it the changes if there are any.
189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/listen/adapter.rb', line 189 def report_changes changed_dirs = nil @mutex.synchronize do return if @changed_dirs.empty? changed_dirs = @changed_dirs.to_a @changed_dirs.clear end @callback.call(changed_dirs, {}) @turnstile.signal end |
#start(blocking = true) ⇒ Object
Starts the adapter.
94 95 96 |
# File 'lib/listen/adapter.rb', line 94 def start(blocking = true) @stop = false end |
#started? ⇒ Boolean
Returns whether the adapter is statred or not
109 110 111 |
# File 'lib/listen/adapter.rb', line 109 def started? @stop.nil? ? false : !@stop end |
#stop ⇒ Object
Stops the adapter.
100 101 102 103 |
# File 'lib/listen/adapter.rb', line 100 def stop @stop = true @turnstile.signal # ensure no thread is blocked end |
#wait_for_callback ⇒ Object
Blocks the main thread until the poll thread runs the callback.
116 117 118 |
# File 'lib/listen/adapter.rb', line 116 def wait_for_callback @turnstile.wait unless @paused end |
#wait_for_changes(goal = 0) ⇒ Object
Blocks the main thread until N changes are detected.
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/listen/adapter.rb', line 123 def wait_for_changes(goal = 0) changes = 0 loop do @mutex.synchronize { changes = @changed_dirs.size } return if @paused || @stop return if changes >= goal sleep(@latency) end end |