mirror of
https://github.com/Qortal/Qortal-Hub.git
synced 2025-04-20 10:05:56 +00:00
fix bug
This commit is contained in:
parent
b665d229f3
commit
abb9eea4c4
@ -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"
|
||||
|
@ -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';
|
||||
|
@ -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;
|
||||
// Attach to window for global access
|
||||
window.sendMessage = sendMessageBackground;
|
||||
|
Loading…
x
Reference in New Issue
Block a user