From a0d206d8d32b5ec25f188eaa174030beb50dcd32 Mon Sep 17 00:00:00 2001 From: Innei Date: Sat, 27 Jul 2024 12:39:30 +0800 Subject: [PATCH] fix: invalidate query Signed-off-by: Innei --- src/main/window.ts | 2 + src/renderer/src/initialize/index.ts | 7 +++ .../providers/invalidate-query-provider.tsx | 59 ++++++++++++++++++- src/shared/src/bridge.ts | 2 + 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/main/window.ts b/src/main/window.ts index 0cd00ea6c..d5319e859 100644 --- a/src/main/window.ts +++ b/src/main/window.ts @@ -197,6 +197,8 @@ export const createMainWindow = () => { window.on("show", () => { cancelPollingUpdateUnreadCount() + + callGlobalContextMethod(window, "invalidateQueries") }) window.on("hide", async () => { diff --git a/src/renderer/src/initialize/index.ts b/src/renderer/src/initialize/index.ts index b697aa163..90f7655bc 100644 --- a/src/renderer/src/initialize/index.ts +++ b/src/renderer/src/initialize/index.ts @@ -3,6 +3,7 @@ import { authConfigManager } from "@hono/auth-js/react" import { repository } from "@pkg" import { getUISettings } from "@renderer/atoms/settings/ui" import { browserDB } from "@renderer/database" +import { InvalidateQueryEvent } from "@renderer/providers/invalidate-query-provider" import { CleanerService } from "@renderer/services/cleaner" import { registerGlobalContext } from "@shared/bridge" import dayjs from "dayjs" @@ -61,6 +62,12 @@ export const initializeApp = async () => { showSetting: () => window.router.showSettings(), getGeneralSettings, getUISettings, + /** + * Electron app only + */ + invalidateQueries() { + document.dispatchEvent(new InvalidateQueryEvent()) + }, toast, }) diff --git a/src/renderer/src/providers/invalidate-query-provider.tsx b/src/renderer/src/providers/invalidate-query-provider.tsx index 37467b52d..9ae63d94a 100644 --- a/src/renderer/src/providers/invalidate-query-provider.tsx +++ b/src/renderer/src/providers/invalidate-query-provider.tsx @@ -3,12 +3,56 @@ import { appLog } from "@renderer/lib/log" import { useQueryClient } from "@tanstack/react-query" import { useEffect, useRef } from "react" +const slateTime = 600000 // 10min + +const DISPATCH_KEY = "invalidate-query-key" + +export class InvalidateQueryEvent extends Event { + static type = DISPATCH_KEY + constructor() { + super(DISPATCH_KEY) + } +} + /** - * InvalidateQuery when electron app window switch to visible, macOS dock icon click + * Add a event listener to invalidate all queries */ -export const InvalidateQueryProvider = () => { + +const InvalidateQueryProviderElectron = () => { const queryClient = useQueryClient() + const currentTimeRef = useRef(Date.now()) + + useEffect(() => { + const handler = () => { + const now = Date.now() + if (now - currentTimeRef.current < slateTime) { + return + } + + currentTimeRef.current = now + + appLog("Window switch to visible, invalidate all queries") + queryClient.invalidateQueries() + } + + document.addEventListener(InvalidateQueryEvent.type, handler) + + return () => { + document.removeEventListener(InvalidateQueryEvent.type, handler) + } + }, [queryClient]) + return null +} + +/** + * Invalidate all queries when the window is visible + */ + +const InvalidateQueryProviderWebApp = () => { + const queryClient = useQueryClient() + + const currentTimeRef = useRef(Date.now()) const currentVisibilityRef = useRef(!document.hidden) const pageVisibility = usePageVisibility() @@ -17,6 +61,13 @@ export const InvalidateQueryProvider = () => { if (currentVisibilityRef.current === pageVisibility) { return } + + const now = Date.now() + if (now - currentTimeRef.current < slateTime) { + return + } + + currentTimeRef.current = now currentVisibilityRef.current = pageVisibility if (pageVisibility) { appLog("Window switch to visible, invalidate all queries") @@ -25,3 +76,7 @@ export const InvalidateQueryProvider = () => { }, [pageVisibility, queryClient]) return null } + +export const InvalidateQueryProvider = window.electron ? + InvalidateQueryProviderElectron : + InvalidateQueryProviderWebApp diff --git a/src/shared/src/bridge.ts b/src/shared/src/bridge.ts index 1325807b4..1df119eed 100644 --- a/src/shared/src/bridge.ts +++ b/src/shared/src/bridge.ts @@ -8,6 +8,8 @@ interface RenderGlobalContext { showSetting: () => void getGeneralSettings: () => GeneralSettings getUISettings: () => UISettings + + invalidateQueries: () => void toast: typeof toast }