Represents a value which will become available in future. May fail with a reason instead.
@!visibility private
# File lib/concurrent/edge/future.rb, line 740 def add_callback(method, *args) state = @State.get if completed?(state) call_callback method, state, *args else @Callbacks.push [method, *args] state = @State.get # take back if it was completed in the meanwhile call_callbacks state if completed?(state) end self end
@return [Future] which has first completed value from futures
# File lib/concurrent/edge/future.rb, line 645 def any(*futures) AnyPromise.new([self, *futures], @DefaultExecutor).future end
@!visibility private
# File lib/concurrent/edge/future.rb, line 754 def apply(block) @State.get.apply block end
# File lib/concurrent/edge/future.rb, line 625 def chain_completable(completable_future) on_completion! { completable_future.complete_with internal_state } end
@!visibility private
# File lib/concurrent/edge/future.rb, line 722 def complete_with(state, raise_on_reassign = true) if @State.compare_and_set(PENDING, state) @Waiters.clear synchronize { ns_broadcast } call_callbacks state else if raise_on_reassign log ERROR, 'Edge::Future', reason if reason # print otherwise hidden error raise(Concurrent::MultipleAssignmentError.new( "Future can be completed only once. Current result is #{result}, " + "trying to set #{state.result}")) end return false end self end
Inserts delay into the chain of Futures making rest of it lazy evaluated. @return [Future]
# File lib/concurrent/edge/future.rb, line 651 def delay ZipFutureEventPromise.new(self, Delay.new(@DefaultExecutor).future, @DefaultExecutor).future end
@example allows failed Future to be risen
raise Concurrent.future.fail
# File lib/concurrent/edge/future.rb, line 602 def exception(*args) raise 'obligation is not failed' unless failed? reason = @State.get.reason if reason.is_a?(Array) reason.each { |e| log Error, 'Edge::Future', e } Concurrent::Error.new 'multiple exceptions, inspect log' else reason.exception(*args) end end
Has Future been failed? @return [Boolean]
# File lib/concurrent/edge/future.rb, line 543 def failed?(state = @State.get) state.completed? && !state.success? end
zips with the Future in the value @example
Concurrent.future { Concurrent.future { 1 } }.flat.vale # => 1
# File lib/concurrent/edge/future.rb, line 640 def flat(level = 1) FlatPromise.new(self, level, @DefaultExecutor).future end
# File lib/concurrent/edge/future.rb, line 536 def fulfilled? deprecated_method 'fulfilled?', 'success?' success? end
@yield [reason] executed async on `executor` when failed? @return self
# File lib/concurrent/edge/future.rb, line 705 def on_failure(executor = nil, &callback) add_callback :pr_async_callback_on_failure, executor || @DefaultExecutor, callback end
@yield [reason] executed sync when failed? @return self
# File lib/concurrent/edge/future.rb, line 717 def on_failure!(&callback) add_callback :pr_callback_on_failure, callback end
@yield [value] executed async on `executor` when success @return self
# File lib/concurrent/edge/future.rb, line 699 def on_success(executor = nil, &callback) add_callback :pr_async_callback_on_success, executor || @DefaultExecutor, callback end
@yield [value] executed sync when success @return self
# File lib/concurrent/edge/future.rb, line 711 def on_success!(&callback) add_callback :pr_callback_on_success, callback end
@return [Exception, nil] the reason of the Future's failure @!macro edge.timeout_nil @!macro edge.periodical_wait
# File lib/concurrent/edge/future.rb, line 565 def reason(timeout = nil) touch @State.get.reason if wait_until_complete timeout end
# File lib/concurrent/edge/future.rb, line 547 def rejected? deprecated_method 'rejected?', 'failed?' failed? end
@yield [reason] executed only on parent failure @return [Future]
# File lib/concurrent/edge/future.rb, line 633 def rescue(executor = nil, &callback) RescuePromise.new(self, @DefaultExecutor, executor || @DefaultExecutor, &callback).future end
@return [Array(Boolean, Object, Exception), nil] triplet of success, value, reason @!macro edge.timeout_nil @!macro edge.periodical_wait
# File lib/concurrent/edge/future.rb, line 573 def result(timeout = nil) touch @State.get.result if wait_until_complete timeout end
Schedules rest of the chain for execution with specified time or on specified time @return [Future]
# File lib/concurrent/edge/future.rb, line 657 def schedule(intended_time) chain do ZipFutureEventPromise.new(self, ScheduledPromise.new(@DefaultExecutor, intended_time).event, @DefaultExecutor).future end.flat end
Has Future been success? @return [Boolean]
# File lib/concurrent/edge/future.rb, line 532 def success?(state = @State.get) state.completed? && state.success? end
@yield [value] executed only on parent success @return [Future]
# File lib/concurrent/edge/future.rb, line 615 def then(executor = nil, &callback) ThenPromise.new(self, @DefaultExecutor, executor || @DefaultExecutor, &callback).future end
Asks the actor with its value. @return [Future] new future with the response form the actor
# File lib/concurrent/edge/future.rb, line 621 def then_ask(actor) self.then { |v| actor.ask(v) }.flat end
@note may block @note only proof of concept
# File lib/concurrent/edge/future.rb, line 693 def then_push(channel) on_success(:io) { |value| channel.push value } end
Zips with selected value form the suplied channels @return [Future]
# File lib/concurrent/edge/future.rb, line 667 def then_select(*channels) ZipPromise.new([self, Concurrent.select(*channels)], @DefaultExecutor).future end
@return [Object, nil] the value of the Future when success, nil on timeout @!macro [attach] edge.timeout_nil
@note If the Future can have value `nil` then it cannot be distinquished from `nil` returned on timeout. In this case is better to use first `wait` then `value` (or similar).
@!macro edge.periodical_wait
# File lib/concurrent/edge/future.rb, line 557 def value(timeout = nil) touch @State.get.value if wait_until_complete timeout end
Wait until Future is complete? @param [Numeric] timeout the maximum time in second to wait. @raise reason on failure @return [Object, nil] @!macro edge.timeout_nil @!macro edge.periodical_wait
# File lib/concurrent/edge/future.rb, line 595 def value!(timeout = nil) touch @State.get.value if wait_until_complete! timeout end
Wait until Future is complete? @param [Numeric] timeout the maximum time in second to wait. @raise reason on failure @return [Event, true, false] self or true/false if timeout is used @!macro edge.periodical_wait
# File lib/concurrent/edge/future.rb, line 583 def wait!(timeout = nil) touch result = wait_until_complete!(timeout) timeout ? result : self end
Changes default executor for rest of the chain @return [Future]
# File lib/concurrent/edge/future.rb, line 673 def with_default_executor(executor) FutureWrapperPromise.new(self, executor).future end
Zip with future producing new Future @return [Future]
# File lib/concurrent/edge/future.rb, line 679 def zip(other) if other.is_a?(Future) ZipFutureFuturePromise.new(self, other, @DefaultExecutor).future else ZipFutureEventPromise.new(self, other, @DefaultExecutor).future end end
Generated with the Darkfish Rdoc Generator 2.