Sha256: 5e8be23c152e3402e0c8d09367924c531823677b4b2a481ce59aa2315081f63d

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 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)
            @reactor = reactor

            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)
            @callback = callback
            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

4 entries across 4 versions & 1 rubygems

Version Path
libuv-4.0.9 lib/libuv/idle.rb
libuv-4.0.2 lib/libuv/idle.rb
libuv-4.0.1 lib/libuv/idle.rb
libuv-4.0.0 lib/libuv/idle.rb