Sha256: 8a05589de72c1b10a0953535510ceda389804c684f555ff24d3e49211d408593

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Libuv
    class Async < Handle


        define_callback function: :on_async


        # @param reactor [::Libuv::Reactor] reactor this async callback will be associated
        def initialize(reactor)
            @reactor = reactor

            async_ptr = ::Libuv::Ext.allocate_handle_async
            on_async = callback(:on_async, async_ptr.address)
            error = check_result(::Libuv::Ext.async_init(reactor.handle, async_ptr, 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
            self
        end

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


        private


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

Version data entries

4 entries across 4 versions & 1 rubygems

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