Skip to content

Commit a4b1948

Browse files
authored
fix: improve chatbot iframe handshake (#1373)
## Description - Make the handshake with the chat easier ## What type of PR is this? (check all applicable) - [ ] πŸ’‘ (feat) - A new feature (non-breaking change which adds functionality) - [ ] πŸ”„ (refactor) - Code Refactoring - A code change that neither fixes a bug nor adds a feature - [x] 🐞 (fix) - Bug Fix (non-breaking change which fixes an issue) - [ ] 🏎 (perf) - Optimization - [ ] πŸ“„ (docs) - Documentation - Documentation only changes - [ ] πŸ“„ (test) - Tests - Adding missing tests or correcting existing tests - [ ] βš™οΈ (ci) - Continuous Integrations - Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs) - [ ] β˜‘οΈ (chore) - Chores - Other changes that don't modify src or test files - [ ] ↩️ (revert) - Reverts - Reverts a previous commit(s). <!-- For a timely review/response, please avoid force-pushing additional commits if your PR already received reviews or comments. Before submitting a Pull Request, please ensure you've done the following: - πŸ‘·β€β™€οΈ Create small PRs. In most cases this will be possible. - βœ… Provide tests for your changes. - πŸ“ Use descriptive commit messages (as described below). - πŸ“— Update any related documentation and include any relevant screenshots. Commit Message Structure (all lower-case): <type>(optional ticket number): <description> [optional body] -->
1 parent 087a8a7 commit a4b1948

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

β€Žsrc/services/iframeComm.service.tsβ€Ž

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ class IframeCommService {
121121
}
122122

123123
this.iframeRef = iframe;
124+
125+
if (this.messageQueue.length > 0) {
126+
LoggerService.debug(
127+
namespaces.iframeCommService,
128+
`Iframe ref set - processing ${this.messageQueue.length} queued messages`
129+
);
130+
void this.processMessageQueue();
131+
}
124132
}
125133

126134
public destroy(): void {
@@ -650,20 +658,6 @@ class IframeCommService {
650658

651659
private async handleIncomingMessages(event: MessageEvent): Promise<void> {
652660
try {
653-
if (!this.iframeRef || !document.contains(this.iframeRef)) {
654-
return;
655-
}
656-
657-
if (event.origin === (this.expectedOrigin || aiChatbotOrigin) && !this.isConnected) {
658-
this.isConnected = true;
659-
if (this.connectionResolve) {
660-
this.connectionResolve();
661-
this.connectionResolve = null;
662-
}
663-
664-
this.sendDatadogContext();
665-
}
666-
667661
if (!this.isValidOrigin(event.origin)) {
668662
LoggerService.debug(
669663
namespaces.iframeCommService,
@@ -688,6 +682,15 @@ class IframeCommService {
688682
return;
689683
}
690684

685+
if (event.origin === (this.expectedOrigin || aiChatbotOrigin) && !this.isConnected) {
686+
this.isConnected = true;
687+
if (this.connectionResolve) {
688+
this.connectionResolve();
689+
this.connectionResolve = null;
690+
}
691+
this.sendDatadogContext();
692+
}
693+
691694
const message = event.data as AkbotMessage;
692695

693696
if (!Object.values(MessageTypes).includes(message?.type)) {
@@ -717,6 +720,19 @@ class IframeCommService {
717720
await this.processMessageQueue();
718721
}
719722

723+
if (!this.iframeRef || !document.contains(this.iframeRef)) {
724+
if (message.type !== MessageTypes.HANDSHAKE && message.type !== MessageTypes.HANDSHAKE_ACK) {
725+
LoggerService.debug(
726+
namespaces.iframeCommService,
727+
`Received ${message.type} but iframe ref not ready - adding to queue`
728+
);
729+
if (this.messageQueue.length < this.maxQueueSize) {
730+
this.messageQueue.push(message);
731+
}
732+
return;
733+
}
734+
}
735+
720736
switch (message.type) {
721737
case MessageTypes.HANDSHAKE: {
722738
const handshakeAckMessage: HandshakeAckMessage = {
@@ -726,7 +742,14 @@ class IframeCommService {
726742
version: CONFIG.APP_VERSION,
727743
},
728744
};
729-
this.sendMessage(handshakeAckMessage);
745+
if (this.iframeRef && document.contains(this.iframeRef)) {
746+
this.sendMessage(handshakeAckMessage);
747+
} else {
748+
LoggerService.debug(
749+
namespaces.iframeCommService,
750+
"Received HANDSHAKE but iframe ref not ready yet - waiting for iframe to be set"
751+
);
752+
}
730753
break;
731754
}
732755
case MessageTypes.HANDSHAKE_ACK:

0 commit comments

Comments
Β (0)