Skip to content

Commit 9698d2c

Browse files
authored
feat(voip): EN-6884: Support participant type in heartbeats (#95)
- Fixed typo in variable names - Formatting updates by prettier-on-save
1 parent b2f4a6d commit 9698d2c

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

src/resources/VoipHeartbeatHandler.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { v4 as uuidv4 } from 'uuid';
22

3-
const deafultHeartbeatIntervalMs = 15 * 1000;
4-
const deafultHeartbeatReadTimeoutMs = 45 * 1000;
3+
const defaultHeartbeatIntervalMs = 15 * 1000;
4+
const defaultHeartbeatReadTimeoutMs = 45 * 1000;
55

66
const validCallStates = {
77
None: 'None',
@@ -13,17 +13,17 @@ const isValidCallState = (callState, callHref) => {
1313
return false;
1414
}
1515

16-
return (callState === validCallStates.None)
17-
|| (typeof callHref === 'string' && callHref !== '');
16+
return callState === validCallStates.None || (typeof callHref === 'string' && callHref !== '');
1817
};
1918

2019
const getHeartbeat = (voipHeartbeatHandler) => {
2120
const now = new Date().toISOString();
22-
const { customerCode, sessionId, callState, callHref } = voipHeartbeatHandler;
21+
const { customerCode, sessionId, participantType, callState, callHref } = voipHeartbeatHandler;
2322
return {
2423
type: 'HEARTBEAT',
2524
at_time: now,
2625
session_id: sessionId,
26+
participant_type: participantType,
2727
customer: customerCode,
2828
current_call_state: callState,
2929
current_call_href: callHref,
@@ -44,19 +44,22 @@ class VoipHeartbeatHandler {
4444
* Track API
4545
* @param {number} [options.heartbeatReadTimeoutMs] The time to wait for receiving a heartbeat
4646
* from Track API
47+
* @param {string} [options.participantType] Indication of the type of participant that is
48+
* reporting a heartbeat. Valid values are "User" (default) and "Supervisor"
4749
*/
4850
constructor(realTimeClient, customerCode, options = {}) {
4951
this.realTimeClient = realTimeClient;
5052
this.customerCode = customerCode;
5153

5254
// uuidv4 is randomly generated.
5355
this.sessionId = uuidv4();
56+
this.participantType = options.participantType || 'User';
5457
this.callState = validCallStates.None;
5558
this.callHref = '';
5659
this.handlers = {};
5760

58-
this.heartbeatIntervalMs = options.heartbeatIntervalMs || deafultHeartbeatIntervalMs;
59-
this.heartbeatReadTimeoutMs = options.heartbeatReadTimeoutMs || deafultHeartbeatReadTimeoutMs;
61+
this.heartbeatIntervalMs = options.heartbeatIntervalMs || defaultHeartbeatIntervalMs;
62+
this.heartbeatReadTimeoutMs = options.heartbeatReadTimeoutMs || defaultHeartbeatReadTimeoutMs;
6063

6164
this.setCallState = this.setCallState.bind(this);
6265
this.sendHeartbeat = this.sendHeartbeat.bind(this);
@@ -84,10 +87,7 @@ class VoipHeartbeatHandler {
8487
throw new Error('Invalid call state.');
8588
}
8689

87-
const {
88-
callState: oldCallState,
89-
callHref: oldCallHref,
90-
} = this;
90+
const { callState: oldCallState, callHref: oldCallHref } = this;
9191

9292
this.callState = callState;
9393
this.callHref = callHref;
@@ -117,14 +117,8 @@ class VoipHeartbeatHandler {
117117
}
118118
this.heartbeatTimeoutInterval = setTimeout(this.onHeartbeatTimeout, heartbeatReadTimeoutMs);
119119

120-
const {
121-
desired_call_state: desiredCallState,
122-
desired_call_href: desiredCallHref,
123-
} = heartbeat;
124-
const {
125-
previousDesiredCallState,
126-
previousDesiredCallHref,
127-
} = this;
120+
const { desired_call_state: desiredCallState, desired_call_href: desiredCallHref } = heartbeat;
121+
const { previousDesiredCallState, previousDesiredCallHref } = this;
128122

129123
const hasDesiredCallStateChanged =
130124
desiredCallState !== previousDesiredCallState || desiredCallHref !== previousDesiredCallHref;
@@ -154,11 +148,9 @@ class VoipHeartbeatHandler {
154148
// actually sent. wait until then until we queue up the next
155149
// heartbeat.
156150
const heartbeat = getHeartbeat(self);
157-
self.realTimeClient
158-
.sendMessage(heartbeat)
159-
.then(() => {
160-
self.heartbeatTimeout = setTimeout(self.sendHeartbeat, heartbeatIntervalMs);
161-
});
151+
self.realTimeClient.sendMessage(heartbeat).then(() => {
152+
self.heartbeatTimeout = setTimeout(self.sendHeartbeat, heartbeatIntervalMs);
153+
});
162154
}
163155

164156
/**

0 commit comments

Comments
 (0)