diff --git a/lib/async/debug/monitor.rb b/lib/async/debug/monitor.rb index 4cc57b7b..99837004 100644 --- a/lib/async/debug/monitor.rb +++ b/lib/async/debug/monitor.rb @@ -20,25 +20,23 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +require 'delegate' + module Async module Debug - class Monitor + class Monitor < Delegator def initialize(monitor, selector) @monitor = monitor @selector = selector end - def close - @selector.deregister(@monitor.io) - @monitor.close + def __getobj__ + @monitor end - def method_missing(*arguments, &block) - @monitor.send(*arguments) - end - - def respond_to?(*arguments) - @monitor.respond_to?(*arguments) + def close + @selector.deregister(self) + @monitor.close end def inspect diff --git a/lib/async/debug/selector.rb b/lib/async/debug/selector.rb index e08ca546..c3887930 100644 --- a/lib/async/debug/selector.rb +++ b/lib/async/debug/selector.rb @@ -24,6 +24,7 @@ require_relative '../logger' require 'nio' +require 'set' module Async module Debug @@ -36,7 +37,7 @@ def initialize(monitors) class Selector def initialize(selector = NIO::Selector.new) @selector = selector - @monitors = {} + @monitors = Set.new end def register(object, interests) @@ -46,26 +47,18 @@ def register(object, interests) raise RuntimeError, "Could not convert #{io} into IO!" end - if monitor = @monitors[io.fileno] - raise RuntimeError, "Trying to register monitor for #{object.inspect} but it was already registered: #{monitor.inspect}!" - end - monitor = Monitor.new(@selector.register(object, interests), self) - @monitors[io.fileno] = monitor + @monitors.add(monitor) return monitor end - def deregister(object) - Async.logger.debug(self) {"Deregistering #{object.inspect}."} - - unless io = ::IO.try_convert(object) - raise RuntimeError, "Could not convert #{io} into IO!" - end + def deregister(monitor) + Async.logger.debug(self) {"Deregistering #{monitor.inspect}."} - unless @monitors.delete(io.fileno) - raise RuntimeError, "Trying to remove monitor for #{io.inspect} but it was not registered!" + unless @monitors.delete?(monitor) + raise RuntimeError, "Trying to remove monitor for #{monitor.inspect} but it was not registered!" end end @@ -75,7 +68,7 @@ def wakeup def close if @monitors.any? - raise LeakError, @monitors.values + raise LeakError, @monitors end ensure @selector.close