Class: Pulsar::Internal::Promise
- Inherits:
-
Object
- Object
- Pulsar::Internal::Promise
- Defined in:
- lib/pulsar/internal/promise.rb
Overview
Thread-safe one-shot completion primitive for async broker responses.
Instance Method Summary collapse
- #completed? ⇒ Boolean
- #fulfill(value) ⇒ Object
-
#initialize ⇒ Promise
constructor
A new instance of Promise.
- #reject(error) ⇒ Object
- #wait(timeout:) ⇒ Object
Constructor Details
#initialize ⇒ Promise
Returns a new instance of Promise.
7 8 9 10 11 12 13 |
# File 'lib/pulsar/internal/promise.rb', line 7 def initialize @mutex = Mutex.new @condition = ConditionVariable.new @completed = false @value = nil @error = nil end |
Instance Method Details
#completed? ⇒ Boolean
33 34 35 |
# File 'lib/pulsar/internal/promise.rb', line 33 def completed? @mutex.synchronize { @completed } end |
#fulfill(value) ⇒ Object
15 16 17 |
# File 'lib/pulsar/internal/promise.rb', line 15 def fulfill(value) complete(value, nil) end |
#reject(error) ⇒ Object
19 20 21 |
# File 'lib/pulsar/internal/promise.rb', line 19 def reject(error) complete(nil, error) end |
#wait(timeout:) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/pulsar/internal/promise.rb', line 23 def wait(timeout:) @mutex.synchronize do @condition.wait(@mutex, timeout) unless @completed raise TimeoutError, 'operation timed out' unless @completed raise @error if @error @value end end |