Sha256: d4a496fe072a10e4556658838e778728147b933cea2e1e3cb06fc0aeceb3f311

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

module Libuv
    class Prepare < Handle


        # @param loop [::Libuv::Loop] loop this prepare handle will be associated
        # @param callback [Proc] callback to be called on loop preparation
        def initialize(loop, callback = nil, &blk)
            @loop = loop
            @callback = callback || blk

            prepare_ptr = ::Libuv::Ext.create_handle(:uv_prepare)
            error = check_result(::Libuv::Ext.prepare_init(@pointer, prepare_ptr))

            super(prepare_ptr, error)
        end

        # Enables the prepare handler.
        def start
            return if @closed
            error = check_result ::Libuv::Ext.prepare_start(handle, callback(:on_prepare))
            reject(error) if error
        end

        # Disables the prepare handler.
        def stop
            return if @closed
            error = check_result ::Libuv::Ext.prepare_stop(handle)
            reject(error) if error
        end

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


        private


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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
libuv-0.10.2 lib/libuv/prepare.rb