Class: Concurrent::Promises::Future
- Inherits:
-
AbstractEventFuture
- Object
- Synchronization::Object
- AbstractEventFuture
- Concurrent::Promises::Future
- Defined in:
- lib/concurrent/promises.rb,
lib-edge/concurrent/edge/promises.rb,
lib-edge/concurrent/edge/promises.rb,
lib-edge/concurrent/edge/throttle.rb,
lib-edge/concurrent/edge/old_channel_integration.rb
Overview
Represents a value which will become available in future. May reject with a reason instead, e.g. when the tasks raises an exception.
Direct Known Subclasses
Defined Under Namespace
Modules: ActorIntegration, FlatShortcuts, NewChannelIntegration, ThrottleIntegration
Instance Method Summary collapse
-
#any(event_or_future) ⇒ Future
(also: #|)
Creates a new event which will be resolved when the first of receiver,
event_or_future
resolves. -
#delay ⇒ Future
Creates new future dependent on receiver which will not evaluate until touched, see AbstractEventFuture#touch.
-
#exception(*args) ⇒ Exception
Allows rejected Future to be risen with
raise
method. -
#flat_event ⇒ Event
Creates new event which will be resolved when the returned event by receiver is.
-
#flat_future(level = 1) ⇒ Future
(also: #flat)
Creates new future which will have result of the future returned by receiver.
-
#fulfilled? ⇒ Boolean
Is it in fulfilled state?.
-
#on_fulfillment(*args, &callback) ⇒ self
Shortcut of #on_fulfillment_using with default
:io
executor supplied. -
#on_fulfillment!(*args) {|value, *args| ... } ⇒ self
Stores the callback to be executed synchronously on resolving thread after it is fulfilled.
-
#on_fulfillment_using(executor, *args) {|value, *args| ... } ⇒ self
Stores the callback to be executed asynchronously on executor after it is fulfilled.
-
#on_rejection(*args, &callback) ⇒ self
Shortcut of #on_rejection_using with default
:io
executor supplied. -
#on_rejection!(*args) {|reason, *args| ... } ⇒ self
Stores the callback to be executed synchronously on resolving thread after it is rejected.
-
#on_rejection_using(executor, *args) {|reason, *args| ... } ⇒ self
Stores the callback to be executed asynchronously on executor after it is rejected.
-
#reason(timeout = nil) ⇒ Exception?
Returns reason of future's rejection.
-
#rejected? ⇒ Boolean
Is it in rejected state?.
-
#rescue(*args, &task) ⇒ Future
Shortcut of #rescue_on with default
:io
executor supplied. -
#rescue_on(executor, *args) {|reason, *args| ... } ⇒ Future
Chains the task to be executed asynchronously on executor after it rejects.
-
#rescue_throttled_by(throttle, *args, &block) ⇒ Future
included
from ThrottleIntegration
Behaves as #rescue but the it is throttled.
-
#result(timeout = nil) ⇒ Array(Boolean, Object, Exception)?
Returns triplet fulfilled?, value, reason.
-
#run ⇒ Future
Allows to use futures as green threads.
-
#schedule(intended_time) ⇒ Future
Creates new event dependent on receiver scheduled to execute on/in intended_time.
-
#then(*args, &task) ⇒ Future
Shortcut of #then_on with default
:io
executor supplied. -
#then_ask(actor) ⇒ Future
included
from ActorIntegration
Asks the actor with its value.
- #then_flat_event(*args, &block) ⇒ Event included from FlatShortcuts
- #then_flat_event_on(executor, *args, &block) ⇒ Event included from FlatShortcuts
- #then_flat_future(*args, &block) ⇒ Future (also: #then_flat) included from FlatShortcuts
- #then_flat_future_on(executor, *args, &block) ⇒ Future (also: #then_flat_on) included from FlatShortcuts
-
#then_on(executor, *args) {|value, *args| ... } ⇒ Future
Chains the task to be executed asynchronously on executor after it fulfills.
-
#then_push_channel(channel) ⇒ Future
included
from NewChannelIntegration
A future which is fulfilled after the message is pushed to the channel.
-
#then_throttled_by(throttle, *args, &block) ⇒ Future
included
from ThrottleIntegration
Behaves as #then but the it is throttled.
-
#to_event ⇒ Event
Converts future to event which is resolved when future is resolved by fulfillment or rejection.
-
#to_future ⇒ Future
Returns self, since this is a future.
-
#value(timeout = nil) ⇒ Object?
Return value of the future.
-
#value!(timeout = nil) ⇒ Object?
Return value of the future.
-
#wait!(timeout = nil) ⇒ Future, true, false
Wait (block the Thread) until receiver is AbstractEventFuture#resolved?.
-
#with_default_executor(executor) ⇒ Future
Crates new object with same class with the executor set as its new default executor.
-
#zip(other) ⇒ Future
(also: #&)
Creates a new event or a future which will be resolved when receiver and other are.
Instance Method Details
#any(event_or_future) ⇒ Future Also known as: |
Creates a new event which will be resolved when the first of receiver, event_or_future
resolves. Returning future will have value nil if event_or_future is event and resolves
first.
1023 1024 1025 |
# File 'lib/concurrent/promises.rb', line 1023 def any(event_or_future) AnyResolvedFuturePromise.new_blocked_by2(self, event_or_future, @DefaultExecutor).future end |
#delay ⇒ Future
Creates new future dependent on receiver which will not evaluate until touched, see AbstractEventFuture#touch. In other words, it inserts delay into the chain of Futures making rest of it lazy evaluated.
1033 1034 1035 1036 |
# File 'lib/concurrent/promises.rb', line 1033 def delay event = DelayPromise.new(@DefaultExecutor).event ZipFutureEventPromise.new_blocked_by2(self, event, @DefaultExecutor).future end |
#exception(*args) ⇒ Exception
Allows rejected Future to be risen with raise
method.
956 957 958 959 960 961 962 963 964 965 966 967 968 |
# File 'lib/concurrent/promises.rb', line 956 def exception(*args) raise Concurrent::Error, 'it is not rejected' unless rejected? reason = Array(internal_state.reason).flatten.compact if reason.size > 1 ex = Concurrent::MultipleErrors.new reason ex.set_backtrace(caller) ex else ex = reason[0].clone.exception(*args) ex.set_backtrace Array(ex.backtrace) + caller ex end end |
#flat_event ⇒ Event
Creates new event which will be resolved when the returned event by receiver is. Be careful if the receiver rejects it will just resolve since Event does not hold reason.
1068 1069 1070 |
# File 'lib/concurrent/promises.rb', line 1068 def flat_event FlatEventPromise.new_blocked_by1(self, @DefaultExecutor).event end |
#flat_future(level = 1) ⇒ Future Also known as: flat
Creates new future which will have result of the future returned by receiver. If receiver rejects it will have its rejection.
1058 1059 1060 |
# File 'lib/concurrent/promises.rb', line 1058 def flat_future(level = 1) FlatFuturePromise.new_blocked_by1(self, level, @DefaultExecutor).future end |
#fulfilled? ⇒ Boolean
Is it in fulfilled state?
886 887 888 889 |
# File 'lib/concurrent/promises.rb', line 886 def fulfilled? state = internal_state state.resolved? && state.fulfilled? end |
#on_fulfillment(*args, &callback) ⇒ self
Shortcut of #on_fulfillment_using with default :io
executor supplied.
1074 1075 1076 |
# File 'lib/concurrent/promises.rb', line 1074 def on_fulfillment(*args, &callback) on_fulfillment_using @DefaultExecutor, *args, &callback end |
#on_fulfillment!(*args) {|value, *args| ... } ⇒ self
Stores the callback to be executed synchronously on resolving thread after it is fulfilled. Does nothing on rejection.
1085 1086 1087 |
# File 'lib/concurrent/promises.rb', line 1085 def on_fulfillment!(*args, &callback) add_callback :callback_on_fulfillment, args, callback end |
#on_fulfillment_using(executor, *args) {|value, *args| ... } ⇒ self
Stores the callback to be executed asynchronously on executor after it is fulfilled. Does nothing on rejection.
1097 1098 1099 |
# File 'lib/concurrent/promises.rb', line 1097 def on_fulfillment_using(executor, *args, &callback) add_callback :async_callback_on_fulfillment, executor, args, callback end |
#on_rejection(*args, &callback) ⇒ self
Shortcut of #on_rejection_using with default :io
executor supplied.
1103 1104 1105 |
# File 'lib/concurrent/promises.rb', line 1103 def on_rejection(*args, &callback) on_rejection_using @DefaultExecutor, *args, &callback end |
#on_rejection!(*args) {|reason, *args| ... } ⇒ self
Stores the callback to be executed synchronously on resolving thread after it is rejected. Does nothing on fulfillment.
1114 1115 1116 |
# File 'lib/concurrent/promises.rb', line 1114 def on_rejection!(*args, &callback) add_callback :callback_on_rejection, args, callback end |
#on_rejection_using(executor, *args) {|reason, *args| ... } ⇒ self
Stores the callback to be executed asynchronously on executor after it is rejected. Does nothing on fulfillment.
1126 1127 1128 |
# File 'lib/concurrent/promises.rb', line 1126 def on_rejection_using(executor, *args, &callback) add_callback :async_callback_on_rejection, executor, args, callback end |
#reason(timeout = nil) ⇒ Exception?
This function potentially blocks current thread until the Future is resolved. Be careful it can deadlock. Try to chain instead.
Make sure returned nil
is not confused with timeout, no value when rejected,
no reason when fulfilled, etc.
Use more exact methods if needed, like AbstractEventFuture#wait, #value!, #result, etc.
Returns reason of future's rejection. Calls AbstractEventFuture#touch.
922 923 924 |
# File 'lib/concurrent/promises.rb', line 922 def reason(timeout = nil) internal_state.reason if wait_until_resolved timeout end |
#rejected? ⇒ Boolean
Is it in rejected state?
893 894 895 896 |
# File 'lib/concurrent/promises.rb', line 893 def rejected? state = internal_state state.resolved? && !state.fulfilled? end |
#rescue(*args, &task) ⇒ Future
Shortcut of #rescue_on with default :io
executor supplied.
990 991 992 |
# File 'lib/concurrent/promises.rb', line 990 def rescue(*args, &task) rescue_on @DefaultExecutor, *args, &task end |
#rescue_on(executor, *args) {|reason, *args| ... } ⇒ Future
Chains the task to be executed asynchronously on executor after it rejects. Does not run the task if it fulfills. It will resolve though, triggering any dependent futures.
1002 1003 1004 |
# File 'lib/concurrent/promises.rb', line 1002 def rescue_on(executor, *args, &task) RescuePromise.new_blocked_by1(self, @DefaultExecutor, executor, args, &task).future end |
#rescue_throttled_by(throttle, *args, &block) ⇒ Future Originally defined in module ThrottleIntegration
Behaves as Concurrent::Promises::Future#rescue but the it is throttled.
#result(timeout = nil) ⇒ Array(Boolean, Object, Exception)?
This function potentially blocks current thread until the Future is resolved. Be careful it can deadlock. Try to chain instead.
Returns triplet fulfilled?, value, reason. Calls AbstractEventFuture#touch.
933 934 935 |
# File 'lib/concurrent/promises.rb', line 933 def result(timeout = nil) internal_state.result if wait_until_resolved timeout end |
#run ⇒ Future
Allows to use futures as green threads. The receiver has to evaluate to a future which represents what should be done next. It basically flattens indefinitely until non Future values is returned which becomes result of the returned future. Any encountered exception will become reason of the returned future.
1142 1143 1144 |
# File 'lib/concurrent/promises.rb', line 1142 def run RunFuturePromise.new_blocked_by1(self, @DefaultExecutor).future end |
#schedule(intended_time) ⇒ Future
Creates new event dependent on receiver scheduled to execute on/in intended_time. In time is interpreted from the moment the receiver is resolved, therefore it inserts delay into the chain.
1040 1041 1042 1043 1044 1045 |
# File 'lib/concurrent/promises.rb', line 1040 def schedule(intended_time) chain do event = ScheduledPromise.new(@DefaultExecutor, intended_time).event ZipFutureEventPromise.new_blocked_by2(self, event, @DefaultExecutor).future end.flat end |
#then(*args, &task) ⇒ Future
Shortcut of #then_on with default :io
executor supplied.
972 973 974 |
# File 'lib/concurrent/promises.rb', line 972 def then(*args, &task) then_on @DefaultExecutor, *args, &task end |
#then_ask(actor) ⇒ Future Originally defined in module ActorIntegration
Asks the actor with its value.
#then_flat_event(*args, &block) ⇒ Event Originally defined in module FlatShortcuts
#then_flat_event_on(executor, *args, &block) ⇒ Event Originally defined in module FlatShortcuts
#then_flat_future(*args, &block) ⇒ Future Also known as: then_flat Originally defined in module FlatShortcuts
#then_flat_future_on(executor, *args, &block) ⇒ Future Also known as: then_flat_on Originally defined in module FlatShortcuts
#then_on(executor, *args) {|value, *args| ... } ⇒ Future
Chains the task to be executed asynchronously on executor after it fulfills. Does not run the task if it rejects. It will resolve though, triggering any dependent futures.
984 985 986 |
# File 'lib/concurrent/promises.rb', line 984 def then_on(executor, *args, &task) ThenPromise.new_blocked_by1(self, @DefaultExecutor, executor, args, &task).future end |
#then_push_channel(channel) ⇒ Future Originally defined in module NewChannelIntegration
Returns a future which is fulfilled after the message is pushed to the channel. May take a moment if the channel is full.
#then_throttled_by(throttle, *args, &block) ⇒ Future Originally defined in module ThrottleIntegration
Behaves as Concurrent::Promises::Future#then but the it is throttled.
#to_event ⇒ Event
Converts future to event which is resolved when future is resolved by fulfillment or rejection.
1154 1155 1156 1157 1158 |
# File 'lib/concurrent/promises.rb', line 1154 def to_event event = Promises.resolvable_event ensure chain_resolvable(event) end |
#to_future ⇒ Future
Returns self, since this is a future
1162 1163 1164 |
# File 'lib/concurrent/promises.rb', line 1162 def to_future self end |
#value(timeout = nil) ⇒ Object?
This function potentially blocks current thread until the Future is resolved. Be careful it can deadlock. Try to chain instead.
Make sure returned nil
is not confused with timeout, no value when rejected,
no reason when fulfilled, etc.
Use more exact methods if needed, like AbstractEventFuture#wait, #value!, #result, etc.
Return value of the future. Calls AbstractEventFuture#touch.
911 912 913 |
# File 'lib/concurrent/promises.rb', line 911 def value(timeout = nil) internal_state.value if wait_until_resolved timeout end |
#value!(timeout = nil) ⇒ Object?
This function potentially blocks current thread until the Future is resolved. Be careful it can deadlock. Try to chain instead.
Make sure returned nil
is not confused with timeout, no value when rejected,
no reason when fulfilled, etc.
Use more exact methods if needed, like AbstractEventFuture#wait, #value!, #result, etc.
Return value of the future. Calls AbstractEventFuture#touch.
947 948 949 |
# File 'lib/concurrent/promises.rb', line 947 def value!(timeout = nil) internal_state.value if wait_until_resolved! timeout end |
#wait!(timeout = nil) ⇒ Future, true, false
This function potentially blocks current thread until the Future is resolved. Be careful it can deadlock. Try to chain instead.
Wait (block the Thread) until receiver is AbstractEventFuture#resolved?. Calls AbstractEventFuture#touch.
939 940 941 942 |
# File 'lib/concurrent/promises.rb', line 939 def wait!(timeout = nil) result = wait_until_resolved!(timeout) timeout ? result : self end |
#with_default_executor(executor) ⇒ Future
Crates new object with same class with the executor set as its new default executor. Any futures depending on it will use the new default executor.
1049 1050 1051 |
# File 'lib/concurrent/promises.rb', line 1049 def with_default_executor(executor) FutureWrapperPromise.new_blocked_by1(self, executor).future end |
#zip(other) ⇒ Future Also known as: &
Creates a new event or a future which will be resolved when receiver and other are. Returns an event if receiver and other are events, otherwise returns a future. If just one of the parties is Future then the result of the returned future is equal to the result of the supplied future. If both are futures then the result is as described in Concurrent::Promises::FactoryMethods#zip_futures_on.
1008 1009 1010 1011 1012 1013 1014 |
# File 'lib/concurrent/promises.rb', line 1008 def zip(other) if other.is_a?(Future) ZipFuturesPromise.new_blocked_by2(self, other, @DefaultExecutor).future else ZipFutureEventPromise.new_blocked_by2(self, other, @DefaultExecutor).future end end |