Sha256: 7c93f17e7afea4e9acfb826c69339c38ea51508f8018d7b99df6b9572ff48f3a

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module UV
  module Handle
    include Assertions, Resource, Listener

    class << self
      def close(handle)
        proc { UV.close(handle, UV.method(:free)) }
      end
    end

    def initialize(loop, pointer)
      @loop, @pointer = loop, pointer

      ObjectSpace.define_finalizer(self, Handle.close(@pointer))
    end

    # Public: Increment internal ref counter for the handle on the loop. Useful for
    # extending the loop with custom watchers that need to make loop not stop
    # 
    # Returns self
    def ref
      UV.ref(@pointer)

      self
    end

    # Public: Decrement internal ref counter for the handle on the loop, useful to stop
    # loop even when there are outstanding open handles
    # 
    # Returns self
    def unref
      UV.unref(@pointer)

      self
    end

    def close(&block)
      return if @pointer.nil?

      ObjectSpace.undefine_finalizer(self)

      if block
        assert_block(block)
        assert_arity(0, block)

        @close_block = block
      end

      UV.close(@pointer, callback(:on_close))

      self
    end

    def active?
      UV.is_active(@pointer) > 0
    end

    def closing?
      UV.is_closing(@pointer) > 0
    end

    protected

    def loop; @loop; end
    def handle; @pointer; end

    private

    def handle_name
      self.class.name.split('::').last.downcase.to_sym
    end

    def on_close(pointer)
      UV.free(pointer)
      clear_callbacks

      @close_block.call unless @close_block.nil?

      @pointer = nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
uvrb-0.2.0 lib/uv/handle.rb