import { Events, CommonServicePayload } from "../types"; import { ERRORS } from "../utils/constant"; import { authenticateUser } from "../services/auth"; import { trackAcquisition } from "../services/member"; import { fetchReleases } from "../services/releases"; import { fetchTutorials } from "../services/tutorials"; import { fetchFeatures, recordFeedback } from "../services/features"; import { recordMetric, updateMetric } from "../services/metrics"; export const services: Record< string, ({ host, headers, payload }: CommonServicePayload) => Promise > = { [Events.INITIATE_AUTHENTICATION]: authenticateUser, [Events.RECORD_MEMBER_ACQUISITION]: trackAcquisition, [Events.FETCH_RELEASES]: fetchReleases, [Events.FETCH_TUTORIALS]: fetchTutorials, [Events.FETCH_FEATURES]: fetchFeatures, [Events.RECORD_FEEDBACK]: recordFeedback, [Events.RECORD_METRIC]: recordMetric, [Events.UPDATE_METRIC]: updateMetric, }; self.addEventListener("message", async event => { const { type, payload, headers, host } = event.data; if (!navigator.onLine) { console.warn({ type: ERRORS.NETWORK_ERROR }); // self.postMessage({ type: ERRORS.NETWORK_ERROR }); return; } const handler = services[type]; if (handler) { try { await handler({ host, headers, payload }); } catch (error) { console.warn(`Worker Error (${type}):`, error); // self.postMessage({ type: ERRORS.INTERNAL_SERVER_ERROR, error }); } } });