Skip to content

Commit

Permalink
Merge pull request #906 from ranfisfrancisco/master
Browse files Browse the repository at this point in the history
Add openPopupWithLock and use to open login popup
  • Loading branch information
ranfisfrancisco authored Aug 5, 2024
2 parents e1689ac + 32a4376 commit 05a8b1e
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG.md

## [2.14.5] - 2024-08-02
Added:
- Made change to prevent more than one login popup from appearing when an embedded CCP is opened in two or more tabs.

## [2.14.1] - 2024-03-29
Added:
- `initCCP` param to set custom ringtone for chat.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amazon-connect-streams",
"version": "2.14.4",
"version": "2.14.5",
"description": "Amazon Connect Streams Library",
"engines": {
"node": ">=12.0.0"
Expand Down
2 changes: 1 addition & 1 deletion release/connect-streams-dr-min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion release/connect-streams-dr.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion release/connect-streams-min.js

Large diffs are not rendered by default.

45 changes: 43 additions & 2 deletions release/connect-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -26896,7 +26896,7 @@ AWS.apiLoader.services['connect']['2017-02-15'] = require('../apis/connect-2017-

connect.core = {};
connect.core.initialized = false;
connect.version = "2.14.4";
connect.version = "2.14.5";
connect.outerContextStreamsVersion = null;
connect.DEFAULT_BATCH_SIZE = 500;

Expand Down Expand Up @@ -26940,6 +26940,8 @@ AWS.apiLoader.services['connect']['2017-02-15'] = require('../apis/connect-2017-
const SNAPSHOT_TOTAL_PROCESSING_TIME = 'SnapshotTotalProcessingTime';
const SNAPSHOT_COMPARISON_STEP_TIME = 'SnapshotComparisonStepTime';

var CHECK_LOGIN_POPUP_INTERVAL_MS = 1000;

const APP = {
GUIDES: 'customviews',
};
Expand Down Expand Up @@ -28334,7 +28336,7 @@ AWS.apiLoader.services['connect']['2017-02-15'] = require('../apis/connect-2017-
try {
var loginUrl = getLoginUrl(params);
connect.getLog().warn("ACK_TIMEOUT occurred, attempting to pop the login page if not already open.").sendInternalLogToServer();
connect.core.loginWindow = connect.core.getPopupManager().open(loginUrl, connect.MasterTopics.LOGIN_POPUP, params.loginOptions);
connect.core._openPopupWithLock(loginUrl, params.loginOptions)
} catch (e) {
connect.getLog().error("ACK_TIMEOUT occurred but we are unable to open the login popup.").withException(e).sendInternalLogToServer();
}
Expand Down Expand Up @@ -28405,6 +28407,45 @@ AWS.apiLoader.services['connect']['2017-02-15'] = require('../apis/connect-2017-
};
};

connect.core._openPopupWithLock = function (loginUrl, loginOptions) {
const lockName = "connect-login-popup-lock" + loginUrl;

try {
navigator.locks.request(lockName, {mode: 'exclusive', ifAvailable: true }, async lock => {
if (!lock) {
connect.getLog().info("Popup already opened by another tab.").sendInternalLogToServer();
return;
}
connect.getLog().info("Opening popup window with weblock.").sendInternalLogToServer();

connect.core.loginWindow = connect.core.getPopupManager().open(loginUrl, connect.MasterTopics.LOGIN_POPUP, loginOptions);

connect.core._shouldHoldPopupLock = true;

const checkPopupInterval = setInterval(function() {
if (!connect.core.loginWindow || connect.core.loginWindow?.closed) {
clearInterval(checkPopupInterval);
connect.core._shouldHoldPopupLock = false;
connect.getLog().info("Cleared check popup interval.").sendInternalLogToServer();
}
}, CHECK_LOGIN_POPUP_INTERVAL_MS);

// hold the lock until the popup is closed
while (connect.core._shouldHoldPopupLock) {
await new Promise(resolve => setTimeout(resolve, 1000));
}

connect.getLog().info("Releasing weblock for opening login popup.").sendInternalLogToServer();
})
} catch (e) {
connect.getLog().error("Failed to use weblock to open popup. Your browser may be out of date.").withException(e).sendInternalLogToServer();

if (!connect.core.loginWindow){
connect.core.loginWindow = connect.core.getPopupManager().open(loginUrl, connect.MasterTopics.LOGIN_POPUP, loginOptions);
}
}
};

connect.core.onIframeRetriesExhausted = function(f) {
connect.core.getEventBus().subscribe(connect.EventType.IFRAME_RETRIES_EXHAUSTED, f);
}
Expand Down
43 changes: 42 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
const SNAPSHOT_TOTAL_PROCESSING_TIME = 'SnapshotTotalProcessingTime';
const SNAPSHOT_COMPARISON_STEP_TIME = 'SnapshotComparisonStepTime';

var CHECK_LOGIN_POPUP_INTERVAL_MS = 1000;

const APP = {
GUIDES: 'customviews',
};
Expand Down Expand Up @@ -1449,7 +1451,7 @@
try {
var loginUrl = getLoginUrl(params);
connect.getLog().warn("ACK_TIMEOUT occurred, attempting to pop the login page if not already open.").sendInternalLogToServer();
connect.core.loginWindow = connect.core.getPopupManager().open(loginUrl, connect.MasterTopics.LOGIN_POPUP, params.loginOptions);
connect.core._openPopupWithLock(loginUrl, params.loginOptions)
} catch (e) {
connect.getLog().error("ACK_TIMEOUT occurred but we are unable to open the login popup.").withException(e).sendInternalLogToServer();
}
Expand Down Expand Up @@ -1520,6 +1522,45 @@
};
};

connect.core._openPopupWithLock = function (loginUrl, loginOptions) {
const lockName = "connect-login-popup-lock" + loginUrl;

try {
navigator.locks.request(lockName, {mode: 'exclusive', ifAvailable: true }, async lock => {
if (!lock) {
connect.getLog().info("Popup already opened by another tab.").sendInternalLogToServer();
return;
}
connect.getLog().info("Opening popup window with weblock.").sendInternalLogToServer();

connect.core.loginWindow = connect.core.getPopupManager().open(loginUrl, connect.MasterTopics.LOGIN_POPUP, loginOptions);

connect.core._shouldHoldPopupLock = true;

const checkPopupInterval = setInterval(function() {
if (!connect.core.loginWindow || connect.core.loginWindow?.closed) {
clearInterval(checkPopupInterval);
connect.core._shouldHoldPopupLock = false;
connect.getLog().info("Cleared check popup interval.").sendInternalLogToServer();
}
}, CHECK_LOGIN_POPUP_INTERVAL_MS);

// hold the lock until the popup is closed
while (connect.core._shouldHoldPopupLock) {
await new Promise(resolve => setTimeout(resolve, 1000));
}

connect.getLog().info("Releasing weblock for opening login popup.").sendInternalLogToServer();
})
} catch (e) {
connect.getLog().error("Failed to use weblock to open popup. Your browser may be out of date.").withException(e).sendInternalLogToServer();

if (!connect.core.loginWindow){
connect.core.loginWindow = connect.core.getPopupManager().open(loginUrl, connect.MasterTopics.LOGIN_POPUP, loginOptions);
}
}
};

connect.core.onIframeRetriesExhausted = function(f) {
connect.core.getEventBus().subscribe(connect.EventType.IFRAME_RETRIES_EXHAUSTED, f);
}
Expand Down

0 comments on commit 05a8b1e

Please sign in to comment.