Skip to content

Commit

Permalink
fix: remove configLoaded in favour of using cookie name / clean up svgs
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkifer committed Jun 4, 2024
1 parent ba099ea commit febad26
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
4 changes: 0 additions & 4 deletions src/images/link.svg

This file was deleted.

4 changes: 0 additions & 4 deletions src/images/open.svg

This file was deleted.

28 changes: 19 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SplitTest[]> = new BehavioralSubject(tests);

Expand Down Expand Up @@ -39,7 +38,6 @@ export function config(userConfig: Partial<Config> = {}) {
_config.userSessionDaysToLive,
);
}
configLoaded = true;
}

/**
Expand Down Expand Up @@ -128,20 +126,32 @@ export function reset(): void {
_config.onVariationChange();
}

const waitUntil = (condition: () => any, checkInterval = 100) => {
return new Promise<void>((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<void>((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),
Expand Down

0 comments on commit febad26

Please sign in to comment.