fix: invalidate query

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-07-27 12:39:30 +08:00
parent 5e3a38c20c
commit a0d206d8d3
4 changed files with 68 additions and 2 deletions
+2
View File
@@ -197,6 +197,8 @@ export const createMainWindow = () => {
window.on("show", () => {
cancelPollingUpdateUnreadCount()
callGlobalContextMethod(window, "invalidateQueries")
})
window.on("hide", async () => {
+7
View File
@@ -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,
})
@@ -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
+2
View File
@@ -8,6 +8,8 @@ interface RenderGlobalContext {
showSetting: () => void
getGeneralSettings: () => GeneralSettings
getUISettings: () => UISettings
invalidateQueries: () => void
toast: typeof toast
}