Class: Signalwire::Relay::Calling::Component
- Inherits:
-
Object
- Object
- Signalwire::Relay::Calling::Component
show all
- Includes:
- Logger
- Defined in:
- lib/signalwire/relay/calling/component.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Logger
#level=, #logger, logger
Constructor Details
#initialize(call:) ⇒ Component
Returns a new instance of Component.
8
9
10
11
12
13
14
|
# 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
|
Instance Attribute Details
#blocker ⇒ Object
Returns the value of attribute blocker.
119
120
121
|
# File 'lib/signalwire/relay/calling/component.rb', line 119
def blocker
@blocker
end
|
#call ⇒ Object
Returns the value of attribute call.
6
7
8
|
# File 'lib/signalwire/relay/calling/component.rb', line 6
def call
@call
end
|
#completed ⇒ Object
Returns the value of attribute completed.
6
7
8
|
# File 'lib/signalwire/relay/calling/component.rb', line 6
def completed
@completed
end
|
#event ⇒ Object
Returns the value of attribute event.
6
7
8
|
# File 'lib/signalwire/relay/calling/component.rb', line 6
def event
@event
end
|
#execute_result ⇒ Object
Returns the value of attribute execute_result.
6
7
8
|
# File 'lib/signalwire/relay/calling/component.rb', line 6
def execute_result
@execute_result
end
|
#state ⇒ Object
Returns the value of attribute state.
6
7
8
|
# File 'lib/signalwire/relay/calling/component.rb', line 6
def state
@state
end
|
#successful ⇒ Object
Returns the value of attribute successful.
6
7
8
|
# File 'lib/signalwire/relay/calling/component.rb', line 6
def successful
@successful
end
|
Instance Method Details
#after_execute(event) ⇒ Object
69
70
71
|
# File 'lib/signalwire/relay/calling/component.rb', line 69
def after_execute(event)
end
|
#check_for_waiting_events ⇒ Object
121
122
123
|
# File 'lib/signalwire/relay/calling/component.rb', line 121
def check_for_waiting_events
unblock(event) if @events_waiting.include?(@state)
end
|
#create_blocker ⇒ Object
111
112
113
|
# File 'lib/signalwire/relay/calling/component.rb', line 111
def create_blocker
@blocker = Concurrent::Promises.resolvable_future
end
|
#event_type ⇒ Object
The event type the subclass listens to
29
30
31
|
# File 'lib/signalwire/relay/calling/component.rb', line 29
def event_type
raise NotImplementedError, 'Define this method on the child classes'
end
|
#execute ⇒ Object
50
51
52
53
54
55
|
# 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 = {}) ⇒ Object
33
34
35
36
37
38
39
|
# 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) ⇒ Object
63
64
65
66
67
|
# 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? ⇒ Boolean
96
97
98
|
# File 'lib/signalwire/relay/calling/component.rb', line 96
def has_blocker?
!@blocker.nil?
end
|
#inner_params ⇒ Object
41
42
43
44
45
46
47
48
|
# 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 ⇒ Object
Relay method corresponding to the command
18
19
20
|
# File 'lib/signalwire/relay/calling/component.rb', line 18
def method
raise NotImplementedError, 'Define this method on the child classes'
end
|
#notification_handler(_event) ⇒ Object
This is the most important method to implement in a subclass
102
103
104
105
106
107
108
109
|
# File 'lib/signalwire/relay/calling/component.rb', line 102
def notification_handler(_event)
raise NotImplementedError, 'Define this method on the child classes'
end
|
#payload ⇒ Object
Relay payload Implement this on child classes
24
25
26
|
# File 'lib/signalwire/relay/calling/component.rb', line 24
def payload
nil
end
|
#setup_handlers ⇒ Object
57
58
59
60
61
|
# 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) ⇒ Object
81
82
83
|
# File 'lib/signalwire/relay/calling/component.rb', line 81
def setup_waiting_events(events)
@events_waiting = events
end
|
#terminate(event = nil) ⇒ Object
73
74
75
76
77
78
79
|
# 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) ⇒ Object
115
116
117
|
# File 'lib/signalwire/relay/calling/component.rb', line 115
def unblock(value)
blocker&.resolve value if has_blocker? && blocker.pending?
end
|
#wait_for(*events) ⇒ Object
85
86
87
88
89
|
# 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 ⇒ Object
91
92
93
94
|
# File 'lib/signalwire/relay/calling/component.rb', line 91
def wait_on_blocker
create_blocker
blocker.wait
end
|