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 Method Summary collapse
-
#initialize(loop) ⇒ Deferred
constructor
A new instance of Deferred.
-
#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.
Methods included from Libuv::Q
all, any, defer, finally, reject
Constructor Details
#initialize(loop) ⇒ Deferred
Returns a new instance of Deferred.
202 203 204 205 206 207 208 |
# File 'lib/libuv/q.rb', line 202 def initialize(loop) super() @pending = [] @value = nil @loop = loop end |
Instance Method Details
#notify(*args) ⇒ Object
Provides an asynchronous callback mechanism
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/libuv/q.rb', line 251 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
243 244 245 |
# File 'lib/libuv/q.rb', line 243 def promise DeferredPromise.new(@loop, self) 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.
236 237 238 |
# File 'lib/libuv/q.rb', line 236 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.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/libuv/q.rb', line 215 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 |