Skip to content

Commit

Permalink
Fix race conditions with events setup
Browse files Browse the repository at this point in the history
  • Loading branch information
max-nav committed Jan 10, 2024
1 parent 1629ca1 commit a7628c4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
9 changes: 2 additions & 7 deletions packages/client/src/analytics/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Auth } from '../api';
import { analyticsReadyEvent } from '../events';
import { analyticsReady } from '../events';
import { initAmplitude, logAmplitudeEvent, logPageView } from './amplitude';
import { AnalyticsEventArgs } from './constants';
import {
Expand All @@ -10,11 +9,7 @@ import {
export const initAnalytics = () => {
initAmplitude();
initTaskAnalytics();
window.addEventListener(analyticsReadyEvent, (e) => {
const response = (e as CustomEvent).detail as Auth;
window.logPageView(window.__DECORATOR_DATA__.params, response);
window.startTaskAnalyticsSurvey(window.__DECORATOR_DATA__);
});
dispatchEvent(analyticsReady);
};

// Connects to partytown forwarding
Expand Down
11 changes: 7 additions & 4 deletions packages/client/src/events.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Auth } from "./api";
import { Auth } from "./api"

export const analyticsReadyEvent = "decorator-analytics-ready";
export const analyticsReady = new CustomEvent('analytics-ready-event', {
bubbles: true,
})

export type AnalyticsLoaded = CustomEvent<Auth>

export const analyticsReady = (response: Auth) => new CustomEvent(analyticsReadyEvent, {
export const analyticsLoaded = new CustomEvent<Auth>('analytics-loaded-event', {
bubbles: true,
detail: response
})
26 changes: 21 additions & 5 deletions packages/client/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { formatParams } from 'decorator-shared/json';
import { LoginLevel, type Context, type Params } from 'decorator-shared/params'; import Cookies from 'js-cookie'; import 'vite/modulepreload-polyfill';
import { LoginLevel, type Context, type Params } from 'decorator-shared/params';
import Cookies from 'js-cookie';
import 'vite/modulepreload-polyfill';
import * as api from './api';
import { logoutWarningController } from './controllers/logout-warning';
import { onLoadListeners } from './listeners';
Expand All @@ -24,7 +26,7 @@ import './views/search-input';
import './views/search-menu';
import { Auth } from './api';
import { addFaroMetaData } from './faro';
import { analyticsReady } from './events';
import { analyticsLoaded, analyticsReady } from './events';

import.meta.glob('./styles/*.css', { eager: true });

Expand Down Expand Up @@ -133,27 +135,41 @@ async function populateLoggedInMenu(authObject: Auth) {

//
// await populateLoggedInMenu(response);

const init = async () => {
const response = await api.checkAuth();

dispatchEvent(
new CustomEvent(analyticsLoaded.type, {
bubbles: true,
detail: { response },
})
);

if (!response.authenticated) {
return;
}

await populateLoggedInMenu(response);

window.dispatchEvent(analyticsReady(response));
await populateLoggedInMenu(response);
};

init();

window.addEventListener(analyticsReady.type, () => {
window.addEventListener(analyticsLoaded.type, (e) => {
const response = (e as CustomEvent<Auth>).detail;
window.logPageView(window.__DECORATOR_DATA__.params, response);
- window.startTaskAnalyticsSurvey(window.__DECORATOR_DATA__);
});
});

function handleLogin() {
const loginLevel = window.__DECORATOR_DATA__.params.level || 'Level4';
document.getElementById('login-button')?.addEventListener('click', async () => {
window.location.href = `${window.__DECORATOR_DATA__.env.LOGIN_URL}?redirect=${window.location.href}&level=${loginLevel}`;
});
}
}

handleLogin();

Expand Down

0 comments on commit a7628c4

Please sign in to comment.