class Signalwire::Relay::Calling::Component

Attributes

blocker[R]
call[R]
completed[R]
event[R]
execute_result[R]
state[R]
successful[R]

Public Class Methods

new(call:) click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 8
def initialize(call:)
  @call = call
  @completed = false
  @successful = false
  @events_waiting = []
  @call.register_component(self)
end

Public Instance Methods

after_execute(event) click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 69
def after_execute(event)
  # implemented in subclasses
end
check_for_waiting_events() click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 121
def check_for_waiting_events
  unblock(event) if @events_waiting.include?(@state)
end
create_blocker() click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 111
def create_blocker
  @blocker = Concurrent::Promises.resolvable_future
end
event_type() click to toggle source

The event type the subclass listens to

# File lib/signalwire/relay/calling/component.rb, line 29
def event_type
  raise NotImplementedError, 'Define this method on the child classes'
end
execute() click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 50
def execute
  setup_handlers
  @call.relay_execute execute_params do |event, outcome|
    handle_execute_result(event, outcome)
  end
end
execute_params(method_suffix = nil, inner_params_merge = {}) click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 33
def execute_params(method_suffix = nil, inner_params_merge = {})
  {
    protocol: @call.client.protocol,
    method: method + method_suffix.to_s,
    params: inner_params.merge(inner_params_merge)
  }
end
handle_execute_result(event, outcome) click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 63
def handle_execute_result(event, outcome)
  @execute_result = event
  after_execute(event)
  terminate if outcome == :failure
end
has_blocker?() click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 96
def has_blocker?
  !@blocker.nil?
end
inner_params() click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 41
def inner_params
  result = {
    node_id: @call.node_id,
    call_id: @call.id
  }
  result[:params] = payload if payload
  result
end
method() click to toggle source

Relay method corresponding to the command

# File lib/signalwire/relay/calling/component.rb, line 18
def method
  raise NotImplementedError, 'Define this method on the child classes'
end
notification_handler(_event) click to toggle source

This is the most important method to implement in a subclass

# File lib/signalwire/relay/calling/component.rb, line 102
def notification_handler(_event)
  # to be implemented by subclasses. An example could be:
  #
  # if event.call_params[:call_state] == 'ended'
  #   unblock
  # end
  raise NotImplementedError, 'Define this method on the child classes'
end
payload() click to toggle source

Relay payload Implement this on child classes

# File lib/signalwire/relay/calling/component.rb, line 24
def payload
  nil
end
setup_handlers() click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 57
def setup_handlers
  @call.on :event, event_type: event_type do |evt|
    notification_handler(evt)
  end
end
setup_waiting_events(events) click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 81
def setup_waiting_events(events)
  @events_waiting = events
end
terminate(event = nil) click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 73
def terminate(event = nil)
  @completed = true
  @successful = false
  @state = 'failed'
  @event = event if event
  blocker&.reject false
end
unblock(value) click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 115
def unblock(value)
  blocker&.resolve value if has_blocker? && blocker.pending?
end
wait_for(*events) click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 85
def wait_for(*events)
  setup_waiting_events(events)
  execute
  wait_on_blocker unless @completed
end
wait_on_blocker() click to toggle source
# File lib/signalwire/relay/calling/component.rb, line 91
def wait_on_blocker
  create_blocker
  blocker.wait
end