Class: AWS::Flow::Core::Future
- Inherits:
-
Object
- Object
- AWS::Flow::Core::Future
- Defined in:
- lib/aws/flow/future.rb
Overview
A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete(Future#set), to wait for its completion(Future#wait), and to retrieve the result of the computation(Future#get). The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. This is okay, however, because it will block that Fiber, and another Fiber will start executing
Instance Method Summary collapse
-
#get ⇒ Object
Blocks if Future is not set raises CancellationError when task is cancelled.
-
#is_flow_future? ⇒ Boolean
determines whether the object is a flow future.
-
#on_set(&block) ⇒ Object
Add a callback, block, which will fire when the future is set.
-
#set(result = nil) ⇒ Object
Sets the value of the future, and notifies all of the Fibers that tried to get when this future wasn’t ready.
- #set? ⇒ Boolean
- #unset ⇒ Object
Instance Method Details
#get ⇒ Object
Blocks if Future is not set raises CancellationError when task is cancelled
51 52 53 54 55 56 57 |
# File 'lib/aws/flow/future.rb', line 51 def get until @set @conditional ||= FiberConditionVariable.new @conditional.wait end @result end |
#is_flow_future? ⇒ Boolean
determines whether the object is a flow future. The contract is that flow futures must have a #get method.
45 46 47 |
# File 'lib/aws/flow/future.rb', line 45 def is_flow_future? true end |
#on_set(&block) ⇒ Object
Add a callback, block, which will fire when the future is set
69 70 71 72 73 |
# File 'lib/aws/flow/future.rb', line 69 def on_set(&block) @listeners ||= [] # TODO probably want to use lambda here @listeners << block end |
#set(result = nil) ⇒ Object
Sets the value of the future, and notifies all of the Fibers that tried to get when this future wasn’t ready.
34 35 36 37 38 39 40 41 |
# File 'lib/aws/flow/future.rb', line 34 def set(result=nil) raise AlreadySetException if @set @set = true @result = result @conditional.broadcast if @conditional @listeners.each { |b| b.call(self) } if @listeners self end |
#set? ⇒ Boolean
59 60 61 |
# File 'lib/aws/flow/future.rb', line 59 def set? @set end |
#unset ⇒ Object
63 64 65 66 |
# File 'lib/aws/flow/future.rb', line 63 def unset @set = nil @result = nil end |