Class: Libuv::Work

Inherits:
Q::DeferredPromise show all
Includes:
Listener, Resource
Defined in:
lib/libuv/work.rb

Constant Summary

Constants inherited from Q::Promise

Q::Promise::MAKE_PROMISE

Instance Method Summary collapse

Methods included from Resource

#check_result, #check_result!, #resolve, #to_ptr

Methods inherited from Q::DeferredPromise

#then

Methods inherited from Q::Promise

#catch, #finally, #progress

Constructor Details

#initialize(loop, work) ⇒ Work

Returns a new instance of Work.

Parameters:

  • loop (::Libuv::Loop) —

    loop this work request will be associated

  • work (Proc) —

    callback to be called in the thread pool



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/libuv/work.rb', line 8

def initialize(loop, work)
    super(loop, loop.defer)

    @work = work
    @complete = false
    @pointer = ::Libuv::Ext.create_request(:uv_work)
    @error = nil    # error in callback

    error = check_result ::Libuv::Ext.queue_work(@loop, @pointer, callback(:on_work), callback(:on_complete))
    if error
        ::Libuv::Ext.free(@pointer)
        @complete = true
        @defer.reject(error)
    end
end

Instance Method Details

#cancel ⇒ true, false

Attempt to cancel the pending work. Returns true if the work has completed or was canceled.

Returns:

  • (true, false)


27
28
29
30
31
32
# File 'lib/libuv/work.rb', line 27

def cancel
    if not @complete
        @complete = ::Libuv::Ext.cancel(@pointer) >= 0
    end
    @complete
end

#completed? ⇒ true, false

Indicates is the work has completed yet or not.

Returns:

  • (true, false)


37
38
39
# File 'lib/libuv/work.rb', line 37

def completed?
    return @complete
end