Class: WebRTC::Promise

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Promise

Returns a new instance of Promise.



7
8
9
# File 'lib/webrtc/promise.rb', line 7

def initialize(&block)
  @future = Concurrent::Promises.future(&block)
end

Class Method Details

.reject(error) ⇒ Object



15
16
17
18
19
# File 'lib/webrtc/promise.rb', line 15

def self.reject(error)
  promise = allocate
  promise.instance_variable_set(:@future, Concurrent::Promises.rejected_future(error))
  promise
end

.resolve(value) ⇒ Object



11
12
13
# File 'lib/webrtc/promise.rb', line 11

def self.resolve(value)
  new { value }
end

Instance Method Details

#await(timeout = nil) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/webrtc/promise.rb', line 39

def await(timeout = nil)
  if timeout
    @future.value!(timeout)
  else
    @future.value!
  end
end

#catch(&block) ⇒ Object



27
28
29
30
31
# File 'lib/webrtc/promise.rb', line 27

def catch(&block)
  new_promise = Promise.allocate
  new_promise.instance_variable_set(:@future, @future.rescue(&block))
  new_promise
end

#finally(&block) ⇒ Object



33
34
35
36
37
# File 'lib/webrtc/promise.rb', line 33

def finally(&block)
  new_promise = Promise.allocate
  new_promise.instance_variable_set(:@future, @future.on_resolution { block.call })
  new_promise
end

#fulfilled?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/webrtc/promise.rb', line 51

def fulfilled?
  @future.fulfilled?
end

#pending?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/webrtc/promise.rb', line 47

def pending?
  @future.pending?
end

#rejected?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/webrtc/promise.rb', line 55

def rejected?
  @future.rejected?
end

#then(&block) ⇒ Object



21
22
23
24
25
# File 'lib/webrtc/promise.rb', line 21

def then(&block)
  new_promise = Promise.allocate
  new_promise.instance_variable_set(:@future, @future.then(&block))
  new_promise
end