diff --git a/src/popup.tsx b/src/popup.tsx index e5516e01a..8f6600a79 100644 --- a/src/popup.tsx +++ b/src/popup.tsx @@ -10,6 +10,7 @@ import { handleSyncLabelsAlarm } from "~api/background/handlers/alarms/sync-labe import { ErrorBoundary } from "~utils/error/ErrorBoundary/errorBoundary"; import { FallbackView } from "~components/page/common/Fallback/fallback.view"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { keepConnectionAlive } from "~utils/connection/connection.utils"; const queryClient = new QueryClient({ defaultOptions: { @@ -25,6 +26,7 @@ const queryClient = new QueryClient({ export function WanderBrowserExtensionApp() { useEffect(() => { handleSyncLabelsAlarm(); + keepConnectionAlive(); }, []); return ( diff --git a/src/utils/connection/connection.constants.ts b/src/utils/connection/connection.constants.ts new file mode 100644 index 000000000..f278db0e7 --- /dev/null +++ b/src/utils/connection/connection.constants.ts @@ -0,0 +1,47 @@ +// TODO: replace once gateway settings are implemented +const gateways: Record[] = [ + { + host: "arweave.net", + protocol: "https" + }, + { + host: "ar-io.net", + protocol: "https" + }, + { + host: "arweave.dev", + protocol: "https" + }, + { + host: "g8way.io", + protocol: "https" + }, + { + host: "arweave.ar", + protocol: "https" + }, + { + host: "ar-io.dev", + protocol: "https" + }, + { + host: "permagate.io", + protocol: "https" + }, + { + host: "defi.ao", + protocol: "https" + }, + { + host: "aoweave.tech", + protocol: "https" + } +]; + +const apps = ["dexi"]; + +export const keepConnectionUrls = apps + .map((app) => + gateways.map((gateway) => `${gateway.protocol}://${app}.${gateway.host}`) + ) + .flat(); diff --git a/src/utils/connection/connection.utils.ts b/src/utils/connection/connection.utils.ts new file mode 100644 index 000000000..cc517e6b9 --- /dev/null +++ b/src/utils/connection/connection.utils.ts @@ -0,0 +1,30 @@ +import { Mutex } from "~utils/mutex"; +import browser from "webextension-polyfill"; +import { keepConnectionUrls } from "./connection.constants"; +import { getActiveTab } from "~applications"; + +let keepAliveInterval: number | null = null; +const mutex = new Mutex(); + +/** + * Function to send periodic keep-alive messages for specific urls + */ +export async function keepConnectionAlive() { + const unlock = await mutex.lock(); + + const activeTab = await getActiveTab(); + const foundUrl = keepConnectionUrls.find((keepUrl: string) => + activeTab.url.includes(keepUrl) + ); + + try { + if (!keepAliveInterval && foundUrl) { + keepAliveInterval = setInterval( + () => browser.alarms.create("keep-alive", { when: Date.now() + 1 }), + 20000 + ); + } + } finally { + unlock(); + } +}