Sha256: 5964e134249da97846f6c66406b9cf3b00c63e303c62dc92ad72865f61a7d066

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

module Libuv
    class Idle < Handle


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

            idle_ptr = ::Libuv::Ext.create_handle(:uv_idle)
            error = check_result(::Libuv::Ext.idle_init(loop.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
        end

        # Disables the idle handler.
        def stop
            return if @closed
            error = check_result ::Libuv::Ext.idle_stop(handle)
            reject(error) if error
        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
        end


        private


        def on_idle(handle)
            begin
                @callback.call
            rescue Exception => e
                @loop.log :error, :idle_cb, e
            end
        end
    end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
libuv-1.0.3 lib/libuv/idle.rb
libuv-1.0.2 lib/libuv/idle.rb
libuv-1.0.0 lib/libuv/idle.rb
libuv-0.12.4 lib/libuv/idle.rb
libuv-0.12.3 lib/libuv/idle.rb