Permalink
Browse files

Code syntax improvements

  • Loading branch information...
1 parent 39bdda5 commit 016b2e2593a266ece7644c529e0ea5d423690cde @thibaudgg thibaudgg committed Apr 9, 2013
View
@@ -5,7 +5,8 @@
module Listen
class Adapter
- attr_accessor :directories, :latency, :paused
+ attr_accessor :directories, :callback, :stopped, :paused,
+ :mutex, :changed_directories, :turnstile, :latency
# The list of existing optimized adapters.
OPTIMIZED_ADAPTERS = %w[Darwin Linux BSD Windows]
@@ -38,8 +39,8 @@ class Adapter
# @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_dirs, options] callback the callback called when a change happens
- # @yieldparam [Array<String>] changed_dirs the changed directories
+ # @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
@@ -65,38 +66,39 @@ def self.select_and_initialize(directories, options = {}, &callback)
Adapters::Polling.new(directories, options, &callback)
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
# @option options [Boolean] report_changes whether or not to automatically report changes (run the callback)
#
- # @yield [changed_dirs, options] callback Callback called when a change happens
- # @yieldparam [Array<String>] changed_dirs the changed directories
+ # @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
#
def initialize(directories, options = {}, &callback)
- @directories = Array(directories)
- @callback = callback
- @stopped = true
- @paused = false
- @mutex = Mutex.new
- @changed_dirs = Set.new
- @turnstile = Turnstile.new
- @latency ||= options[:latency] || DEFAULT_LATENCY
- @report_changes = options[:report_changes].nil? ? true : options[:report_changes]
+ @directories = Array(directories)
+ @callback = callback
+ @stopped = true
+ @paused = false
+ @mutex = Mutex.new
+ @changed_directories = Set.new
+ @turnstile = Turnstile.new
+ @latency ||= options[:latency] || DEFAULT_LATENCY
+ @report_changes = options.fetch(:report_changes, true)
end
# Starts the adapter.
#
# @param [Boolean] blocking whether or not to block the current thread after starting
#
def start(blocking = true)
- @mutex.synchronize do
- return unless @stopped
+ mutex.synchronize do
+ return unless stopped
@stopped = false
end
end
@@ -105,7 +107,7 @@ def start(blocking = true)
#
def stop
@stopped = true
- @turnstile.signal # ensure no thread is blocked
+ turnstile.signal # ensure no thread is blocked
end
# Pauses the adapter.
@@ -125,22 +127,22 @@ def unpause
# @return [Boolean] whether the adapter is started or not
#
def started?
- !@stopped
+ !stopped
end
# Returns whether the adapter is paused or not.
#
# @return [Boolean] whether the adapter is paused or not
#
def paused?
- @paused
+ paused
end
# Blocks the main thread until the poll thread
# runs the callback.
#
def wait_for_callback
- @turnstile.wait unless @paused
+ turnstile.wait unless paused
end
# Blocks the main thread until N changes are
@@ -150,12 +152,12 @@ def wait_for_changes(threshold = 0)
changes = 0
loop do
- @mutex.synchronize { changes = @changed_dirs.size }
+ mutex.synchronize { changes = changed_directories.size }
- return if @paused || @stopped
+ return if paused || stopped
return if changes >= threshold
- sleep(@latency)
+ sleep(latency)
end
end
@@ -192,9 +194,10 @@ def self.usable_and_works?(directories, options = {})
# @return [Boolean] whether the adapter works or not
#
def self.works?(directory, options = {})
- work, test_file = false, "#{directory}/.listen_test"
- callback = lambda { |*| work = true }
- adapter = self.new(directory, options, &callback)
+ work = false
+ test_file = "#{directory}/.listen_test"
+ callback = lambda { |*| work = true }
+ adapter = self.new(directory, options, &callback)
adapter.start(false)
FileUtils.touch(test_file)
@@ -214,14 +217,18 @@ def self.works?(directory, options = {})
def report_changes
changed_dirs = nil
- @mutex.synchronize do
- return if @changed_dirs.empty?
- changed_dirs = @changed_dirs.to_a
- @changed_dirs.clear
+ mutex.synchronize do
+ return if @changed_directories.empty?
+ changed_dirs = @changed_directories.to_a
+ @changed_directories.clear
end
- @callback.call(changed_dirs, {})
- @turnstile.signal
+ callback.call(changed_dirs, {})
+ turnstile.signal
+ end
+
+ def report_changes?
+ @report_changes
end
private
@@ -243,9 +250,9 @@ def self.warn_polling_fallback(warning, options)
# Polls changed directories and reports them back
# when there are changes.
#
- def poll_changed_dirs
- until @stopped
- sleep(@latency)
+ def poll_changed_directories
+ until stopped
+ sleep(latency)
report_changes
end
end
View
@@ -16,13 +16,15 @@ class BSD < Adapter
#
EVENTS = [:delete, :write, :extend, :attrib, :link, :rename, :revoke]
+ attr_accessor :worker, :worker_thread, :poll_thread
+
# Initializes the Adapter.
#
# @see Listen::Adapter#initialize
#
def initialize(directories, options = {}, &callback)
super
- @kqueue = init_kqueue
+ @worker = init_worker
end
# Starts the adapter.
@@ -32,28 +34,27 @@ def initialize(directories, options = {}, &callback)
def start(blocking = true)
super
- @kqueue_thread = Thread.new do
- until @stopped
- @kqueue.poll
- sleep(@latency)
+ @worker_thread = Thread.new do
+ until stopped
+ worker.poll
+ sleep(latency)
end
end
- @poll_thread = Thread.new { poll_changed_dirs } if @report_changes
-
- @kqueue_thread.join if blocking
+ @poll_thread = Thread.new { poll_changed_directories } if report_changes?
+ worker_thread.join if blocking
end
# Stops the adapter.
#
def stop
- @mutex.synchronize do
- return if @stopped
+ mutex.synchronize do
+ return if stopped
super
end
- @kqueue.stop
- Thread.kill(@kqueue_thread) if @kqueue_thread
- @poll_thread.join if @poll_thread
+ worker.stop
+ Thread.kill(worker_thread) if worker_thread
+ poll_thread.join if poll_thread
end
# Checks if the adapter is usable on BSD.
@@ -72,15 +73,15 @@ def self.usable?
#
# @return [INotify::Notifier] initialized kqueue
#
- def init_kqueue
+ def init_worker
require 'find'
callback = lambda do |event|
path = event.watcher.path
- @mutex.synchronize do
+ mutex.synchronize do
# kqueue watches everything, but Listen only needs the
# directory where stuffs happens.
- @changed_dirs << (File.directory?(path) ? path : File.dirname(path))
+ @changed_directories << (File.directory?(path) ? path : File.dirname(path))
# If it is a directory, and it has a write flag, it means a
# file has been added so find out which and deal with it.
@@ -98,7 +99,7 @@ def init_kqueue
end
KQueue::Queue.new.tap do |queue|
- @directories.each do |directory|
+ directories.each do |directory|
Find.find(directory) do |path|
queue.watch_file(path, *EVENTS, &callback)
end
@@ -11,6 +11,8 @@ class Darwin < Adapter
LAST_SEPARATOR_REGEX = /\/$/
+ attr_accessor :worker, :worker_thread, :poll_thread
+
# Initializes the Adapter.
#
# @see Listen::Adapter#initialize
@@ -27,29 +29,29 @@ def initialize(directories, options = {}, &callback)
def start(blocking = true)
super
- @worker_thread = Thread.new { @worker.run }
+ @worker_thread = Thread.new { worker.run }
# The FSEvent worker needs some time to start up. Turnstiles can't
# be used to wait for it as it runs in a loop.
# TODO: Find a better way to block until the worker starts.
sleep 0.1
- @poll_thread = Thread.new { poll_changed_dirs } if @report_changes
+ @poll_thread = Thread.new { poll_changed_directories } if report_changes?
- @worker_thread.join if blocking
+ worker_thread.join if blocking
end
# Stops the adapter.
#
def stop
- @mutex.synchronize do
- return if @stopped
+ mutex.synchronize do
+ return if stopped
super
end
- @worker.stop
- @worker_thread.join if @worker_thread
- @poll_thread.join if @poll_thread
+ worker.stop
+ Thread.kill(worker_thread) if worker_thread
+ poll_thread.join if poll_thread
end
# Checks if the adapter is usable on Mac OSX.
@@ -70,11 +72,11 @@ def self.usable?
#
def init_worker
FSEvent.new.tap do |worker|
- worker.watch(@directories.dup, latency: @latency) do |changes|
- next if @paused
+ worker.watch(directories.dup, latency: latency) do |changes|
+ next if paused
- @mutex.synchronize do
- changes.each { |path| @changed_dirs << path.sub(LAST_SEPARATOR_REGEX, '') }
+ mutex.synchronize do
+ changes.each { |path| @changed_directories << path.sub(LAST_SEPARATOR_REGEX, '') }
end
end
end
@@ -25,6 +25,8 @@ class Linux < Adapter
for information on how to solve this issue.
EOS
+ attr_accessor :worker, :worker_thread, :poll_thread
+
# Initializes the Adapter.
#
# @see Listen::Adapter#initialize
@@ -43,23 +45,23 @@ def initialize(directories, options = {}, &callback)
def start(blocking = true)
super
- @worker_thread = Thread.new { @worker.run }
- @poll_thread = Thread.new { poll_changed_dirs } if @report_changes
+ @worker_thread = Thread.new { worker.run }
+ @poll_thread = Thread.new { poll_changed_directories } if report_changes?
- @worker_thread.join if blocking
+ worker_thread.join if blocking
end
# Stops the adapter.
#
def stop
- @mutex.synchronize do
- return if @stopped
+ mutex.synchronize do
+ return if stopped
super
end
- @worker.stop
- Thread.kill(@worker_thread) if @worker_thread
- @poll_thread.join if @poll_thread
+ worker.stop
+ Thread.kill(worker_thread) if worker_thread
+ poll_thread.join if poll_thread
end
# Checks if the adapter is usable on Linux.
@@ -80,7 +82,7 @@ def self.usable?
#
def init_worker
callback = lambda do |event|
- if @paused || (
+ if paused || (
# Event on root directory
event.name == ""
) || (
@@ -94,15 +96,13 @@ def init_worker
next
end
- @mutex.synchronize do
- @changed_dirs << File.dirname(event.absolute_name)
+ mutex.synchronize do
+ @changed_directories << File.dirname(event.absolute_name)
end
end
INotify::Notifier.new.tap do |worker|
- @directories.each do |directory|
- worker.watch(directory, *EVENTS, &callback)
- end
+ directories.each { |dir| worker.watch(dir, *EVENTS, &callback) }
end
end
Oops, something went wrong.

0 comments on commit 016b2e2

Please sign in to comment.