Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap Listener instances in WeakRef #477

Merged
merged 2 commits into from
Aug 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/listen.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'logger'
require 'weakref'
require 'listen/logger'
require 'listen/listener'

Expand All @@ -18,6 +19,8 @@
Listen::Logger.info "Listen version: #{Listen::VERSION}"

module Listen
@listeners = Queue.new

class << self
# Listens to file system modifications on a either single directory or
# multiple directories.
Expand All @@ -32,21 +35,23 @@ class << self
# @return [Listen::Listener] the listener
#
def to(*args, &block)
@listeners ||= []
Listener.new(*args, &block).tap do |listener|
@listeners << listener
@listeners.enq(WeakRef.new(listener))
end
end

# This is used by the `listen` binary to handle Ctrl-C
#
def stop
Internals::ThreadPool.stop
@listeners ||= []

# TODO: should use a mutex for this
@listeners.each(&:stop)
@listeners = nil
while (listener = @listeners.deq(true))
begin
listener.stop
rescue WeakRef::RefError
end
end
rescue ThreadError
end
end
end