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.
-
#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
45 46 47 48 49 50 51 |
# File 'lib/aws/flow/future.rb', line 45 def get until @set @conditional ||= FiberConditionVariable.new @conditional.wait end @result end |
#on_set(&block) ⇒ Object
Add a callback, block, which will fire when the future is set
63 64 65 66 67 |
# File 'lib/aws/flow/future.rb', line 63 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
53 54 55 |
# File 'lib/aws/flow/future.rb', line 53 def set? @set end |
#unset ⇒ Object
57 58 59 60 |
# File 'lib/aws/flow/future.rb', line 57 def unset @set = nil @result = nil end |