Sha256: cde23947b319912e06f2154a9d40563ee369bd04319593f5c68a6fc8991504a5

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module MTLibuv
    class Async < Handle


        define_callback function: :on_async


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

            async_ptr = ::MTLibuv::Ext.allocate_handle_async
            on_async = callback(:on_async, async_ptr.address)
            error = check_result(::MTLibuv::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 ::MTLibuv::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

5 entries across 5 versions & 1 rubygems

Version Path
mt-libuv-4.1.04 lib/mt-libuv/async.rb
mt-libuv-4.1.03 lib/mt-libuv/async.rb
mt-libuv-4.1.02 lib/mt-libuv/async.rb
mt-libuv-4.1.01 lib/mt-libuv/async.rb
mt-libuv-4.1.0 lib/mt-libuv/async.rb