Class: NatsAsync::Client::RequestPromise

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:) ⇒ RequestPromise

Returns a new instance of RequestPromise.



28
29
30
31
32
33
34
# File 'lib/nats_async/client.rb', line 28

def initialize(id:)
  @id = id
  @status = :pending
  @condition = Async::Condition.new
  @value = nil
  @error = nil
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



26
27
28
# File 'lib/nats_async/client.rb', line 26

def error
  @error
end

#idObject (readonly)

Returns the value of attribute id.



26
27
28
# File 'lib/nats_async/client.rb', line 26

def id
  @id
end

#statusObject (readonly)

Returns the value of attribute status.



26
27
28
# File 'lib/nats_async/client.rb', line 26

def status
  @status
end

Instance Method Details

#fulfill(value) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/nats_async/client.rb', line 56

def fulfill(value)
  return false unless pending?

  @value = value
  @status = :fulfilled
  @condition.signal
  true
end

#fulfilled?Boolean

Returns:

  • (Boolean)


38
# File 'lib/nats_async/client.rb', line 38

def fulfilled? = @status == :fulfilled

#pending?Boolean

Returns:

  • (Boolean)


36
# File 'lib/nats_async/client.rb', line 36

def pending? = @status == :pending

#reject(error) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/nats_async/client.rb', line 65

def reject(error)
  return false unless pending?

  @error = error
  @status = :rejected
  @condition.signal
  true
end

#rejected?Boolean

Returns:

  • (Boolean)


40
# File 'lib/nats_async/client.rb', line 40

def rejected? = @status == :rejected

#to_sObject Also known as: inspect



74
75
76
# File 'lib/nats_async/client.rb', line 74

def to_s
  "#<#{self.class.name} id=#{id} status=#{status}>"
end

#valueObject

Raises:



49
50
51
52
53
54
# File 'lib/nats_async/client.rb', line 49

def value
  return @value if fulfilled?
  raise @error if rejected?

  nil
end

#wait(timeout: nil) ⇒ Object

Raises:



42
43
44
45
46
47
# File 'lib/nats_async/client.rb', line 42

def wait(timeout: nil)
  wait_for_completion(timeout: timeout) if pending?
  raise @error if rejected?

  @value
end