Class: ESM::Connection::Promise

Inherits:
Object
  • Object
show all
Defined in:
lib/esm/connection/promise.rb

Instance Method Summary collapse

Constructor Details

#initializePromise

Returns a new instance of Promise.



6
7
8
9
# File 'lib/esm/connection/promise.rb', line 6

def initialize
  @inner = Concurrent::MVar.new
  @promise = Concurrent::Promise.new
end

Instance Method Details

#set_response(response) ⇒ Object



11
12
13
14
# File 'lib/esm/connection/promise.rb', line 11

def set_response(response)
  @inner.put(response)
  true
end

#thenObject



16
17
18
19
# File 'lib/esm/connection/promise.rb', line 16

def then(&)
  @promise = @promise.then(&)
  self
end

#wait_for_response(timeout = 0) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/esm/connection/promise.rb', line 23

def wait_for_response(timeout = 0)
  self.then do |_|
    case (result = @inner.take(timeout))
    when Request
      Response.fulfilled(result.content)
    when StandardError
      Response.rejected(result)
    else
      # Concurrent::MVar::TIMEOUT
      Response.rejected(ESM::Exception::RequestTimeout.new)
    end
  end

  execute if @promise.state == :unscheduled
  @promise = @promise.wait

  if @promise.fulfilled?
    @promise.value
  else
    Response.rejected(@promise.reason)
  end
end