From febad263268bcfd851d4a6d579a1e09a60ecbe7c Mon Sep 17 00:00:00 2001 From: kamilkifer <47425451+kamilkifer@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:45:30 +0100 Subject: [PATCH] fix: remove configLoaded in favour of using cookie name / clean up svgs --- src/images/link.svg | 4 ---- src/images/open.svg | 4 ---- src/main.ts | 28 +++++++++++++++++++--------- 3 files changed, 19 insertions(+), 17 deletions(-) delete mode 100644 src/images/link.svg delete mode 100644 src/images/open.svg diff --git a/src/images/link.svg b/src/images/link.svg deleted file mode 100644 index 08d13d8..0000000 --- a/src/images/link.svg +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/src/images/open.svg b/src/images/open.svg deleted file mode 100644 index 02bbef7..0000000 --- a/src/images/open.svg +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/src/main.ts b/src/main.ts index 50f5dea..617c57d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,7 +8,6 @@ import _getUserAgentInfo from './userAgentInfo'; import userSession, { UserSession } from './userSession'; const userAgentInfo = _getUserAgentInfo(); -let configLoaded = false; export const tests: SplitTest[] = []; export const testsObservable: BehavioralSubject = new BehavioralSubject(tests); @@ -39,7 +38,6 @@ export function config(userConfig: Partial = {}) { _config.userSessionDaysToLive, ); } - configLoaded = true; } /** @@ -128,20 +126,32 @@ export function reset(): void { _config.onVariationChange(); } -const waitUntil = (condition: () => any, checkInterval = 100) => { - return new Promise((resolve) => { +//auto resolves the promise after 2 seconds if the condition is not met - stops a hanging promise +const waitUntil = (condition: () => any, checkInterval = 100, timeout = 2000) => { + return new Promise((resolve, reject) => { + const startTime = Date.now(); const interval = setInterval(() => { - if (!condition()) { - return; + if (condition()) { + clearInterval(interval); + clearTimeout(safetyTimeout); + resolve(); + } else if (Date.now() - startTime >= timeout) { + clearInterval(interval); + clearTimeout(safetyTimeout); + reject(new Error("Timeout")); } - clearInterval(interval); - resolve(); }, checkInterval); + + const safetyTimeout = setTimeout(() => { + clearInterval(interval); + reject(new Error("Timeout")); + }, timeout); }); }; export async function shouldShowUI() { - await waitUntil(() => configLoaded); + // cookie name is provided from split test package user config. Ensures it's loaded before calling the shouldShowUI function + await waitUntil(() => _config.cookieName === "trustpilotABTest").catch(()=>{}); const promises = [ _config.globalCondition(userAgentInfo), _config.uiCondition(userAgentInfo),