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.



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

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.



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

def error
  @error
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#fulfill(value) ⇒ Object



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

def fulfill(value)
  return false unless pending?

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

#fulfilled?Boolean

Returns:

  • (Boolean)


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

def fulfilled? = @status == :fulfilled

#pending?Boolean

Returns:

  • (Boolean)


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

def pending? = @status == :pending

#reject(error) ⇒ Object



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

def reject(error)
  return false unless pending?

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

#rejected?Boolean

Returns:

  • (Boolean)


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

def rejected? = @status == :rejected

#to_sObject Also known as: inspect



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

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

#valueObject

Raises:



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

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

  nil
end

#wait(timeout: nil) ⇒ Object

Raises:



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

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

  @value
end