Class: Libuv::Q::Deferred
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 Attribute Summary collapse
-
#pending ⇒ Object
readonly
Returns the value of attribute pending.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(loop) ⇒ Deferred
constructor
A new instance of Deferred.
-
#inspect ⇒ Object
Overwrite to prevent inspecting errors hanging the VM.
-
#notify(*args) ⇒ Object
Provides an asynchronous callback mechanism.
-
#promise ⇒ Object
Creates a promise object associated with this deferred.
-
#reject(reason = nil) ⇒ Object
rejects the derived promise with the reason.
-
#resolve(val = nil) ⇒ Object
resolves the derived promise with the value.
- #resolved? ⇒ Boolean
Methods included from Libuv::Q
all, any, defer, finally, reject
Constructor Details
#initialize(loop) ⇒ Deferred
Returns a new instance of Deferred.
225 226 227 228 229 230 231 |
# File 'lib/libuv/q.rb', line 225 def initialize(loop) super() @pending = [] @value = nil @loop = loop end |
Instance Attribute Details
#pending ⇒ Object (readonly)
Returns the value of attribute pending.
233 234 235 |
# File 'lib/libuv/q.rb', line 233 def pending @pending end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
233 234 235 |
# File 'lib/libuv/q.rb', line 233 def value @value end |
Instance Method Details
#inspect ⇒ Object
Overwrite to prevent inspecting errors hanging the VM
295 296 297 298 299 300 301 |
# File 'lib/libuv/q.rb', line 295 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
277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/libuv/q.rb', line 277 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 |
#promise ⇒ Object
Creates a promise object associated with this deferred
268 269 270 271 |
# File 'lib/libuv/q.rb', line 268 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.
261 262 263 |
# File 'lib/libuv/q.rb', line 261 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.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/libuv/q.rb', line 240 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
290 291 292 |
# File 'lib/libuv/q.rb', line 290 def resolved? @pending.nil? end |