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.
-
#reference ⇒ Object
readonly
Returns the value of attribute reference.
Instance Method Summary collapse
-
#initialize(reactor) ⇒ 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
- #value ⇒ Object
Methods included from Libuv::Q
all, any, defer, finally, reject
Constructor Details
#initialize(reactor) ⇒ Deferred
Returns a new instance of Deferred.
227 228 229 230 231 232 233 |
# File 'lib/libuv/q.rb', line 227 def initialize(reactor) super() @pending = [] @reference = nil @reactor = reactor end |
Instance Attribute Details
#pending ⇒ Object (readonly)
Returns the value of attribute pending.
235 236 237 |
# File 'lib/libuv/q.rb', line 235 def pending @pending end |
#reference ⇒ Object (readonly)
Returns the value of attribute reference.
235 236 237 |
# File 'lib/libuv/q.rb', line 235 def reference @reference end |
Instance Method Details
#inspect ⇒ Object
Overwrite to prevent inspecting errors hanging the VM
303 304 305 306 307 308 309 |
# File 'lib/libuv/q.rb', line 303 def inspect if @pending.nil? "#<#{self.class}:0x#{self.__id__.to_s(16)} @reactor=#{@reactor.inspect} @reference=#{@reference.inspect}>" else "#<#{self.class}:0x#{self.__id__.to_s(16)} @reactor=#{@reactor.inspect} @pending.count=#{@pending.length}>" end end |
#notify(*args) ⇒ Object
Provides an asynchronous callback mechanism
280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/libuv/q.rb', line 280 def notify(*args) @reactor.schedule do # just in case we are on a different event reactor if @pending && @pending.length > 0 callbacks = @pending @reactor.next_tick do callbacks.each do |callback| callback[2].call(*args) end end end end self end |
#promise ⇒ Object
Creates a promise object associated with this deferred
271 272 273 274 |
# File 'lib/libuv/q.rb', line 271 def promise @promise ||= DeferredPromise.new(@reactor, 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.
264 265 266 |
# File 'lib/libuv/q.rb', line 264 def reject(reason = nil) resolve(Q.reject(@reactor, 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.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/libuv/q.rb', line 242 def resolve(val = nil) @reactor.schedule do if not @pending.nil? callbacks = @pending @pending = nil @reference = ref(@reactor, val) if callbacks.length > 0 callbacks.each do |callback| @reference.then(callback[0], callback[1], callback[2]) end end end end self end |
#resolved? ⇒ Boolean
294 295 296 |
# File 'lib/libuv/q.rb', line 294 def resolved? @pending.nil? end |
#value ⇒ Object
298 299 300 |
# File 'lib/libuv/q.rb', line 298 def value co self.promise end |