diff --git a/src/hono.ts b/src/hono.ts index a8cbd1cbbc..ee74a23800 100644 --- a/src/hono.ts +++ b/src/hono.ts @@ -70,16 +70,16 @@ declare const languageSchema: z.ZodEnum<["en", "ja", "zh-CN", "zh-TW"]>; declare const actionsItemOpenAPISchema: z.ZodObject<{ name: z.ZodString; condition: z.ZodArray; + field: z.ZodEnum<["view", "title", "site_url", "feed_url", "category"]>; operator: z.ZodEnum<["contains", "not_contains", "eq", "not_eq", "gt", "lt", "regex"]>; value: z.ZodString; }, "strip", z.ZodTypeAny, { value: string; - field: "title" | "view" | "site_url" | "feed_url"; + field: "title" | "view" | "site_url" | "category" | "feed_url"; operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"; }, { value: string; - field: "title" | "view" | "site_url" | "feed_url"; + field: "title" | "view" | "site_url" | "category" | "feed_url"; operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"; }>, "many">; result: z.ZodObject<{ @@ -137,7 +137,7 @@ declare const actionsItemOpenAPISchema: z.ZodObject<{ name: string; condition: { value: string; - field: "title" | "view" | "site_url" | "feed_url"; + field: "title" | "view" | "site_url" | "category" | "feed_url"; operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"; }[]; result: { @@ -157,7 +157,7 @@ declare const actionsItemOpenAPISchema: z.ZodObject<{ name: string; condition: { value: string; - field: "title" | "view" | "site_url" | "feed_url"; + field: "title" | "view" | "site_url" | "category" | "feed_url"; operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"; }[]; result: { @@ -185,16 +185,16 @@ declare const actionsOpenAPISchema: z.ZodObject; + field: z.ZodEnum<["view", "title", "site_url", "feed_url", "category"]>; operator: z.ZodEnum<["contains", "not_contains", "eq", "not_eq", "gt", "lt", "regex"]>; value: z.ZodString; }, "strip", z.ZodTypeAny, { value: string; - field: "title" | "view" | "site_url" | "feed_url"; + field: "title" | "view" | "site_url" | "category" | "feed_url"; operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"; }, { value: string; - field: "title" | "view" | "site_url" | "feed_url"; + field: "title" | "view" | "site_url" | "category" | "feed_url"; operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"; }>, "many">; result: z.ZodObject<{ @@ -252,7 +252,7 @@ declare const actionsOpenAPISchema: z.ZodObject { if (isMacOS) { event.preventDefault() window.hide() + + callGlobalContextMethod(window, "electronClose") } else { windows.mainWindow = null } @@ -260,7 +262,7 @@ export const createMainWindow = () => { window.on("show", () => { cancelPollingUpdateUnreadCount() - callGlobalContextMethod(window, "invalidateQueries") + callGlobalContextMethod(window, "electronShow") }) window.on("hide", async () => { diff --git a/src/renderer/src/initialize/index.ts b/src/renderer/src/initialize/index.ts index 0213c22b3d..a1c92d7215 100644 --- a/src/renderer/src/initialize/index.ts +++ b/src/renderer/src/initialize/index.ts @@ -5,7 +5,7 @@ import { getUISettings } from "@renderer/atoms/settings/ui" import { isElectronBuild } from "@renderer/constants" import { browserDB } from "@renderer/database" import { getStorageNS } from "@renderer/lib/ns" -import { InvalidateQueryEvent } from "@renderer/providers/invalidate-query-provider" +import { ElectronCloseEvent, ElectronShowEvent } from "@renderer/providers/invalidate-query-provider" import { CleanerService } from "@renderer/services/cleaner" import { registerGlobalContext } from "@shared/bridge" import dayjs from "dayjs" @@ -111,9 +111,13 @@ export const initializeApp = async () => { /** * Electron app only */ - invalidateQueries() { - document.dispatchEvent(new InvalidateQueryEvent()) + electronClose() { + document.dispatchEvent(new ElectronCloseEvent()) }, + electronShow() { + document.dispatchEvent(new ElectronShowEvent()) + }, + toast, }) diff --git a/src/renderer/src/providers/invalidate-query-provider.tsx b/src/renderer/src/providers/invalidate-query-provider.tsx index 9ae63d94a1..5e35a8df13 100644 --- a/src/renderer/src/providers/invalidate-query-provider.tsx +++ b/src/renderer/src/providers/invalidate-query-provider.tsx @@ -5,12 +5,16 @@ import { useEffect, useRef } from "react" const slateTime = 600000 // 10min -const DISPATCH_KEY = "invalidate-query-key" - -export class InvalidateQueryEvent extends Event { - static type = DISPATCH_KEY +export class ElectronCloseEvent extends Event { + static type = "electron-close" constructor() { - super(DISPATCH_KEY) + super("electron-close") + } +} +export class ElectronShowEvent extends Event { + static type = "electron-show" + constructor() { + super("electron-show") } } @@ -21,25 +25,37 @@ export class InvalidateQueryEvent extends Event { const InvalidateQueryProviderElectron = () => { const queryClient = useQueryClient() - const currentTimeRef = useRef(Date.now()) + const currentTimeRef = useRef(0) useEffect(() => { const handler = () => { - const now = Date.now() - if (now - currentTimeRef.current < slateTime) { - return - } + currentTimeRef.current = Date.now() + appLog("Window switch to close") + } - currentTimeRef.current = now + document.addEventListener(ElectronCloseEvent.type, handler) - appLog("Window switch to visible, invalidate all queries") - queryClient.invalidateQueries() + return () => { + document.removeEventListener(ElectronCloseEvent.type, handler) + } + }, [queryClient]) + + useEffect(() => { + const handler = () => { + const now = Date.now() + if (!currentTimeRef.current || (now - currentTimeRef.current < slateTime)) { + appLog(`Window switch to visible, but skip invalidation, ${currentTimeRef.current ? now - currentTimeRef.current : 0}`) + } else { + appLog("Window switch to visible, invalidate all queries") + queryClient.invalidateQueries() + } + currentTimeRef.current = 0 } - document.addEventListener(InvalidateQueryEvent.type, handler) + document.addEventListener(ElectronShowEvent.type, handler) return () => { - document.removeEventListener(InvalidateQueryEvent.type, handler) + document.removeEventListener(ElectronShowEvent.type, handler) } }, [queryClient]) return null diff --git a/src/shared/src/bridge.ts b/src/shared/src/bridge.ts index 409666d2cc..b67d910f2c 100644 --- a/src/shared/src/bridge.ts +++ b/src/shared/src/bridge.ts @@ -9,7 +9,9 @@ interface RenderGlobalContext { getGeneralSettings: () => GeneralSettings getUISettings: () => UISettings - invalidateQueries: () => void + electronClose: () => void + electronShow: () => void + toast: typeof toast }