Skip to content

Commit

Permalink
feat: only closing window can trigger query invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Aug 26, 2024
1 parent a09f55a commit a8e48f4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 36 deletions.
32 changes: 16 additions & 16 deletions src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<z.ZodObject<{
field: z.ZodEnum<["view", "title", "site_url", "feed_url"]>;
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<{
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -185,16 +185,16 @@ declare const actionsOpenAPISchema: z.ZodObject<z.objectUtil.extendShape<Omit<{
rules: z.ZodNullable<z.ZodOptional<z.ZodArray<z.ZodObject<{
name: z.ZodString;
condition: z.ZodArray<z.ZodObject<{
field: z.ZodEnum<["view", "title", "site_url", "feed_url"]>;
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<{
Expand Down Expand Up @@ -252,7 +252,7 @@ declare const actionsOpenAPISchema: z.ZodObject<z.objectUtil.extendShape<Omit<{
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: {
Expand All @@ -272,7 +272,7 @@ declare const actionsOpenAPISchema: z.ZodObject<z.objectUtil.extendShape<Omit<{
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: {
Expand All @@ -295,7 +295,7 @@ declare const actionsOpenAPISchema: z.ZodObject<z.objectUtil.extendShape<Omit<{
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: {
Expand All @@ -318,7 +318,7 @@ declare const actionsOpenAPISchema: z.ZodObject<z.objectUtil.extendShape<Omit<{
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: {
Expand Down Expand Up @@ -458,6 +458,8 @@ type MediaModel = {
url: string;
type: "photo" | "video";
preview_image_url?: string;
width?: number;
height?: number;
};
type AttachmentsModel = {
url: string;
Expand Down Expand Up @@ -2751,7 +2753,7 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
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: {
Expand Down Expand Up @@ -2780,7 +2782,7 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
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: {
Expand Down Expand Up @@ -3231,8 +3233,6 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
type: "photo" | "video";
url: string;
preview_image_url?: string | undefined;
height?: number | undefined;
width?: number | undefined;
}[] | null | undefined;
attachments?: {
url: string;
Expand Down
4 changes: 3 additions & 1 deletion src/main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ export const createMainWindow = () => {
if (isMacOS) {
event.preventDefault()
window.hide()

callGlobalContextMethod(window, "electronClose")
} else {
windows.mainWindow = null
}
Expand All @@ -260,7 +262,7 @@ export const createMainWindow = () => {
window.on("show", () => {
cancelPollingUpdateUnreadCount()

callGlobalContextMethod(window, "invalidateQueries")
callGlobalContextMethod(window, "electronShow")
})

window.on("hide", async () => {
Expand Down
10 changes: 7 additions & 3 deletions src/renderer/src/initialize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
})

Expand Down
46 changes: 31 additions & 15 deletions src/renderer/src/providers/invalidate-query-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/shared/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ interface RenderGlobalContext {
getGeneralSettings: () => GeneralSettings
getUISettings: () => UISettings

invalidateQueries: () => void
electronClose: () => void
electronShow: () => void

toast: typeof toast
}

Expand Down

0 comments on commit a8e48f4

Please sign in to comment.