Sha256: 676552ce5ba028f7f9d36e5c5e5cd3ba74305fe4c49a49917c22008dd15f69b3

Contents?: true

Size: 1.57 KB

Versions: 12

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)
            ::Fiber.new {
                begin
                    @callback.call
                rescue Exception => e
                    @reactor.log e, 'performing idle callback'
                end
            }.resume
        end
    end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
libuv-3.1.9 lib/libuv/idle.rb
libuv-3.1.8 lib/libuv/idle.rb
libuv-3.1.7 lib/libuv/idle.rb
libuv-3.1.6 lib/libuv/idle.rb
libuv-3.1.5 lib/libuv/idle.rb
libuv-3.1.4 lib/libuv/idle.rb
libuv-3.1.3 lib/libuv/idle.rb
libuv-3.1.2 lib/libuv/idle.rb
libuv-3.1.1 lib/libuv/idle.rb
libuv-3.1.0 lib/libuv/idle.rb
libuv-3.0.1 lib/libuv/idle.rb
libuv-3.0.0 lib/libuv/idle.rb