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 ⇒ Object
Returns future value or raises future error.
-
#join ⇒ self
Block until the future has been resolved.
-
#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
233 234 235 |
# File 'lib/cassandra/future.rb', line 233 def self.all(*futures) @@factory.all(*futures) end |
.error(error) ⇒ Cassandra::Future<Exception>
Returns a future resolved to a given error
221 222 223 |
# File 'lib/cassandra/future.rb', line 221 def self.error(error) @@factory.error(error) end |
.promise ⇒ Object
Returns a new promise instance
238 239 240 |
# File 'lib/cassandra/future.rb', line 238 def self.promise @@factory.promise end |
.value(value) ⇒ Cassandra::Future<Object>
Returns a future resolved to a given value
214 215 216 |
# File 'lib/cassandra/future.rb', line 214 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
298 299 300 301 302 303 304 305 |
# File 'lib/cassandra/future.rb', line 298 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.
353 354 355 356 |
# File 'lib/cassandra/future.rb', line 353 def fallback(&block) raise ::ArgumentError, "no block given" unless block_given? @signal.fallback(&block) end |
#get ⇒ Object
This method blocks until a future is resolved
Returns future value or raises future error
362 363 364 365 |
# File 'lib/cassandra/future.rb', line 362 def get @signal.get end |
#join ⇒ self
This method blocks until a future is resolved
This method won't raise any errors or return anything but the future itself
Block until the future has been resolved
372 373 374 375 |
# File 'lib/cassandra/future.rb', line 372 def join @signal.join self 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.
283 284 285 286 287 |
# File 'lib/cassandra/future.rb', line 283 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
267 268 269 270 271 |
# File 'lib/cassandra/future.rb', line 267 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
254 255 256 257 258 |
# File 'lib/cassandra/future.rb', line 254 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.
327 328 329 330 |
# File 'lib/cassandra/future.rb', line 327 def then(&block) raise ::ArgumentError, "no block given" unless block_given? @signal.then(&block) end |