Sha256: 8afebb7428b1ca7aedbff55ac6813a8f3e06783e728ed6aef3e56e717a5ce042

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

module Libuv
    class Async < Handle


        # @param loop [::Libuv::Loop] loop this async callback will be associated
        def initialize(loop, callback = nil, &blk)
            @loop = loop
            @callback = callback || blk
            async_ptr = ::Libuv::Ext.create_handle(:uv_async)
            error = check_result(::Libuv::Ext.async_init(loop.handle, async_ptr, callback(:on_async)))

            super(async_ptr, error)
        end

        # Triggers a notify event, calling everything in the notify chain
        def call
            return if @closed
            error = check_result ::Libuv::Ext.async_send(handle)
            reject(error) if error
        end

        # Used to update the callback that will be triggered when async is called
        #
        # @param callback [Proc] the callback to be called on loop prepare
        def progress(callback = nil, &blk)
            @callback = callback || blk
        end


        private


        def on_async(handle, status)
            e = check_result(status)

            if e
                reject(e)
            else
                begin
                    @callback.call
                rescue Exception => e
                    @loop.log :error, :async_cb, e
                end
            end
        end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
libuv-0.11.22 lib/libuv/async.rb
libuv-0.11.4 lib/libuv/async.rb