Module: Ione::Future::Factories
- Included in:
- Ione::Future
- Defined in:
- lib/ione/future.rb
Overview
Instance Method Summary collapse
-
#after(*futures) ⇒ Ione::Future
Combines multiple futures into a new future which resolves when all constituent futures complete, or fails when one or more of them fails.
-
#all(*futures) ⇒ Ione::Future<Array>
Combines multiple futures into a new future which resolves when all constituent futures complete, or fails when one or more of them fails.
-
#failed(error) ⇒ Ione::Future
Creates a new pre-failed future.
-
#first(*futures) ⇒ Ione::Future
Returns a future which will be resolved with the value of the first (resolved) of the specified futures.
-
#reduce(futures, initial_value = nil, options = nil) {|accumulator, value| ... } ⇒ Ione::Future
Returns a future that will resolve to a value which is the reduction of the values of a list of source futures.
-
#resolved(value = nil) ⇒ Ione::Future
Creates a new pre-resolved future.
-
#traverse(values) {|value| ... } ⇒ Ione::Future
Takes calls the block once for each element in an array, expecting each invocation to return a future, and returns a future that resolves to an array of the values of those futures.
Instance Method Details
#after(*futures) ⇒ Ione::Future
Combines multiple futures into a new future which resolves when all constituent futures complete, or fails when one or more of them fails.
The resulting future has no value.
232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/ione/future.rb', line 232 def after(*futures) if futures.size == 1 && (fs = futures.first).is_a?(Enumerable) *futures = *fs end futures.reject! { |f| f.respond_to?(:resolved?) && f.resolved? } if futures.count == 0 ResolvedFuture::NIL elsif futures.count == 1 futures.first.map(nil) else CombinedNilFuture.new(futures) end end |
#all(*futures) ⇒ Ione::Future<Array>
Combines multiple futures into a new future which resolves when all constituent futures complete, or fails when one or more of them fails.
The value of the combined future is an array of the values of the constituent futures.
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ione/future.rb', line 206 def all(*futures) if futures.size == 1 && (fs = futures.first).is_a?(Enumerable) futures = fs end if futures.count == 0 resolved([]) elsif (failed = futures.find { |f| f.respond_to?(:failed?) && f.failed? }) failed else CombinedFuture.new(futures) end end |
#failed(error) ⇒ Ione::Future
Creates a new pre-failed future.
368 369 370 |
# File 'lib/ione/future.rb', line 368 def failed(error) FailedFuture.new(error) end |
#first(*futures) ⇒ Ione::Future
Returns a future which will be resolved with the value of the first (resolved) of the specified futures. If all of the futures fail, the returned future will also fail (with the error of the last failed future).
263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/ione/future.rb', line 263 def first(*futures) if futures.size == 1 && (fs = futures.first).is_a?(Enumerable) futures = fs end if futures.count == 0 ResolvedFuture::NIL elsif (done = futures.find { |f| f.respond_to?(:resolved?) && f.resolved? }) done else FirstFuture.new(futures) end end |
#reduce(futures, initial_value = nil, options = nil) {|accumulator, value| ... } ⇒ Ione::Future
Returns a future that will resolve to a value which is the reduction of the values of a list of source futures.
This is essentially a parallel, streaming version of Enumerable#reduce
,
but for futures. Use this method for example when you want to do a number
of asynchronous operations in parallel and then merge the results together
when all are done.
The block will not be called concurrently, which means that unless you're handling the initial value or other values in the scope of the block you don't need (and shouldn't) do any locking to ensure that the accumulator passed to the block is safe to modify. It is, of course, even better if you don't modify the accumulator, but return a new, immutable value on each invocation.
347 348 349 350 351 352 353 |
# File 'lib/ione/future.rb', line 347 def reduce(futures, initial_value=nil, =nil, &reducer) if && [:ordered] == false UnorderedReducingFuture.new(futures, initial_value, reducer) else OrderedReducingFuture.new(futures, initial_value, reducer) end end |
#resolved(value = nil) ⇒ Ione::Future
Creates a new pre-resolved future.
359 360 361 362 |
# File 'lib/ione/future.rb', line 359 def resolved(value=nil) return ResolvedFuture::NIL if value.nil? ResolvedFuture.new(value) end |
#traverse(values) {|value| ... } ⇒ Ione::Future
Takes calls the block once for each element in an array, expecting each invocation to return a future, and returns a future that resolves to an array of the values of those futures.
292 293 294 295 296 |
# File 'lib/ione/future.rb', line 292 def traverse(values, &block) all(values.map(&block)) rescue => e failed(e) end |