Object
The list of all existing adapters.
The default delay between checking for changes.
The list of existing fallback adapters.
The list of existing optimized adapters.
The default warning message when falling back to polling adapter.
Load the adapter gem
@return [Boolean] whether loaded or not
# File lib/listen/adapter.rb, line 205 def self.load_dependent_adapter return true if @loaded require adapter_gem return @loaded = true end
Initializes the adapter.
@param [String, Array<String>] directories the directories to watch @param [Hash] options the adapter options @option options [Float] latency the delay between checking for changes in seconds
@yield [changed_directories, options] callback Callback called when a change happens @yieldparam [Array<String>] changed_directories the changed directories @yieldparam [Hash] options callback options (like recursive: true)
@return [Listen::Adapter] the adapter
# File lib/listen/adapter.rb, line 78 def initialize(directories, options = {}, &callback) @directories = Array(directories) @callback = callback @stopped = true @paused = false @mutex = Mutex.new @changed_directories = Set.new @turnstile = Turnstile.new @latency = options.fetch(:latency, default_latency) @worker = initialize_worker end
Selects the appropriate adapter implementation for the current OS and initializes it.
@param [String, Array<String>] directories the directories to watch @param [Hash] options the adapter options @option options [Boolean] force_polling to force polling or not @option options [String, Boolean] polling_fallback_message to change polling fallback message or remove it @option options [Float] latency the delay between checking for changes in seconds
@yield [changed_directories, options] callback the callback called when a change happens @yieldparam [Array<String>] changed_directories the changed directories @yieldparam [Hash] options callback options (like recursive: true)
@return [Listen::Adapter] the chosen adapter
# File lib/listen/adapter.rb, line 44 def self.select_and_initialize(directories, options = {}, &callback) forced_adapter_class = options.delete(:force_adapter) force_polling = options.delete(:force_polling) if forced_adapter_class forced_adapter_class.load_dependent_adapter return forced_adapter_class.new(directories, options, &callback) end return Adapters::Polling.new(directories, options, &callback) if force_polling OPTIMIZED_ADAPTERS.each do |adapter| namespaced_adapter = Adapters.const_get(adapter) if namespaced_adapter.send(:usable_and_works?, directories, options) return namespaced_adapter.new(directories, options, &callback) end end self.warn_polling_fallback(options) Adapters::Polling.new(directories, options, &callback) end
Checks if the adapter is usable on target OS.
@return [Boolean] whether usable or not
# File lib/listen/adapter.rb, line 197 def self.usable? load_dependent_adapter if RbConfig::CONFIG['target_os'] =~ target_os_regex end
Checks if the adapter is usable and works on the current OS.
@param [String, Array<String>] directories the directories to watch @param [Hash] options the adapter options @option options [Float] latency the delay between checking for changes in seconds
@return [Boolean] whether the adapter is usable and work or not
# File lib/listen/adapter.rb, line 189 def self.usable_and_works?(directories, options = {}) usable? && Array(directories).all? { |d| works?(d, options) } end
Runs a tests to determine if the adapter can actually pick up changes in a given directory and returns the result.
@note This test takes some time depending on the adapter latency.
@param [String, Pathname] directory the directory to watch @param [Hash] options the adapter options @option options [Float] latency the delay between checking for changes in seconds
@return [Boolean] whether the adapter works or not
# File lib/listen/adapter.rb, line 222 def self.works?(directory, options = {}) work = false test_file = "#{directory}/.listen_test" callback = lambda { |*| work = true } adapter = self.new(directory, options, &callback) adapter.start 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, :force => true) adapter.stop if adapter && adapter.started? end
Pauses the adapter.
# File lib/listen/adapter.rb, line 132 def pause @paused = true end
Returns whether the adapter is paused or not.
@return [Boolean] whether the adapter is paused or not
# File lib/listen/adapter.rb, line 154 def paused? paused end
Runs the callback and passes it the changes if there are any.
# File lib/listen/adapter.rb, line 243 def report_changes changed_dirs = nil mutex.synchronize do return if @changed_directories.empty? changed_dirs = @changed_directories.to_a @changed_directories.clear end callback.call(changed_dirs, {}) turnstile.signal end
Starts the adapter and don't block the current thread.
@param [Boolean] blocking whether or not to block the current thread after starting
# File lib/listen/adapter.rb, line 94 def start mutex.synchronize do return unless stopped @stopped = false end start_worker start_poller end
Starts the adapter and block the current thread.
@since 1.0.0
# File lib/listen/adapter.rb, line 108 def start! start blocking_thread.join end
Returns whether the adapter is started or not.
@return [Boolean] whether the adapter is started or not
# File lib/listen/adapter.rb, line 146 def started? !stopped end
Stops the adapter.
# File lib/listen/adapter.rb, line 115 def stop mutex.synchronize do return if stopped @stopped = true turnstile.signal # ensure no thread is blocked end worker.stop if worker Thread.kill(worker_thread) if worker_thread if poller_thread poller_thread.kill poller_thread.join end end
Unpauses the adapter.
# File lib/listen/adapter.rb, line 138 def unpause @paused = false end
Blocks the main thread until the poll thread runs the callback.
# File lib/listen/adapter.rb, line 161 def wait_for_callback turnstile.wait unless paused end
Blocks the main thread until N changes are detected.
# File lib/listen/adapter.rb, line 168 def wait_for_changes(threshold = 0) changes = 0 loop do mutex.synchronize { changes = changed_directories.size } return if paused || stopped return if changes >= threshold sleep(latency) end end
Generated with the Darkfish Rdoc Generator 2.