class AWS::Flow::Core::Future
Represents the result of an asynchronous computation. Methods are provided to:
-
retrieve the result of the computation, once it is complete ({Future#get}).
-
check if the computation is complete ({Future#set?})
-
execute a block when computation is complete ({Future#on_set})
The result of a Future
can only be retrieved when the computation has completed. {Future#get} blocks execution, if necessary, until the Future
is ready. This is okay: because it will block that fiber, another fiber will start executing.
Public Class Methods
# File lib/aws/flow/future.rb, line 37 def initialize @conditional = FiberConditionVariable.new @set = false end
Public Instance Methods
Blocks if future is not set. Returns the result of the future once {#set} is true.
@return
The result of the future.
@raise CancellationError
when the task is cancelled.
# File lib/aws/flow/future.rb, line 69 def get @conditional.wait until @set @result end
Adds a callback, passed in as a block, which will fire when the future is set.
# File lib/aws/flow/future.rb, line 90 def on_set(&block) @listeners ||= [] # TODO probably want to use lambda here @listeners << block end
Sets the value of the {Future}, and notifies all of the fibers that tried to call {#get} when this future wasn’t ready. @api private
# File lib/aws/flow/future.rb, line 45 def set(result=nil) raise AlreadySetException if @set @set = true @result = result @listeners.each { |b| b.call(self) } if @listeners @conditional.broadcast if @conditional self end
@return
true if the {Future} has been set.
# File lib/aws/flow/future.rb, line 76 def set? @set end
Unsets the future.
@api private
# File lib/aws/flow/future.rb, line 83 def unset @set = false @result = nil end