From 5be1a681ca6dba3d24feba3dd324afc8129443c3 Mon Sep 17 00:00:00 2001 From: heixiansen Date: Fri, 9 May 2025 11:39:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=AF=8F=E6=AC=A1=E6=89=93?= =?UTF-8?q?=E5=BC=80=E7=AA=97=E5=8F=A3=E4=BF=A1=E6=81=AF=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E4=BC=9A=E5=87=BA=E7=8E=B0=E4=B8=8D=E5=90=8C=E6=AD=A5=E7=9A=84?= =?UTF-8?q?bug=EF=BC=88=E5=A6=82=E6=97=A5=E5=BF=97=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E4=B8=8D=E6=9B=B4=E6=96=B0=EF=BC=89=EF=BC=88=E5=AD=90=E7=BD=91?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E6=8C=89=E9=92=AE=E7=82=B9=E4=B8=8D=E8=B5=B7?= =?UTF-8?q?=E7=AD=89=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composables/windows.ts | 67 ++++++++++++++++++++++++------------------ pages/index.vue | 2 +- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/composables/windows.ts b/composables/windows.ts index 9019823..1dd5132 100644 --- a/composables/windows.ts +++ b/composables/windows.ts @@ -5,16 +5,31 @@ import type { WebviewLabel, WebviewOptions } from "@tauri-apps/api/webview"; import useMainStore from "@/stores/index"; import { onBeforeUnmount } from "vue"; -const _listenersMaps: { [key: string]: [UnlistenFn | null, UnlistenFn | null] } = {}; +const _listenersMaps: { [key: string]: [UnlistenFn | null] } = {}; +const dealCloseListener = async ( + dialog: WebviewWindow, + label: string, + beforeCloseFunc?: () => void | null +) => { + const unlistenFnList: [UnlistenFn | null] = _listenersMaps[label] || [null]; + let [unListenlogClose] = unlistenFnList; + + unListenlogClose && (await unListenlogClose()); + + unListenlogClose = await dialog.onCloseRequested(async () => { + beforeCloseFunc && (await beforeCloseFunc()); + unListenlogClose && (await unListenlogClose()); + console.log("close") + }); +}; export default async ( label: WebviewLabel, options?: Omit & WindowOptions, afterCreatedFunc?: (webviewWindow: WebviewWindow, appWindow: Window) => void | null, beforeCloseFunc?: () => void | null ) => { - const unlistenFnList: [UnlistenFn | null, UnlistenFn | null] = _listenersMaps[label] || [null, null]; - let [unlistenLogCreated, unListenlogClose] = unlistenFnList; + const unlistenFnList: [UnlistenFn | null] = _listenersMaps[label] || [null]; let dialog = await WebviewWindow.getByLabel(label); if (!dialog) { let defaultOpts: { [key: string]: any; parent: Window | undefined } = { @@ -38,28 +53,20 @@ export default async ( defaultOpts.x = logicalPosition.x; defaultOpts.y = logicalPosition.y; } - unlistenLogCreated && (await (unlistenLogCreated as Function)()); - unListenlogClose && (await unListenlogClose()); dialog = new WebviewWindow(label, { ...defaultOpts, ...options }); - unlistenLogCreated = await dialog.listen("tauri://webview-created", async () => { - if (dialog) { - afterCreatedFunc && (await afterCreatedFunc(dialog, appWindow)); - await dialog.show(); - } - }); - unlistenFnList[0] = unlistenLogCreated; - unListenlogClose = await dialog.onCloseRequested(async () => { - beforeCloseFunc && (await beforeCloseFunc()); - unListenlogClose && (await unListenlogClose()); - unlistenLogCreated && (await (unlistenLogCreated as Function)()); - }); - unlistenFnList[1] = unlistenLogCreated; - _listenersMaps[label] = unlistenFnList; + await dealCloseListener(dialog, label, beforeCloseFunc); + afterCreatedFunc && (await afterCreatedFunc(dialog, appWindow)); + await dialog.show(); + } else { const visible = await dialog.isVisible(); if (visible) { await dialog.close(); - unlistenLogCreated && (await (unlistenLogCreated as Function)()); + } else { + const appWindow = getCurrentWindow(); + await dealCloseListener(dialog, label, beforeCloseFunc); + afterCreatedFunc && (await afterCreatedFunc(dialog, appWindow)); + await dialog.show(); } } }; @@ -68,15 +75,19 @@ export const dataSubscribe = async (cb?: (...args: any) => any) => { if (!cb) return; const mainStore = useMainStore(); const abort = new AbortController(); - window.addEventListener("storage", async () => { - mainStore.$hydrate(); - if (cb && cb instanceof Function) { - await cb(); + window.addEventListener( + "storage", + async () => { + mainStore.$hydrate(); + if (cb && cb instanceof Function) { + await cb(); + } + }, + { + signal: abort.signal } - }, { - signal: abort.signal - }) + ); onBeforeUnmount(() => { abort?.abort(); - }) + }); }; diff --git a/pages/index.vue b/pages/index.vue index c0525bb..eaa0573 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1827,7 +1827,7 @@ cidrTimer && clearInterval(cidrTimer); cidrTimer = setInterval(() => { appWindow.emitTo({ kind: "WebviewWindow", label: "cidr" }, "route", data.isStart); - }, 1000); + }, 650); }, () => { cidrTimer && clearInterval(cidrTimer);