diff --git a/src/main/index.ts b/src/main/index.ts index 26dbc43f0..16aed90a6 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -23,6 +23,10 @@ if (process.defaultApp) { app.dock.setIcon(path.join(__dirname, "../../resources/icon.png")) +let mainWindow: BrowserWindow + +export const getMainWindow = () => mainWindow + // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. @@ -37,7 +41,6 @@ app.whenReady().then(() => { optimizer.watchWindowShortcuts(window) }) - let mainWindow: BrowserWindow mainWindow = createWindow() app.on("activate", () => { diff --git a/src/main/renderer-handlers.ts b/src/main/renderer-handlers.ts new file mode 100644 index 000000000..78216cfaf --- /dev/null +++ b/src/main/renderer-handlers.ts @@ -0,0 +1,3 @@ +export type RendererHandlers = { + invalidateQuery: (key: (string | number | undefined)[]) => void +} diff --git a/src/main/tipc.ts b/src/main/tipc.ts index c372581b4..368a62791 100644 --- a/src/main/tipc.ts +++ b/src/main/tipc.ts @@ -1,6 +1,8 @@ -import { tipc } from "@egoist/tipc/main" +import { tipc, getRendererHandlers } from "@egoist/tipc/main" import type { MessageBoxOptions } from "electron" import { dialog, Menu, ShareMenu } from "electron" +import { RendererHandlers } from "./renderer-handlers" +import { getMainWindow } from "." const t = tipc.create() @@ -93,6 +95,14 @@ export const router = { return null } }), + + invalidateQuery: t.procedure + .input<(string | number | undefined)[]>() + .action(async ({ input }) => { + const mainWindow = getMainWindow() + const handlers = getRendererHandlers(mainWindow.webContents) + handlers.invalidateQuery.send(input) + }), } export type Router = typeof router diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index 4e3adc6a4..8c0dd1e1d 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -1,9 +1,25 @@ import { Outlet } from "react-router-dom" import { useDark } from "./hooks/useDark" +import { handlers } from "./tipc" +import { useEffect } from "react" + +import { queryClient } from "@renderer/lib/query-client" + function App() { useDark() + + useEffect(() => { + const unlisten = handlers?.invalidateQuery.listen((queryKey) => { + queryClient.invalidateQueries({ + queryKey, + }) + }) + + return unlisten + }, []) + return ( <>
({ + on: (channel, callback) => { + const remover = window.electron!.ipcRenderer.on(channel, callback) + return () => { + remover() + } + }, + + send: window.electron!.ipcRenderer.send, +}) : null