@@ -197,6 +197,8 @@ export const createMainWindow = () => {
|
||||
|
||||
window.on("show", () => {
|
||||
cancelPollingUpdateUnreadCount()
|
||||
|
||||
callGlobalContextMethod(window, "invalidateQueries")
|
||||
})
|
||||
|
||||
window.on("hide", async () => {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -8,6 +8,8 @@ interface RenderGlobalContext {
|
||||
showSetting: () => void
|
||||
getGeneralSettings: () => GeneralSettings
|
||||
getUISettings: () => UISettings
|
||||
|
||||
invalidateQueries: () => void
|
||||
toast: typeof toast
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user