diff --git a/src/App.tsx b/src/App.tsx index 4f3802b..6880ca4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -105,7 +105,7 @@ import { useRecoilState, useResetRecoilState, useSetRecoilState } from "recoil"; import { canSaveSettingToQdnAtom, fullScreenAtom, hasSettingsChangedAtom, oldPinnedAppsAtom, settingsLocalLastUpdatedAtom, settingsQDNLastUpdatedAtom, sortablePinnedAppsAtom } from "./atoms/global"; import { useAppFullScreen } from "./useAppFullscreen"; import { NotAuthenticated } from "./ExtStates/NotAuthenticated"; -import { sendMessageBackground, sendMessageState } from "./messaging/messagesToBackground"; + type extStates = | "not-authenticated" diff --git a/src/main.tsx b/src/main.tsx index 2bdf4b3..c19eaec 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -2,7 +2,7 @@ import React from 'react' import ReactDOM from 'react-dom/client' import App from './App.tsx' import './index.css' - +import "./messaging/messagesToBackground"; import { ThemeProvider, createTheme } from '@mui/material/styles'; import { CssBaseline } from '@mui/material'; import { MessageQueueProvider } from './MessageQueueContext.tsx'; diff --git a/src/messaging/messagesToBackground.tsx b/src/messaging/messagesToBackground.tsx index a374992..6d1a4d2 100644 --- a/src/messaging/messagesToBackground.tsx +++ b/src/messaging/messagesToBackground.tsx @@ -28,27 +28,30 @@ window.addEventListener("message", (event) => { } }); -// Define the sendMessage function export const sendMessageBackground = (action, data = {}, timeout = 60000) => { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const requestId = generateRequestId(); // Unique ID for each request - callbackMap.set(requestId, { resolve }); // Store resolve callback only + callbackMap.set(requestId, { resolve, reject }); // Store both resolve and reject callbacks // Send the message with `backgroundMessage` type window.postMessage({ type: "backgroundMessage", action, requestId, payload: data }, "*"); - // Set up a timeout to automatically resolve with an error if no response is received + // Set up a timeout to automatically reject if no response is received const timeoutId = setTimeout(() => { // Remove the callback to prevent memory leaks callbackMap.delete(requestId); - resolve({ error: "timeout", message: `Request timed out after ${timeout} ms` }); + reject({ error: "timeout", message: `Request timed out after ${timeout} ms` }); }, timeout); - // Adjust resolve to clear the timeout when a response arrives + // Adjust resolve/reject to clear the timeout when a response arrives callbackMap.set(requestId, { resolve: (response) => { clearTimeout(timeoutId); // Clear the timeout if the response is received in time resolve(response); + }, + reject: (error) => { + clearTimeout(timeoutId); // Clear the timeout if an error occurs + reject(error); } }); }).then((response) => { @@ -61,6 +64,5 @@ export const sendMessageBackground = (action, data = {}, timeout = 60000) => { }); }; - - -window.sendMessage = sendMessageBackground; \ No newline at end of file +// Attach to window for global access +window.sendMessage = sendMessageBackground;