Class: Libuv::Q::Deferred

Inherits:
Object show all
Includes:
Libuv::Q
Defined in:
lib/libuv/q.rb

Overview

The purpose of the deferred object is to expose the associated Promise instance as well as APIs that can be used for signalling the successful or unsuccessful completion of a task.

Instance Method Summary collapse

Methods included from Libuv::Q

all, any, defer, finally, reject

Constructor Details

#initialize(loop) ⇒ Deferred

Returns a new instance of Deferred.



222
223
224
225
226
227
228
# File 'lib/libuv/q.rb', line 222

def initialize(loop)
    super()
    
    @pending = []
    @value = nil
    @loop = loop
end

Instance Method Details

#inspectObject

Overwrite to prevent inspecting errors hanging the VM



290
291
292
293
294
295
296
# File 'lib/libuv/q.rb', line 290

def inspect
    if @pending.nil?
        "#<#{self.class}:0x#{self.__id__.to_s(16)} @loop=#{@loop.inspect} @value=#{@value.inspect}>"
    else
          "#<#{self.class}:0x#{self.__id__.to_s(16)} @loop=#{@loop.inspect} @pending.count=#{@pending.length}>"
    end
end

#notify(*args) ⇒ Object

Provides an asynchronous callback mechanism

Parameters:

  • data (*Object)

    you would like to send down the promise chain.



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/libuv/q.rb', line 272

def notify(*args)
    @loop.schedule do     # just in case we are on a different event loop

        if @pending && @pending.length > 0
            callbacks = @pending
            @loop.next_tick do
                callbacks.each do |callback|
                    callback[2].call(*args)
                end
            end
        end
    end
end

#promiseObject

Creates a promise object associated with this deferred



263
264
265
266
# File 'lib/libuv/q.rb', line 263

def promise
    @promise ||= DeferredPromise.new(@loop, self)
    @promise # Should only ever be one per deferred

end

#reject(reason = nil) ⇒ Object

rejects the derived promise with the reason. This is equivalent to resolving it with a rejection constructed via Q.reject.

Parameters:

  • reason (Object) (defaults to: nil)

    constant, message, exception or an object representing the rejection reason.



256
257
258
# File 'lib/libuv/q.rb', line 256

def reject(reason = nil)
    resolve(Q.reject(@loop, reason))
end

#resolve(val = nil) ⇒ Object

resolves the derived promise with the value. If the value is a rejection constructed via Q.reject, the promise will be rejected instead.

Parameters:

  • val (Object) (defaults to: nil)

    constant, message or an object representing the result.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/libuv/q.rb', line 235

def resolve(val = nil)
    @loop.schedule do
        if not @pending.nil?
            callbacks = @pending
            @pending = nil
            @value = ref(@loop, val)
            
            if callbacks.length > 0
                callbacks.each do |callback|
                    @value.then(callback[0], callback[1], callback[2])
                end
            end
        end
    end
end

#resolved?Boolean

Returns:

  • (Boolean)


285
286
287
# File 'lib/libuv/q.rb', line 285

def resolved?
    @pending.nil?
end