Class: Cql::Future
- Inherits:
-
Object
- Object
- Cql::Future
- Extended by:
- FutureFactories
- Includes:
- FutureCallbacks, FutureCombinators
- Defined in:
- lib/cql/future.rb
Overview
A future represents the value of a process that may not yet have completed.
Direct Known Subclasses
Instance Method Summary collapse
-
#completed? ⇒ Boolean
Returns true if this future is resolved or failed.
-
#failed? ⇒ Boolean
Returns true if this future has failed.
-
#initialize ⇒ Future
constructor
A new instance of Future.
-
#resolved? ⇒ Boolean
Returns true if this future is resolved.
-
#value ⇒ Object
Returns the value of this future, blocking until it is available if necessary.
Methods included from FutureFactories
Methods included from FutureCallbacks
#on_complete, #on_failure, #on_value
Methods included from FutureCombinators
#fallback, #flat_map, #map, #recover
Constructor Details
#initialize ⇒ Future
Returns a new instance of Future.
313 314 315 316 317 318 319 320 |
# File 'lib/cql/future.rb', line 313 def initialize @lock = Mutex.new @resolved = false @failed = false @failure_listeners = [] @value_listeners = [] @complete_listeners = [] end |
Instance Method Details
#completed? ⇒ Boolean
Returns true if this future is resolved or failed
349 350 351 |
# File 'lib/cql/future.rb', line 349 def completed? resolved? || failed? end |
#failed? ⇒ Boolean
Returns true if this future has failed
359 360 361 |
# File 'lib/cql/future.rb', line 359 def failed? @lock.synchronize { @failed } end |
#resolved? ⇒ Boolean
Returns true if this future is resolved
354 355 356 |
# File 'lib/cql/future.rb', line 354 def resolved? @lock.synchronize { @resolved } end |
#value ⇒ Object
Returns the value of this future, blocking until it is available if necessary.
If the future fails this method will raise the error that failed the future.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/cql/future.rb', line 329 def value semaphore = nil @lock.synchronize do raise @error if @failed return @value if @resolved semaphore = Queue.new u = proc { semaphore << :unblock } @value_listeners << u @failure_listeners << u end while true @lock.synchronize do raise @error if @failed return @value if @resolved end semaphore.pop end end |