Class: AWS::Flow::Core::Future

Inherits:
Object
  • Object
show all
Defined in:
aws-flow-ruby/aws-flow/lib/aws/flow/future.rb

Overview

Represents the result of an asynchronous computation. Methods are provided to:

  • retrieve the result of the computation, once it is complete (#get).
  • check if the computation is complete (#set?)
  • execute a block when computation is complete (#on_set)

The result of a Future can only be retrieved when the computation has completed. #get blocks execution, if necessary, until the Future is ready. This is okay: because it will block that fiber, another fiber will start executing.

Direct Known Subclasses

ExternalFuture

Instance Method Summary (collapse)

Constructor Details

- (Future) initialize

Returns a new instance of Future



37
38
39
40
# File 'aws-flow-ruby/aws-flow/lib/aws/flow/future.rb', line 37

def initialize
  @conditional = FiberConditionVariable.new
  @set = false
end

Instance Method Details

- (Object) get

Blocks if future is not set. Returns the result of the future once #set is true.

Returns:

  • The result of the future.

Raises:

  • CancellationError when the task is cancelled.



69
70
71
72
# File 'aws-flow-ruby/aws-flow/lib/aws/flow/future.rb', line 69

def get
  @conditional.wait until @set
  @result
end

- (Object) on_set(&block)

Adds a callback, passed in as a block, which will fire when the future is set.



90
91
92
93
94
# File 'aws-flow-ruby/aws-flow/lib/aws/flow/future.rb', line 90

def on_set(&block)
  @listeners ||= []
  # TODO probably want to use lambda here
  @listeners << block
end

- (Boolean) set?

Returns true if the AWS::Flow::Core::Future has been set.

Returns:



76
77
78
# File 'aws-flow-ruby/aws-flow/lib/aws/flow/future.rb', line 76

def set?
  @set
end