Skip to content

Commit

Permalink
MWPW-146491 Fast follow to change timeout analytic and use Date inste…
Browse files Browse the repository at this point in the history
…ad of performance (adobecom#2163)

* Do not use Error when timing out

* revert and lint fix

---------

Co-authored-by: ChrisChrisChris <[email protected]>
  • Loading branch information
vgoodric and chrischrischris authored Apr 18, 2024
1 parent c5c6389 commit a3e6ddb
Showing 1 changed file with 21 additions and 38 deletions.
59 changes: 21 additions & 38 deletions libs/martech/martech.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ const setDeep = (obj, path, value) => {
currentObj[pathArr[pathArr.length - 1]] = value;
};

const waitForEventOrTimeout = (eventName, timeout, timeoutVal) => new Promise((resolve, reject) => {
// eslint-disable-next-line max-len
const waitForEventOrTimeout = (eventName, timeout, returnValIfTimeout) => new Promise((resolve) => {
const listener = (event) => {
// eslint-disable-next-line no-use-before-define
clearTimeout(timer);
resolve(event.detail);
};

const timer = setTimeout(() => {
if (timeoutVal !== undefined) {
resolve(timeoutVal);
window.removeEventListener(eventName, listener);
if (returnValIfTimeout !== undefined) {
resolve(returnValIfTimeout);
} else {
reject(new Error(`Timeout waiting for ${eventName} after ${timeout}ms`));
resolve({ timeout: true });
}
}, timeout);

window.addEventListener(eventName, (event) => {
clearTimeout(timer);
resolve(event.detail);
}, { once: true });
window.addEventListener(eventName, listener, { once: true });
});

const getExpFromParam = (expParam) => {
Expand Down Expand Up @@ -76,7 +81,7 @@ function roundToQuarter(num) {
}

function calculateResponseTime(responseStart) {
const responseTime = performance.now() - responseStart;
const responseTime = Date.now() - responseStart;
return roundToQuarter(responseTime);
}

Expand Down Expand Up @@ -112,41 +117,19 @@ const getTargetPersonalization = async () => {
|| parseInt(getMetadata('target-timeout'), 10)
|| TARGET_TIMEOUT_MS;

let response;

const responseStart = performance.now();
const responseStart = Date.now();
window.addEventListener(ALLOY_SEND_EVENT, () => {
const responseTime = calculateResponseTime(responseStart);
window.lana.log('target response time', responseTime);
}, { once: true });

try {
response = await waitForEventOrTimeout(ALLOY_SEND_EVENT, timeout);
sendTargetResponseAnalytics(false, responseStart, timeout);
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
if (e.message.startsWith('Timeout waiting for alloy_sendEvent after')) {
const timer = setTimeout(() => {
// eslint-disable-next-line no-use-before-define
window.removeEventListener(ALLOY_SEND_EVENT, sendAnalytics);
sendTargetResponseAnalytics(true, responseStart, timeout);
}, 5100 - timeout);

// eslint-disable-next-line no-inner-declarations
function sendAnalytics() {
clearTimeout(timer);
sendTargetResponseAnalytics(true, responseStart, timeout);
}

window.addEventListener(ALLOY_SEND_EVENT, sendAnalytics, { once: true });
} else {
sendTargetResponseAnalytics(false, responseStart, timeout, e.message);
}
}

let manifests = [];
if (response) {
const response = await waitForEventOrTimeout(ALLOY_SEND_EVENT, timeout);
if (response.timeout) {
waitForEventOrTimeout(ALLOY_SEND_EVENT, 5100 - timeout)
.then(() => sendTargetResponseAnalytics(true, responseStart, timeout));
} else {
sendTargetResponseAnalytics(false, responseStart, timeout);
manifests = handleAlloyResponse(response.result);
}

Expand Down

0 comments on commit a3e6ddb

Please sign in to comment.