Sha256: fd3f042c80ccb34a1d345a289ac865d75e2bbd97f83d937051cbc619320e301c

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module MTLibuv
    class Check < Handle


        define_callback function: :on_check


        # @param reactor [::MTLibuv::Reactor] reactor this check will be associated
        # @param callback [Proc] callback to be called on reactor check
        def initialize(reactor)
            @reactor = reactor

            check_ptr = ::MTLibuv::Ext.allocate_handle_check
            error = check_result(::MTLibuv::Ext.check_init(reactor.handle, check_ptr))

            super(check_ptr, error)
        end

        # Enables the check handler.
        def start
            return if @closed
            error = check_result ::MTLibuv::Ext.check_start(handle, callback(:on_check))
            reject(error) if error
            self
        end

        # Disables the check handler.
        def stop
            return if @closed
            error = check_result ::MTLibuv::Ext.check_stop(handle)
            reject(error) if error
            self
        end

        # Used to update the callback that will be triggered on reactor check
        #
        # @param callback [Proc] the callback to be called on reactor check
        def progress(&callback)
            @callback = callback
            self
        end


        private


        def on_check(handle)
            @reactor.exec do
                begin
                    @callback.call
                rescue Exception => e
                    @reactor.log e, 'performing check callback'
                end
            end
        end
    end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mt-libuv-4.1.04 lib/mt-libuv/check.rb
mt-libuv-4.1.03 lib/mt-libuv/check.rb
mt-libuv-4.1.02 lib/mt-libuv/check.rb
mt-libuv-4.1.01 lib/mt-libuv/check.rb
mt-libuv-4.1.0 lib/mt-libuv/check.rb