@microsoft/omnichannel-chat-widget 2.0.1-main.2b5f4df → 2.0.1-main.38c3c85

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -114,6 +114,7 @@ let TelemetryEvent = exports.TelemetryEvent = /*#__PURE__*/function (TelemetryEv
114
114
  TelemetryEvent["RecoveryEligibilityEvaluated"] = "RecoveryEligibilityEvaluated";
115
115
  TelemetryEvent["GetConversationDetailsCallStarted"] = "GetConversationDetailsCallStarted";
116
116
  TelemetryEvent["GetConversationDetailsCallFailed"] = "GetConversationDetailsCallFailed";
117
+ TelemetryEvent["UpdateTelemetryDataFailed"] = "UpdateTelemetryDataFailed";
117
118
  TelemetryEvent["EndChatSDKCallFailed"] = "EndChatSDKCallFailed";
118
119
  TelemetryEvent["DisconnectEndChatSDKCallFailed"] = "DisconnectEndChatSDKCallFailed";
119
120
  TelemetryEvent["GetChatReconnectContextSDKCallStarted"] = "GetChatReconnectContextSDKCallStarted";
@@ -430,6 +431,7 @@ let TelemetryConstants = exports.TelemetryConstants = /*#__PURE__*/function () {
430
431
  case TelemetryEvent.OnNewMessageFailed:
431
432
  case TelemetryEvent.OnNewMessageAudioNotificationFailed:
432
433
  case TelemetryEvent.GetConversationDetailsCallFailed:
434
+ case TelemetryEvent.UpdateTelemetryDataFailed:
433
435
  case TelemetryEvent.EndChatSDKCall:
434
436
  case TelemetryEvent.EndChatEventReceived:
435
437
  case TelemetryEvent.EndChatSDKCallFailed:
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.isReconnectEnabled = exports.isPersistentEnabled = exports.handleChatReconnect = exports.getChatReconnectContext = void 0;
6
+ exports.isReconnectTriggered = exports.isReconnectEnabled = exports.isPersistentEnabled = exports.handleChatReconnect = exports.getChatReconnectContext = void 0;
7
7
  require("regenerator-runtime/runtime");
8
8
  var _TelemetryConstants = require("../../../common/telemetry/TelemetryConstants");
9
9
  var _Constants = require("../../../common/Constants");
@@ -13,6 +13,7 @@ var _omnichannelChatComponents = require("@microsoft/omnichannel-chat-components
13
13
  var _ConversationState = require("../../../contexts/common/ConversationState");
14
14
  var _LiveChatWidgetActionType = require("../../../contexts/common/LiveChatWidgetActionType");
15
15
  var _TelemetryHelper = require("../../../common/telemetry/TelemetryHelper");
16
+ var _createReducer = require("../../../contexts/createReducer");
16
17
  var _startChatErrorHandler = require("./startChatErrorHandler");
17
18
  // Return value: should start normal chat flow when reconnect is enabled
18
19
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -55,8 +56,35 @@ const handleChatReconnect = async (facadeChatSDK, props, dispatch, setAdapter, i
55
56
  return true;
56
57
  };
57
58
 
59
+ /**
60
+ * Returns true when chat reconnect took over the start: it started the chat itself or showed the reconnect pane.
61
+ * In that case the caller must not start another chat.
62
+ */
58
63
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
64
  exports.handleChatReconnect = handleChatReconnect;
65
+ const isReconnectTriggered = async (facadeChatSDK, props, dispatch, setAdapter, initStartChat, state) => {
66
+ if (isReconnectEnabled(props.chatConfig) === true && !isPersistentEnabled(props.chatConfig)) {
67
+ let reconnectChatStarted = false;
68
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
69
+ const initStartChatAndTrack = async function () {
70
+ reconnectChatStarted = await initStartChat(...arguments);
71
+ };
72
+ const noValidReconnectId = await handleChatReconnect(facadeChatSDK, props, dispatch, setAdapter, initStartChatAndTrack, state);
73
+ // initStartChat resolves before React renders Active, so the in-memory state can still read Loading here.
74
+ // Its result tells whether reconnect started the chat; the in-memory state covers the reconnect pane.
75
+ const inMemoryState = (0, _createReducer.executeReducer)(state, {
76
+ type: _LiveChatWidgetActionType.LiveChatWidgetActionType.GET_IN_MEMORY_STATE,
77
+ payload: null
78
+ });
79
+ if (!noValidReconnectId && (reconnectChatStarted || inMemoryState.appStates.conversationState === _ConversationState.ConversationState.Active || inMemoryState.appStates.conversationState === _ConversationState.ConversationState.ReconnectChat)) {
80
+ return true;
81
+ }
82
+ }
83
+ return false;
84
+ };
85
+
86
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
87
+ exports.isReconnectTriggered = isReconnectTriggered;
60
88
  const getChatReconnectContext = async (facadeChatSDK, chatConfig, props, isAuthenticatedChat, dispatch) => {
61
89
  try {
62
90
  var _props$reconnectChatP4;
@@ -68,7 +68,7 @@ const getGuard = (guards, facadeChatSDK) => {
68
68
  * Runs `operation` only if no other invocation for the same widget instance is already in
69
69
  * flight. Concurrent invocations are dropped (and reported through telemetry) rather than
70
70
  * awaiting the in-flight promise, so a future re-entrant call can never deadlock on itself.
71
- * No caller acts on the resolved value, so dropping is safe.
71
+ * A dropped invocation resolves to `undefined`.
72
72
  *
73
73
  * `blockedBy` lets an operation also yield to a different, already running operation for the
74
74
  * same widget instance without claiming that operation's lock.
@@ -84,14 +84,14 @@ const runExclusively = async (guards, facadeChatSDK, description, operation, pro
84
84
  WidgetCacheId: props ? (0, _utils.getWidgetCacheIdfromProps)(props) : undefined
85
85
  }
86
86
  });
87
- return;
87
+ return undefined;
88
88
  }
89
89
 
90
90
  // Claim the lock before invoking the operation, so a re-entrant call made from the
91
91
  // operation's synchronous preamble (before its first await) cannot slip past the check above.
92
92
  guard.inFlight = true;
93
93
  try {
94
- await operation();
94
+ return await operation();
95
95
  } finally {
96
96
  guard.inFlight = false;
97
97
  }
@@ -228,10 +228,15 @@ const setPreChatAndInitiateChat = async (facadeChatSDK, dispatch, setAdapter, is
228
228
  await initStartChat(facadeChatSDK, dispatch, setAdapter, state, props, optionalParams);
229
229
  };
230
230
 
231
+ /**
232
+ * Resolves to `true` when the chat reached Active, and `false` when it failed or the call was dropped.
233
+ * It resolves before React renders Active, so callers must use this value rather than reading the conversation state.
234
+ */
231
235
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
232
236
  exports.setPreChatAndInitiateChat = setPreChatAndInitiateChat;
233
237
  const initStartChat = async (facadeChatSDK, dispatch, setAdapter, state, props, params, persistedState) => {
234
- return runExclusively(initStartChatGuards, facadeChatSDK, "Concurrent initStartChat call ignored, a start chat operation is already in progress for this widget instance.", () => initStartChatInternal(facadeChatSDK, dispatch, setAdapter, state, props, params, persistedState), props);
238
+ const chatStarted = await runExclusively(initStartChatGuards, facadeChatSDK, "Concurrent initStartChat call ignored, a start chat operation is already in progress for this widget instance.", () => initStartChatInternal(facadeChatSDK, dispatch, setAdapter, state, props, params, persistedState), props);
239
+ return chatStarted === true;
235
240
  };
236
241
 
237
242
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -346,7 +351,7 @@ const initStartChatInternal = async (facadeChatSDK, dispatch, setAdapter, state,
346
351
  (0, _startChatErrorHandler.logWidgetLoadComplete)(_Constants.WidgetLoadTelemetryMessage.PersistedStateRetrievedMessage);
347
352
  // Set post chat context in state, load in background to do not block the load
348
353
  (0, _setPostChatContextAndLoadSurvey.setPostChatContextAndLoadSurvey)(facadeChatSDK, dispatch, true);
349
- return;
354
+ return true;
350
355
  }
351
356
 
352
357
  // Persistent Chat relies on the `reconnectId` retrieved from reconnectablechats API to reconnect upon start chat and not `liveChatContext`
@@ -362,10 +367,13 @@ const initStartChatInternal = async (facadeChatSDK, dispatch, setAdapter, state,
362
367
  (0, _startChatErrorHandler.logWidgetLoadComplete)();
363
368
  // Set post chat context in state, load in background to do not block the load
364
369
  (0, _setPostChatContextAndLoadSurvey.setPostChatContextAndLoadSurvey)(facadeChatSDK, dispatch);
365
- // Updating chat session detail for telemetry
366
- await (0, _updateSessionDataForTelemetry.updateTelemetryData)(facadeChatSDK, dispatch);
370
+ // Updating chat session detail for telemetry, in background: getConversationDetails retries until the
371
+ // live work item exists (several seconds), and awaiting it would hold the start chat locks and drop the next start chat
372
+ (0, _updateSessionDataForTelemetry.updateTelemetryData)(facadeChatSDK, dispatch);
373
+ return true;
367
374
  } catch (ex) {
368
375
  (0, _startChatErrorHandler.handleStartChatError)(dispatch, facadeChatSDK, props, ex, isStartChatSuccessful);
376
+ return false;
369
377
  }
370
378
  };
371
379
 
@@ -12,16 +12,42 @@ var _TelemetryManager = require("../../../common/telemetry/TelemetryManager");
12
12
  var _utils = require("../../../common/utils");
13
13
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
14
  const updateTelemetryData = async (facadeChatSDK, dispatch) => {
15
- // load it concurrently, this will reduce the load time
16
- await Promise.all([updateSessionDataForTelemetry(facadeChatSDK, dispatch), updateConversationDataForTelemetry(facadeChatSDK, dispatch)]);
15
+ var _facadeChatSDK$getCha;
16
+ // startChat no longer waits for this, so the next chat can start (chatSDKStateCleanUp assigns a new requestId)
17
+ // before these calls resolve. Results for an earlier chat must not overwrite the current chat's data.
18
+ // endChat also assigns a new requestId, so the chat that just ended still applies its own results.
19
+ const requestId = facadeChatSDK === null || facadeChatSDK === void 0 || (_facadeChatSDK$getCha = facadeChatSDK.getChatSDK()) === null || _facadeChatSDK$getCha === void 0 ? void 0 : _facadeChatSDK$getCha.requestId;
20
+ try {
21
+ // load it concurrently, this will reduce the load time
22
+ await Promise.all([updateSessionDataForTelemetry(facadeChatSDK, dispatch, requestId), updateConversationDataForTelemetry(facadeChatSDK, dispatch, requestId)]);
23
+ } catch (error) {
24
+ _TelemetryHelper.TelemetryHelper.logSDKEvent(_TelemetryConstants.LogLevel.ERROR, {
25
+ Event: _TelemetryConstants.TelemetryEvent.UpdateTelemetryDataFailed,
26
+ ExceptionDetails: {
27
+ exception: `Update telemetry data failed: ${error}`
28
+ }
29
+ });
30
+ }
17
31
  };
18
32
 
19
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
+ // The requestId alone isn't enough: endChat also replaces it. Only the next chat's startChat gets a new chat token
34
+ // (endChat and chatSDKStateCleanUp clear it), so drop the result only when the SDK holds that next chat.
20
35
  exports.updateTelemetryData = updateTelemetryData;
21
- const updateSessionDataForTelemetry = async (facadeChatSDK, dispatch) => {
36
+ const isFromEarlierChat = (facadeChatSDK, requestId) => {
37
+ var _chatSDK$chatToken;
38
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
+ const chatSDK = facadeChatSDK.getChatSDK();
40
+ return (chatSDK === null || chatSDK === void 0 ? void 0 : chatSDK.requestId) !== requestId && !!(chatSDK !== null && chatSDK !== void 0 && (_chatSDK$chatToken = chatSDK.chatToken) !== null && _chatSDK$chatToken !== void 0 && _chatSDK$chatToken.chatId);
41
+ };
42
+
43
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
+ const updateSessionDataForTelemetry = async (facadeChatSDK, dispatch, requestId) => {
22
45
  if (facadeChatSDK && facadeChatSDK.getChatSDK()) {
23
46
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
47
  const chatSession = await facadeChatSDK.getCurrentLiveChatContext();
48
+ if (isFromEarlierChat(facadeChatSDK, requestId)) {
49
+ return;
50
+ }
25
51
  const telemetryData = _TelemetryHelper.TelemetryHelper.addSessionDataToTelemetry(chatSession, _TelemetryManager.TelemetryManager.InternalTelemetryData);
26
52
  dispatch({
27
53
  type: _LiveChatWidgetActionType.LiveChatWidgetActionType.SET_TELEMETRY_DATA,
@@ -37,10 +63,13 @@ const updateSessionDataForTelemetry = async (facadeChatSDK, dispatch) => {
37
63
  };
38
64
 
39
65
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
- const updateConversationDataForTelemetry = async (facadeChatSDK, dispatch) => {
66
+ const updateConversationDataForTelemetry = async (facadeChatSDK, dispatch, requestId) => {
41
67
  if (facadeChatSDK && facadeChatSDK.getChatSDK()) {
42
68
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
69
  const liveWorkItem = await (0, _utils.getConversationDetailsCall)(facadeChatSDK);
70
+ if (isFromEarlierChat(facadeChatSDK, requestId)) {
71
+ return;
72
+ }
44
73
  const telemetryData = _TelemetryHelper.TelemetryHelper.addConversationDataToTelemetry(liveWorkItem, _TelemetryManager.TelemetryManager.InternalTelemetryData);
45
74
  dispatch({
46
75
  type: _LiveChatWidgetActionType.LiveChatWidgetActionType.SET_TELEMETRY_DATA,
@@ -163,20 +163,6 @@ const LiveChatWidgetStateful = props => {
163
163
 
164
164
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
165
165
  const startChat = async (props, localState) => {
166
- const isReconnectTriggered = async () => {
167
- if ((0, _reconnectChatHelper.isReconnectEnabled)(props.chatConfig) === true && !(0, _reconnectChatHelper.isPersistentEnabled)(props.chatConfig)) {
168
- const noValidReconnectId = await (0, _reconnectChatHelper.handleChatReconnect)(facadeChatSDK, props, dispatch, setAdapter, _startChat.initStartChat, state);
169
- const inMemoryState = (0, _createReducer.executeReducer)(state, {
170
- type: _LiveChatWidgetActionType.LiveChatWidgetActionType.GET_IN_MEMORY_STATE,
171
- payload: null
172
- });
173
- // If chat reconnect has kicked in chat state will become Active or Reconnect. So just exit, else go next
174
- if (!noValidReconnectId && (inMemoryState.appStates.conversationState === _ConversationState.ConversationState.Active || inMemoryState.appStates.conversationState === _ConversationState.ConversationState.ReconnectChat)) {
175
- return true;
176
- }
177
- }
178
- return false;
179
- };
180
166
  let isChatValid = false;
181
167
  //Start a chat from cache/reconnectid
182
168
  if (activeCachedChatExist === true) {
@@ -188,7 +174,7 @@ const LiveChatWidgetStateful = props => {
188
174
  //Check if conversation state is not in wrapup or closed state
189
175
  isChatValid = await (0, _startChat.checkIfConversationStillValid)(facadeChatSDK, dispatch, state);
190
176
  if (isChatValid === true) {
191
- const reconnectTriggered = await isReconnectTriggered();
177
+ const reconnectTriggered = await (0, _reconnectChatHelper.isReconnectTriggered)(facadeChatSDK, props, dispatch, setAdapter, _startChat.initStartChat, state);
192
178
  if (!reconnectTriggered) {
193
179
  await (0, _startChat.initStartChat)(facadeChatSDK, dispatch, setAdapter, state, props, optionalParams);
194
180
  }
@@ -204,7 +190,7 @@ const LiveChatWidgetStateful = props => {
204
190
  if (isChatValid === false) {
205
191
  if (localState) {
206
192
  // adding the reconnect logic for the case when customer tries to reconnect from a new browser or InPrivate browser
207
- const reconnectTriggered = await isReconnectTriggered();
193
+ const reconnectTriggered = await (0, _reconnectChatHelper.isReconnectTriggered)(facadeChatSDK, props, dispatch, setAdapter, _startChat.initStartChat, state);
208
194
  if (!reconnectTriggered) {
209
195
  const inMemoryState = (0, _createReducer.executeReducer)(state, {
210
196
  type: _LiveChatWidgetActionType.LiveChatWidgetActionType.GET_IN_MEMORY_STATE,
@@ -1000,7 +986,9 @@ const LiveChatWidgetStateful = props => {
1000
986
  const endChatRelay = (adapter, skipEndChatSDK, skipCloseChat, postMessageToOtherTab) => (0, _endChat.endChat)(props, facadeChatSDK, state, dispatch, setAdapter, setWebChatStyles, adapter, skipEndChatSDK, skipCloseChat, postMessageToOtherTab);
1001
987
  const prepareStartChatRelay = () => (0, _startChat.prepareStartChat)(props, facadeChatSDK, state, dispatch, setAdapter);
1002
988
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1003
- const initStartChatRelay = (optionalParams, persistedState) => (0, _startChat.initStartChat)(facadeChatSDK, dispatch, setAdapter, state, props, optionalParams, persistedState);
989
+ const initStartChatRelay = async (optionalParams, persistedState) => {
990
+ await (0, _startChat.initStartChat)(facadeChatSDK, dispatch, setAdapter, state, props, optionalParams, persistedState);
991
+ };
1004
992
  const confirmationPaneProps = (0, _initConfirmationPropsComposer.initConfirmationPropsComposer)(props);
1005
993
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1006
994
  const prepareEndChatRelay = () => (0, _endChat.prepareEndChat)(props, facadeChatSDK, state, dispatch, setAdapter, setWebChatStyles, adapter);
@@ -112,6 +112,7 @@ export let TelemetryEvent = /*#__PURE__*/function (TelemetryEvent) {
112
112
  TelemetryEvent["RecoveryEligibilityEvaluated"] = "RecoveryEligibilityEvaluated";
113
113
  TelemetryEvent["GetConversationDetailsCallStarted"] = "GetConversationDetailsCallStarted";
114
114
  TelemetryEvent["GetConversationDetailsCallFailed"] = "GetConversationDetailsCallFailed";
115
+ TelemetryEvent["UpdateTelemetryDataFailed"] = "UpdateTelemetryDataFailed";
115
116
  TelemetryEvent["EndChatSDKCallFailed"] = "EndChatSDKCallFailed";
116
117
  TelemetryEvent["DisconnectEndChatSDKCallFailed"] = "DisconnectEndChatSDKCallFailed";
117
118
  TelemetryEvent["GetChatReconnectContextSDKCallStarted"] = "GetChatReconnectContextSDKCallStarted";
@@ -428,6 +429,7 @@ export let TelemetryConstants = /*#__PURE__*/function () {
428
429
  case TelemetryEvent.OnNewMessageFailed:
429
430
  case TelemetryEvent.OnNewMessageAudioNotificationFailed:
430
431
  case TelemetryEvent.GetConversationDetailsCallFailed:
432
+ case TelemetryEvent.UpdateTelemetryDataFailed:
431
433
  case TelemetryEvent.EndChatSDKCall:
432
434
  case TelemetryEvent.EndChatEventReceived:
433
435
  case TelemetryEvent.EndChatSDKCallFailed:
@@ -7,6 +7,7 @@ import { BroadcastService } from "@microsoft/omnichannel-chat-components";
7
7
  import { ConversationState } from "../../../contexts/common/ConversationState";
8
8
  import { LiveChatWidgetActionType } from "../../../contexts/common/LiveChatWidgetActionType";
9
9
  import { TelemetryHelper } from "../../../common/telemetry/TelemetryHelper";
10
+ import { executeReducer } from "../../../contexts/createReducer";
10
11
  import { handleStartChatError } from "./startChatErrorHandler";
11
12
 
12
13
  // Return value: should start normal chat flow when reconnect is enabled
@@ -50,6 +51,32 @@ const handleChatReconnect = async (facadeChatSDK, props, dispatch, setAdapter, i
50
51
  return true;
51
52
  };
52
53
 
54
+ /**
55
+ * Returns true when chat reconnect took over the start: it started the chat itself or showed the reconnect pane.
56
+ * In that case the caller must not start another chat.
57
+ */
58
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
+ const isReconnectTriggered = async (facadeChatSDK, props, dispatch, setAdapter, initStartChat, state) => {
60
+ if (isReconnectEnabled(props.chatConfig) === true && !isPersistentEnabled(props.chatConfig)) {
61
+ let reconnectChatStarted = false;
62
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
63
+ const initStartChatAndTrack = async function () {
64
+ reconnectChatStarted = await initStartChat(...arguments);
65
+ };
66
+ const noValidReconnectId = await handleChatReconnect(facadeChatSDK, props, dispatch, setAdapter, initStartChatAndTrack, state);
67
+ // initStartChat resolves before React renders Active, so the in-memory state can still read Loading here.
68
+ // Its result tells whether reconnect started the chat; the in-memory state covers the reconnect pane.
69
+ const inMemoryState = executeReducer(state, {
70
+ type: LiveChatWidgetActionType.GET_IN_MEMORY_STATE,
71
+ payload: null
72
+ });
73
+ if (!noValidReconnectId && (reconnectChatStarted || inMemoryState.appStates.conversationState === ConversationState.Active || inMemoryState.appStates.conversationState === ConversationState.ReconnectChat)) {
74
+ return true;
75
+ }
76
+ }
77
+ return false;
78
+ };
79
+
53
80
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
81
  const getChatReconnectContext = async (facadeChatSDK, chatConfig, props, isAuthenticatedChat, dispatch) => {
55
82
  try {
@@ -143,4 +170,4 @@ const isPersistentEnabled = chatConfig => {
143
170
  const hasReconnectId = reconnectAvailabilityResponse => {
144
171
  return reconnectAvailabilityResponse && !isNullOrUndefined(reconnectAvailabilityResponse.reconnectId);
145
172
  };
146
- export { handleChatReconnect, isReconnectEnabled, isPersistentEnabled, getChatReconnectContext };
173
+ export { handleChatReconnect, isReconnectTriggered, isReconnectEnabled, isPersistentEnabled, getChatReconnectContext };
@@ -62,7 +62,7 @@ const getGuard = (guards, facadeChatSDK) => {
62
62
  * Runs `operation` only if no other invocation for the same widget instance is already in
63
63
  * flight. Concurrent invocations are dropped (and reported through telemetry) rather than
64
64
  * awaiting the in-flight promise, so a future re-entrant call can never deadlock on itself.
65
- * No caller acts on the resolved value, so dropping is safe.
65
+ * A dropped invocation resolves to `undefined`.
66
66
  *
67
67
  * `blockedBy` lets an operation also yield to a different, already running operation for the
68
68
  * same widget instance without claiming that operation's lock.
@@ -78,14 +78,14 @@ const runExclusively = async (guards, facadeChatSDK, description, operation, pro
78
78
  WidgetCacheId: props ? getWidgetCacheIdfromProps(props) : undefined
79
79
  }
80
80
  });
81
- return;
81
+ return undefined;
82
82
  }
83
83
 
84
84
  // Claim the lock before invoking the operation, so a re-entrant call made from the
85
85
  // operation's synchronous preamble (before its first await) cannot slip past the check above.
86
86
  guard.inFlight = true;
87
87
  try {
88
- await operation();
88
+ return await operation();
89
89
  } finally {
90
90
  guard.inFlight = false;
91
91
  }
@@ -221,9 +221,14 @@ const setPreChatAndInitiateChat = async (facadeChatSDK, dispatch, setAdapter, is
221
221
  await initStartChat(facadeChatSDK, dispatch, setAdapter, state, props, optionalParams);
222
222
  };
223
223
 
224
+ /**
225
+ * Resolves to `true` when the chat reached Active, and `false` when it failed or the call was dropped.
226
+ * It resolves before React renders Active, so callers must use this value rather than reading the conversation state.
227
+ */
224
228
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
225
229
  const initStartChat = async (facadeChatSDK, dispatch, setAdapter, state, props, params, persistedState) => {
226
- return runExclusively(initStartChatGuards, facadeChatSDK, "Concurrent initStartChat call ignored, a start chat operation is already in progress for this widget instance.", () => initStartChatInternal(facadeChatSDK, dispatch, setAdapter, state, props, params, persistedState), props);
230
+ const chatStarted = await runExclusively(initStartChatGuards, facadeChatSDK, "Concurrent initStartChat call ignored, a start chat operation is already in progress for this widget instance.", () => initStartChatInternal(facadeChatSDK, dispatch, setAdapter, state, props, params, persistedState), props);
231
+ return chatStarted === true;
227
232
  };
228
233
 
229
234
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -337,7 +342,7 @@ const initStartChatInternal = async (facadeChatSDK, dispatch, setAdapter, state,
337
342
  logWidgetLoadComplete(WidgetLoadTelemetryMessage.PersistedStateRetrievedMessage);
338
343
  // Set post chat context in state, load in background to do not block the load
339
344
  setPostChatContextAndLoadSurvey(facadeChatSDK, dispatch, true);
340
- return;
345
+ return true;
341
346
  }
342
347
 
343
348
  // Persistent Chat relies on the `reconnectId` retrieved from reconnectablechats API to reconnect upon start chat and not `liveChatContext`
@@ -353,10 +358,13 @@ const initStartChatInternal = async (facadeChatSDK, dispatch, setAdapter, state,
353
358
  logWidgetLoadComplete();
354
359
  // Set post chat context in state, load in background to do not block the load
355
360
  setPostChatContextAndLoadSurvey(facadeChatSDK, dispatch);
356
- // Updating chat session detail for telemetry
357
- await updateTelemetryData(facadeChatSDK, dispatch);
361
+ // Updating chat session detail for telemetry, in background: getConversationDetails retries until the
362
+ // live work item exists (several seconds), and awaiting it would hold the start chat locks and drop the next start chat
363
+ updateTelemetryData(facadeChatSDK, dispatch);
364
+ return true;
358
365
  } catch (ex) {
359
366
  handleStartChatError(dispatch, facadeChatSDK, props, ex, isStartChatSuccessful);
367
+ return false;
360
368
  }
361
369
  };
362
370
 
@@ -1,4 +1,4 @@
1
- import { BroadcastEvent } from "../../../common/telemetry/TelemetryConstants";
1
+ import { BroadcastEvent, LogLevel, TelemetryEvent } from "../../../common/telemetry/TelemetryConstants";
2
2
  import { BroadcastService } from "@microsoft/omnichannel-chat-components";
3
3
  import { LiveChatWidgetActionType } from "../../../contexts/common/LiveChatWidgetActionType";
4
4
  import { TelemetryHelper } from "../../../common/telemetry/TelemetryHelper";
@@ -7,15 +7,41 @@ import { getConversationDetailsCall } from "../../../common/utils";
7
7
 
8
8
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
9
  export const updateTelemetryData = async (facadeChatSDK, dispatch) => {
10
- // load it concurrently, this will reduce the load time
11
- await Promise.all([updateSessionDataForTelemetry(facadeChatSDK, dispatch), updateConversationDataForTelemetry(facadeChatSDK, dispatch)]);
10
+ var _facadeChatSDK$getCha;
11
+ // startChat no longer waits for this, so the next chat can start (chatSDKStateCleanUp assigns a new requestId)
12
+ // before these calls resolve. Results for an earlier chat must not overwrite the current chat's data.
13
+ // endChat also assigns a new requestId, so the chat that just ended still applies its own results.
14
+ const requestId = facadeChatSDK === null || facadeChatSDK === void 0 || (_facadeChatSDK$getCha = facadeChatSDK.getChatSDK()) === null || _facadeChatSDK$getCha === void 0 ? void 0 : _facadeChatSDK$getCha.requestId;
15
+ try {
16
+ // load it concurrently, this will reduce the load time
17
+ await Promise.all([updateSessionDataForTelemetry(facadeChatSDK, dispatch, requestId), updateConversationDataForTelemetry(facadeChatSDK, dispatch, requestId)]);
18
+ } catch (error) {
19
+ TelemetryHelper.logSDKEvent(LogLevel.ERROR, {
20
+ Event: TelemetryEvent.UpdateTelemetryDataFailed,
21
+ ExceptionDetails: {
22
+ exception: `Update telemetry data failed: ${error}`
23
+ }
24
+ });
25
+ }
26
+ };
27
+
28
+ // The requestId alone isn't enough: endChat also replaces it. Only the next chat's startChat gets a new chat token
29
+ // (endChat and chatSDKStateCleanUp clear it), so drop the result only when the SDK holds that next chat.
30
+ const isFromEarlierChat = (facadeChatSDK, requestId) => {
31
+ var _chatSDK$chatToken;
32
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
+ const chatSDK = facadeChatSDK.getChatSDK();
34
+ return (chatSDK === null || chatSDK === void 0 ? void 0 : chatSDK.requestId) !== requestId && !!(chatSDK !== null && chatSDK !== void 0 && (_chatSDK$chatToken = chatSDK.chatToken) !== null && _chatSDK$chatToken !== void 0 && _chatSDK$chatToken.chatId);
12
35
  };
13
36
 
14
37
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
- const updateSessionDataForTelemetry = async (facadeChatSDK, dispatch) => {
38
+ const updateSessionDataForTelemetry = async (facadeChatSDK, dispatch, requestId) => {
16
39
  if (facadeChatSDK && facadeChatSDK.getChatSDK()) {
17
40
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
41
  const chatSession = await facadeChatSDK.getCurrentLiveChatContext();
42
+ if (isFromEarlierChat(facadeChatSDK, requestId)) {
43
+ return;
44
+ }
19
45
  const telemetryData = TelemetryHelper.addSessionDataToTelemetry(chatSession, TelemetryManager.InternalTelemetryData);
20
46
  dispatch({
21
47
  type: LiveChatWidgetActionType.SET_TELEMETRY_DATA,
@@ -31,10 +57,13 @@ const updateSessionDataForTelemetry = async (facadeChatSDK, dispatch) => {
31
57
  };
32
58
 
33
59
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
- const updateConversationDataForTelemetry = async (facadeChatSDK, dispatch) => {
60
+ const updateConversationDataForTelemetry = async (facadeChatSDK, dispatch, requestId) => {
35
61
  if (facadeChatSDK && facadeChatSDK.getChatSDK()) {
36
62
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
63
  const liveWorkItem = await getConversationDetailsCall(facadeChatSDK);
64
+ if (isFromEarlierChat(facadeChatSDK, requestId)) {
65
+ return;
66
+ }
38
67
  const telemetryData = TelemetryHelper.addConversationDataToTelemetry(liveWorkItem, TelemetryManager.InternalTelemetryData);
39
68
  dispatch({
40
69
  type: LiveChatWidgetActionType.SET_TELEMETRY_DATA,
@@ -17,7 +17,7 @@ import { checkIfConversationStillValid, initStartChat, prepareStartChat, setPreC
17
17
  import { createTimer, getBroadcastChannelName, getConversationDetailsCall, getLocaleDirection, getStateFromCache, getWidgetCacheIdfromProps, getWidgetEndChatEventName, isNullOrEmptyString, isNullOrUndefined, isThisSessionPopout, isUndefinedOrEmpty, setAriaHiddenForSiblings, setOcUserAgent } from "../../../common/utils";
18
18
  import { customEventCallback, subscribeToSendCustomEvent } from "../common/customEventHandler";
19
19
  import { defaultClientDataStoreProvider, isCookieAllowed } from "../../../common/storage/default/defaultClientDataStoreProvider";
20
- import { handleChatReconnect, isPersistentEnabled, isReconnectEnabled } from "../common/reconnectChatHelper";
20
+ import { isPersistentEnabled, isReconnectTriggered } from "../common/reconnectChatHelper";
21
21
  import { shouldShowCallingContainer, shouldShowChatButton, shouldShowConfirmationPane, shouldShowEmailTranscriptPane, shouldShowHeader, shouldShowLoadingPane, shouldShowOutOfOfficeHoursPane, shouldShowPostChatLoadingPane, shouldShowPostChatSurveyPane, shouldShowPreChatSurveyPane, shouldShowProactiveChatPane, shouldShowReconnectChatPane, shouldShowStartChatErrorPane, shouldShowWebChatContainer } from "../../../controller/componentController";
22
22
  import { ActivityStreamHandler } from "../common/ActivityStreamHandler";
23
23
  import CallingContainerStateful from "../../callingcontainerstateful/CallingContainerStateful";
@@ -156,20 +156,6 @@ export const LiveChatWidgetStateful = props => {
156
156
 
157
157
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
158
158
  const startChat = async (props, localState) => {
159
- const isReconnectTriggered = async () => {
160
- if (isReconnectEnabled(props.chatConfig) === true && !isPersistentEnabled(props.chatConfig)) {
161
- const noValidReconnectId = await handleChatReconnect(facadeChatSDK, props, dispatch, setAdapter, initStartChat, state);
162
- const inMemoryState = executeReducer(state, {
163
- type: LiveChatWidgetActionType.GET_IN_MEMORY_STATE,
164
- payload: null
165
- });
166
- // If chat reconnect has kicked in chat state will become Active or Reconnect. So just exit, else go next
167
- if (!noValidReconnectId && (inMemoryState.appStates.conversationState === ConversationState.Active || inMemoryState.appStates.conversationState === ConversationState.ReconnectChat)) {
168
- return true;
169
- }
170
- }
171
- return false;
172
- };
173
159
  let isChatValid = false;
174
160
  //Start a chat from cache/reconnectid
175
161
  if (activeCachedChatExist === true) {
@@ -181,7 +167,7 @@ export const LiveChatWidgetStateful = props => {
181
167
  //Check if conversation state is not in wrapup or closed state
182
168
  isChatValid = await checkIfConversationStillValid(facadeChatSDK, dispatch, state);
183
169
  if (isChatValid === true) {
184
- const reconnectTriggered = await isReconnectTriggered();
170
+ const reconnectTriggered = await isReconnectTriggered(facadeChatSDK, props, dispatch, setAdapter, initStartChat, state);
185
171
  if (!reconnectTriggered) {
186
172
  await initStartChat(facadeChatSDK, dispatch, setAdapter, state, props, optionalParams);
187
173
  }
@@ -197,7 +183,7 @@ export const LiveChatWidgetStateful = props => {
197
183
  if (isChatValid === false) {
198
184
  if (localState) {
199
185
  // adding the reconnect logic for the case when customer tries to reconnect from a new browser or InPrivate browser
200
- const reconnectTriggered = await isReconnectTriggered();
186
+ const reconnectTriggered = await isReconnectTriggered(facadeChatSDK, props, dispatch, setAdapter, initStartChat, state);
201
187
  if (!reconnectTriggered) {
202
188
  const inMemoryState = executeReducer(state, {
203
189
  type: LiveChatWidgetActionType.GET_IN_MEMORY_STATE,
@@ -993,7 +979,9 @@ export const LiveChatWidgetStateful = props => {
993
979
  const endChatRelay = (adapter, skipEndChatSDK, skipCloseChat, postMessageToOtherTab) => endChat(props, facadeChatSDK, state, dispatch, setAdapter, setWebChatStyles, adapter, skipEndChatSDK, skipCloseChat, postMessageToOtherTab);
994
980
  const prepareStartChatRelay = () => prepareStartChat(props, facadeChatSDK, state, dispatch, setAdapter);
995
981
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
996
- const initStartChatRelay = (optionalParams, persistedState) => initStartChat(facadeChatSDK, dispatch, setAdapter, state, props, optionalParams, persistedState);
982
+ const initStartChatRelay = async (optionalParams, persistedState) => {
983
+ await initStartChat(facadeChatSDK, dispatch, setAdapter, state, props, optionalParams, persistedState);
984
+ };
997
985
  const confirmationPaneProps = initConfirmationPropsComposer(props);
998
986
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
999
987
  const prepareEndChatRelay = () => prepareEndChat(props, facadeChatSDK, state, dispatch, setAdapter, setWebChatStyles, adapter);
@@ -98,6 +98,7 @@ export declare enum TelemetryEvent {
98
98
  RecoveryEligibilityEvaluated = "RecoveryEligibilityEvaluated",
99
99
  GetConversationDetailsCallStarted = "GetConversationDetailsCallStarted",
100
100
  GetConversationDetailsCallFailed = "GetConversationDetailsCallFailed",
101
+ UpdateTelemetryDataFailed = "UpdateTelemetryDataFailed",
101
102
  EndChatSDKCallFailed = "EndChatSDKCallFailed",
102
103
  DisconnectEndChatSDKCallFailed = "DisconnectEndChatSDKCallFailed",
103
104
  GetChatReconnectContextSDKCallStarted = "GetChatReconnectContextSDKCallStarted",
@@ -6,7 +6,12 @@ import { ILiveChatWidgetAction } from "../../../contexts/common/ILiveChatWidgetA
6
6
  import { ILiveChatWidgetContext } from "../../../contexts/common/ILiveChatWidgetContext";
7
7
  import { ILiveChatWidgetProps } from "../interfaces/ILiveChatWidgetProps";
8
8
  declare const handleChatReconnect: (facadeChatSDK: FacadeChatSDK, props: ILiveChatWidgetProps, dispatch: Dispatch<ILiveChatWidgetAction>, setAdapter: any, initStartChat: any, state: ILiveChatWidgetContext) => Promise<boolean>;
9
+ /**
10
+ * Returns true when chat reconnect took over the start: it started the chat itself or showed the reconnect pane.
11
+ * In that case the caller must not start another chat.
12
+ */
13
+ declare const isReconnectTriggered: (facadeChatSDK: FacadeChatSDK, props: ILiveChatWidgetProps, dispatch: Dispatch<ILiveChatWidgetAction>, setAdapter: any, initStartChat: any, state: ILiveChatWidgetContext) => Promise<boolean>;
9
14
  declare const getChatReconnectContext: (facadeChatSDK: FacadeChatSDK, chatConfig: ChatConfig, props: any, isAuthenticatedChat: boolean, dispatch: Dispatch<ILiveChatWidgetAction>) => Promise<import("@microsoft/omnichannel-chat-sdk/lib/core/ChatReconnectContext").default | undefined>;
10
15
  declare const isReconnectEnabled: (chatConfig?: ChatConfig) => boolean;
11
16
  declare const isPersistentEnabled: (chatConfig?: ChatConfig) => boolean;
12
- export { handleChatReconnect, isReconnectEnabled, isPersistentEnabled, getChatReconnectContext };
17
+ export { handleChatReconnect, isReconnectTriggered, isReconnectEnabled, isPersistentEnabled, getChatReconnectContext };
@@ -6,6 +6,10 @@ import { ILiveChatWidgetProps } from "../interfaces/ILiveChatWidgetProps";
6
6
  import StartChatOptionalParams from "@microsoft/omnichannel-chat-sdk/lib/core/StartChatOptionalParams";
7
7
  declare const prepareStartChat: (props: ILiveChatWidgetProps, facadeChatSDK: FacadeChatSDK, state: ILiveChatWidgetContext, dispatch: Dispatch<ILiveChatWidgetAction>, setAdapter: any) => Promise<void>;
8
8
  declare const setPreChatAndInitiateChat: (facadeChatSDK: FacadeChatSDK, dispatch: Dispatch<ILiveChatWidgetAction>, setAdapter: any, isProactiveChat?: boolean | false, proactiveChatEnablePrechatState?: boolean | false, state?: ILiveChatWidgetContext, props?: ILiveChatWidgetProps) => Promise<void>;
9
- declare const initStartChat: (facadeChatSDK: FacadeChatSDK, dispatch: Dispatch<ILiveChatWidgetAction>, setAdapter: any, state: ILiveChatWidgetContext | undefined, props?: ILiveChatWidgetProps, params?: StartChatOptionalParams, persistedState?: any) => Promise<void>;
9
+ /**
10
+ * Resolves to `true` when the chat reached Active, and `false` when it failed or the call was dropped.
11
+ * It resolves before React renders Active, so callers must use this value rather than reading the conversation state.
12
+ */
13
+ declare const initStartChat: (facadeChatSDK: FacadeChatSDK, dispatch: Dispatch<ILiveChatWidgetAction>, setAdapter: any, state: ILiveChatWidgetContext | undefined, props?: ILiveChatWidgetProps, params?: StartChatOptionalParams, persistedState?: any) => Promise<boolean>;
10
14
  declare const checkIfConversationStillValid: (facadeChatSDK: FacadeChatSDK, dispatch: Dispatch<ILiveChatWidgetAction>, state: ILiveChatWidgetContext) => Promise<boolean>;
11
15
  export { prepareStartChat, initStartChat, setPreChatAndInitiateChat, checkIfConversationStillValid };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@microsoft/omnichannel-chat-widget",
3
- "version": "2.0.1-main.2b5f4df",
3
+ "version": "2.0.1-main.38c3c85",
4
4
  "description": "Microsoft Omnichannel Chat Widget",
5
5
  "main": "lib/cjs/index.js",
6
6
  "types": "lib/types/index.d.ts",