Sha256: c394a87bcd41b3f5662c6aa9963224cba699d14b0a7767d19af62e0260c2f7a1

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module Libuv
    class Idle < Handle


        define_callback function: :on_idle


        # @param reactor [::Libuv::Reactor] reactor this idle handler will be associated
        # @param callback [Proc] callback to be called when the reactor is idle
        def initialize(reactor, callback = nil, &blk)
            @reactor = reactor
            @callback = callback || blk

            idle_ptr = ::Libuv::Ext.allocate_handle_idle
            error = check_result(::Libuv::Ext.idle_init(reactor.handle, idle_ptr))

            super(idle_ptr, error)
        end

        # Enables the idle handler.
        def start
            return if @closed
            error = check_result ::Libuv::Ext.idle_start(handle, callback(:on_idle))
            reject(error) if error
            self
        end

        # Disables the idle handler.
        def stop
            return if @closed
            error = check_result ::Libuv::Ext.idle_stop(handle)
            reject(error) if error
            self
        end

        # Used to update the callback that will be triggered on idle
        #
        # @param callback [Proc] the callback to be called on idle trigger
        def progress(callback = nil, &blk)
            @callback = callback || blk
            self
        end


        private


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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
libuv-3.3.0 lib/libuv/idle.rb
libuv-3.2.4 lib/libuv/idle.rb
libuv-3.2.3 lib/libuv/idle.rb
libuv-3.2.2 lib/libuv/idle.rb
libuv-3.2.1 lib/libuv/idle.rb
libuv-3.2.0 lib/libuv/idle.rb