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.
230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/ione/future.rb', line 230 def after(*futures) if futures.size == 1 && (fs = futures.first).is_a?(Enumerable) *futures = *fs end 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 |
# 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([]) else CombinedFuture.new(futures) end end |
#failed(error) ⇒ Ione::Future
Creates a new pre-failed future.
363 364 365 |
# File 'lib/ione/future.rb', line 363 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).
260 261 262 263 264 265 266 267 268 269 |
# File 'lib/ione/future.rb', line 260 def first(*futures) if futures.size == 1 && (fs = futures.first).is_a?(Enumerable) futures = fs end if futures.count == 0 resolved 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.
342 343 344 345 346 347 348 |
# File 'lib/ione/future.rb', line 342 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.
354 355 356 357 |
# File 'lib/ione/future.rb', line 354 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.
287 288 289 290 291 |
# File 'lib/ione/future.rb', line 287 def traverse(values, &block) all(values.map(&block)) rescue => e failed(e) end |