Class: Cql::Future

Inherits:
Object
  • Object
show all
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.

See Also:

Direct Known Subclasses

CompletableFuture, FailedFuture, ResolvedFuture

Instance Method Summary collapse

Methods included from FutureFactories

all, failed, first, resolved

Methods included from FutureCallbacks

#on_complete, #on_failure, #on_value

Methods included from FutureCombinators

#fallback, #flat_map, #map, #recover

Constructor Details

#initializeFuture

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

Returns:

  • (Boolean)


349
350
351
# File 'lib/cql/future.rb', line 349

def completed?
  resolved? || failed?
end

#failed?Boolean

Returns true if this future has failed

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


354
355
356
# File 'lib/cql/future.rb', line 354

def resolved?
  @lock.synchronize { @resolved }
end

#valueObject

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.

Returns:

  • (Object)

    the value of this 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