Class: Cassandra::Future
- Inherits:
-
Object
- Object
- Cassandra::Future
- Defined in:
- lib/cassandra/future.rb
Overview
A Future represents a result of asynchronous execution. It can be used to block until a value is available or an error has happened, or register a listener to be notified whenever the execution is complete.
Defined Under Namespace
Classes: Listener
Class Method Summary collapse
-
.all(*futures) ⇒ Object
Returns a future that resolves with values of all futures.
-
.error(error) ⇒ Cassandra::Future<Exception>
Returns a future resolved to a given error.
-
.promise ⇒ Object
Returns a new promise instance.
-
.value(value) ⇒ Cassandra::Future<Object>
Returns a future resolved to a given value.
Instance Method Summary collapse
-
#add_listener(listener) ⇒ self
Add future listener.
-
#fallback {|error| ... } ⇒ Cassandra::Future
Returns a new future that will resolve to the result of the block in case of an error.
-
#get(timeout = nil) ⇒ Object
(also: #join)
Returns future value or raises future error.
-
#on_complete {|value, error| ... } ⇒ self
Run block when future resolves.
-
#on_failure {|error| ... } ⇒ self
Run block when future resolves to error.
-
#on_success {|value| ... } ⇒ self
Run block when future resolves to a value.
-
#then {|value| ... } ⇒ Cassandra::Future
Returns a new future that will resolve to the result of the block.
Class Method Details
.all(*futures) ⇒ Cassandra::Future<Array<Object>> .all(futures) ⇒ Cassandra::Future<Array<Object>>
Returns a future that resolves with values of all futures
263 264 265 |
# File 'lib/cassandra/future.rb', line 263 def self.all(*futures) @@factory.all(*futures) end |
.error(error) ⇒ Cassandra::Future<Exception>
Returns a future resolved to a given error
251 252 253 |
# File 'lib/cassandra/future.rb', line 251 def self.error(error) @@factory.error(error) end |
.promise ⇒ Object
Returns a new promise instance
268 269 270 |
# File 'lib/cassandra/future.rb', line 268 def self.promise @@factory.promise end |
.value(value) ⇒ Cassandra::Future<Object>
Returns a future resolved to a given value
244 245 246 |
# File 'lib/cassandra/future.rb', line 244 def self.value(value) @@factory.value(value) end |
Instance Method Details
#add_listener(listener) ⇒ self
The listener can be notified synchronously, from current thread, if the future has already been resolved, or, asynchronously, from background thread upon resolution.
that provided listener doesn't have to extend Listener, only conform to the same interface
Add future listener
328 329 330 331 332 333 334 335 |
# File 'lib/cassandra/future.rb', line 328 def add_listener(listener) unless listener.respond_to?(:success) && listener.respond_to?(:failure) raise ::ArgumentError, 'listener must respond to both #success and #failure' end @signal.add_listener(listener) self end |
#fallback {|error| ... } ⇒ Cassandra::Future
The block can be called synchronously from current thread if the future has already been resolved, or, asynchronously, from background thread upon resolution.
Returns a new future that will resolve to the result of the block in case of an error. Besides regular values, block can return other futures, which will be transparently unwrapped before resolving the future from this method.
383 384 385 386 |
# File 'lib/cassandra/future.rb', line 383 def fallback(&block) raise ::ArgumentError, 'no block given' unless block_given? @signal.fallback(&block) end |
#get(timeout = nil) ⇒ Object Also known as: join
This method blocks until a future is resolved or a times out
Returns future value or raises future error
401 402 403 |
# File 'lib/cassandra/future.rb', line 401 def get(timeout = nil) @signal.get(timeout) end |
#on_complete {|value, error| ... } ⇒ self
The block can be called synchronously from current thread if the future has already been resolved, or, asynchronously, from background thread upon resolution.
Run block when future resolves. The block will always be called with 2 arguments - value and error. In case a future resolves to an error, the error argument will be non-nil.
313 314 315 316 317 |
# File 'lib/cassandra/future.rb', line 313 def on_complete(&block) raise ::ArgumentError, 'no block given' unless block_given? @signal.on_complete(&block) self end |
#on_failure {|error| ... } ⇒ self
The block can be called synchronously from current thread if the future has already been resolved, or, asynchronously, from background thread upon resolution.
Run block when future resolves to error
297 298 299 300 301 |
# File 'lib/cassandra/future.rb', line 297 def on_failure(&block) raise ::ArgumentError, 'no block given' unless block_given? @signal.on_failure(&block) self end |
#on_success {|value| ... } ⇒ self
The block can be called synchronously from current thread if the future has already been resolved, or, asynchronously, from background thread upon resolution.
Run block when future resolves to a value
284 285 286 287 288 |
# File 'lib/cassandra/future.rb', line 284 def on_success(&block) raise ::ArgumentError, 'no block given' unless block_given? @signal.on_success(&block) self end |
#then {|value| ... } ⇒ Cassandra::Future
The block can be called synchronously from current thread if the future has already been resolved, or, asynchronously, from background thread upon resolution.
Returns a new future that will resolve to the result of the block. Besides regular values, block can return other futures, which will be transparently unwrapped before resolving the future from this method.
357 358 359 360 |
# File 'lib/cassandra/future.rb', line 357 def then(&block) raise ::ArgumentError, 'no block given' unless block_given? @signal.then(&block) end |