Class: RobotLab::DelegationFuture
- Inherits:
-
Object
- Object
- RobotLab::DelegationFuture
- Defined in:
- lib/robot_lab/delegation_future.rb
Overview
A promise-like object returned by robot.delegate(async: true).
The delegated task runs in a background thread. The caller can check whether the result is ready with resolved? and block until it arrives with value (or its alias wait).
Defined Under Namespace
Classes: DelegationTimeout
Instance Attribute Summary collapse
-
#delegated_by ⇒ String
readonly
Name of the robot that created this future.
-
#robot_name ⇒ String
readonly
Name of the robot that was delegated to.
Instance Method Summary collapse
-
#initialize(robot_name:, delegated_by:) ⇒ DelegationFuture
constructor
A new instance of DelegationFuture.
- #reject!(error) ⇒ Object
- #resolve!(result) ⇒ Object
-
#resolved? ⇒ Boolean
True once the delegated task has completed (successfully or with an error).
-
#value(timeout: nil) ⇒ RobotResult
(also: #wait)
Block until the result is available and return it.
Constructor Details
#initialize(robot_name:, delegated_by:) ⇒ DelegationFuture
Returns a new instance of DelegationFuture.
34 35 36 37 38 39 40 41 42 |
# File 'lib/robot_lab/delegation_future.rb', line 34 def initialize(robot_name:, delegated_by:) @robot_name = robot_name @delegated_by = delegated_by @mutex = Mutex.new @cv = ConditionVariable.new @result = nil @error = nil @resolved = false end |
Instance Attribute Details
#delegated_by ⇒ String (readonly)
Returns name of the robot that created this future.
30 31 32 |
# File 'lib/robot_lab/delegation_future.rb', line 30 def delegated_by @delegated_by end |
#robot_name ⇒ String (readonly)
Returns name of the robot that was delegated to.
27 28 29 |
# File 'lib/robot_lab/delegation_future.rb', line 27 def robot_name @robot_name end |
Instance Method Details
#reject!(error) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/robot_lab/delegation_future.rb', line 85 def reject!(error) @mutex.synchronize do @error = error @resolved = true @cv.broadcast end end |
#resolve!(result) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/robot_lab/delegation_future.rb', line 76 def resolve!(result) @mutex.synchronize do @result = result @resolved = true @cv.broadcast end end |
#resolved? ⇒ Boolean
True once the delegated task has completed (successfully or with an error).
47 48 49 |
# File 'lib/robot_lab/delegation_future.rb', line 47 def resolved? @mutex.synchronize { @resolved } end |
#value(timeout: nil) ⇒ RobotResult Also known as: wait
Block until the result is available and return it.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/robot_lab/delegation_future.rb', line 57 def value(timeout: nil) @mutex.synchronize do unless @resolved @cv.wait(@mutex, timeout) end unless @resolved raise DelegationTimeout, "Delegation to '#{@robot_name}' timed out after #{timeout}s" end raise @error if @error @result end end |