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

Inherits:
Object
  • Object
show all
Defined in:
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

#initializeFuture

Returns a new instance of Future.



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

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

Instance Method Details

#getObject

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 'lib/aws/flow/future.rb', line 69

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

#is_flow_future?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Is the object is an AWS Flow future? AWS Flow futures must have a #get method.

Returns:

  • (Boolean)


57
58
59
# File 'lib/aws/flow/future.rb', line 57

def is_flow_future?
  true
end

#on_set(&block) ⇒ Object

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



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

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

#set(result = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets the value of the AWS::Flow::Core::Future, and notifies all of the fibers that tried to call #get when this future wasn’t ready.



45
46
47
48
49
50
51
52
# 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

#set?Boolean

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

Returns:



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

def set?
  @set
end

#unsetObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Unsets the future.



83
84
85
86
# File 'lib/aws/flow/future.rb', line 83

def unset
  @set = false
  @result = nil
end