Class: Circus::Agents::Promise

Inherits:
Object
  • Object
show all
Defined in:
lib/circus/agents/client.rb

Overview

A promise provides a reference to an expected future result. Client calls return immediately upon message dispatch - in order to retrieve the final result, the promise can be inspected.

Instance Method Summary collapse

Constructor Details

#initializePromise

Returns a new instance of Promise.



52
53
54
55
56
57
# File 'lib/circus/agents/client.rb', line 52

def initialize
  @monitor = Monitor.new
  @cond = @monitor.new_cond
  @completed = false
  @result = nil
end

Instance Method Details

#completed!(result) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/circus/agents/client.rb', line 59

def completed!(result)
  @monitor.synchronize do
    @completed = true
    @result = result
    @cond.signal
  end
end

#resultObject



67
68
69
70
71
72
73
74
# File 'lib/circus/agents/client.rb', line 67

def result
  @monitor.synchronize do
    return @result if @completed
    
    @cond.wait
    @result
  end
end