From 01d475860b1d9745ffc65823113b6428722ef0c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=91=E5=85=88=E6=A3=AE?= Date: Wed, 5 Feb 2025 19:30:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9C=AC=E5=9C=B0=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E4=BB=A3=E7=A0=81=E8=B4=A8=E9=87=8F=E4=BD=BF=E7=94=A8?= =?UTF-8?q?pinia=20$hydrate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composables/windows.ts | 29 ++++++++++-------------- pages/advance.vue | 6 +---- pages/cidr.vue | 5 ++--- pages/index.vue | 51 +++++++++--------------------------------- pages/server.vue | 4 +--- pages/tool.vue | 14 +----------- 6 files changed, 27 insertions(+), 82 deletions(-) diff --git a/composables/windows.ts b/composables/windows.ts index f344f5e..9019823 100644 --- a/composables/windows.ts +++ b/composables/windows.ts @@ -67,21 +67,16 @@ export default async ( export const dataSubscribe = async (cb?: (...args: any) => any) => { if (!cb) return; const mainStore = useMainStore(); - const currentWindow = getCurrentWindow(); - let removeSubscribe = mainStore.$subscribe(async (...args) => { - const emitData = await cb(args); - await currentWindow.emitTo({ kind: "Window", label: "main" }, "config", emitData); - }); - const unlisten = currentWindow.listen("global-main-store", event => { - removeSubscribe && removeSubscribe(); - mainStore.$patch({ ...event.payload.store }); // 更新全局状态 - removeSubscribe = mainStore.$subscribe(async (...args) => { - const emitData = await cb(args); - await currentWindow.emitTo({ kind: "Window", label: "main" }, "config", emitData); - }); - }); - onBeforeUnmount(async () => { - unlisten && (await unlisten)(); - removeSubscribe && removeSubscribe(); - }); + const abort = new AbortController(); + window.addEventListener("storage", async () => { + mainStore.$hydrate(); + if (cb && cb instanceof Function) { + await cb(); + } + }, { + signal: abort.signal + }) + onBeforeUnmount(() => { + abort?.abort(); + }) }; diff --git a/pages/advance.vue b/pages/advance.vue index 7a608ec..27b1d81 100644 --- a/pages/advance.vue +++ b/pages/advance.vue @@ -302,7 +302,7 @@ guids.value = guidsValue && guidsValue.length > 0 ? guidsValue : []; }; - dataSubscribe(async (...a) => { + dataSubscribe(async () => { if (!mainStore.createConfigInEasytier) { // 考虑删除本地config.json文件 const configJsonPath = import.meta.env.VITE_CONFIG_FILE_NAME; @@ -322,9 +322,5 @@ } } } - return { - config: { ...mainStore.config }, - createConfigInEasytier: mainStore.createConfigInEasytier - }; }); diff --git a/pages/cidr.vue b/pages/cidr.vue index 7a8a7c1..8c04318 100644 --- a/pages/cidr.vue +++ b/pages/cidr.vue @@ -107,13 +107,12 @@ onMounted(async () => { unlistenStart = await listenStart(); + dataSubscribe(); }); onBeforeUnmount(() => { unlistenStart && unlistenStart(); }); - dataSubscribe(async (...a) => { - return { cidrEnable: mainStore.cidrEnable, config: { ...mainStore.config } }; - }); + diff --git a/pages/index.vue b/pages/index.vue index 239425d..9f69ab0 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -839,23 +839,11 @@ mainStore.basePeers = uniq([mainStore.config.serverUrl, ...mainStore.basePeers]); }; - // 手动触发持久化数据 - const persist = async (appWindow: Window) => { - try { - appWindow.emitTo({ kind: "Any" }, "global-main-store", { store: { ...mainStore.$state } }); - mainStore.$persist(); - } catch (err) { - console.error(err); - ElMessage.error("手动持久数据失败"); - } - }; - const listenObj: { [key: string]: any } = { unListenOutPut: null, unListenThreadId: null, unListenServerOutPut: null, unListenServerThreadId: null, - unListenConfigStart: null, unListenStartStopServer: null, thread_id: null, server_thread_id: ref(null), @@ -940,24 +928,7 @@ }); this.unListenServerThreadId = unListen; }, - async listenConfigStart() { - const appWindow = getCurrentWindow(); - const unListen = await listen("config", event => { - const ipv4 = config.ipv4; - mainStore.$patch(event.payload as any); - config.ipv4 = ipv4; - if (mainStore.config.enablePreventSleep) { - preventSleep(); - } else { - stopPreventSleep(); - } - persist(appWindow); - }); - const unListen2 = mainStore.$subscribe(() => { - persist(appWindow); - }); - this.unListenConfigStart = [unListen, unListen2]; - }, + async listenStartStopServer() { const unListen = await listen<{ args: Array }>("startStopServer", async event => { await listenObj?.unListenServerOutPut?.(); @@ -1137,14 +1108,6 @@ } } } - const b = bounce(600); - mainStore.$subscribe(async (...a) => { - if (mainStore.createConfigInEasytier) { - b(async () => { - await updateConfigJson(data.configJsonSeverUrl); - }); - } - }); }; const initAutoStartServer = async () => { @@ -1180,6 +1143,7 @@ let serverLogsTimer: NodeJS.Timeout | null = null; let cidrTimer: NodeJS.Timeout | null = null; + const b = bounce(600); onMounted(async () => { await initGuiJson(); await compatibleInitAutoStart(); @@ -1188,7 +1152,6 @@ await getCoreVersion(); await listenObj.listenThreadId(); await listenObj.listenServerThreadId(); - await listenObj.listenConfigStart(); await listenObj.listenStartStopServer(); await initConfigDir(); await initAutoStartServer(); @@ -1197,13 +1160,19 @@ mountedShow(); // 不需要await closePrevent(); data.hasNewVersion = await checkNewVersion(); + window.addEventListener("storage", () => { + mainStore.$hydrate(); + if (mainStore.createConfigInEasytier) { + b(async () => { + await updateConfigJson(data.configJsonSeverUrl); + }); + } + }); }); onBeforeUnmount(() => { unListenAll(); listenObj.unListenReleaseList && listenObj.unListenReleaseList(); - listenObj.unListenConfigStart && listenObj.unListenConfigStart[0](); - listenObj.unListenConfigStart && listenObj.unListenConfigStart[1](); listenObj.unListenStartStopServer && listenObj.unListenStartStopServer(); logsTimer && clearInterval(logsTimer); serverLogsTimer && clearInterval(serverLogsTimer); diff --git a/pages/server.vue b/pages/server.vue index 0a36999..c4285fa 100644 --- a/pages/server.vue +++ b/pages/server.vue @@ -123,8 +123,6 @@ await appWindow.emitTo("main", "startStopServer", { args }); }; - dataSubscribe(async (...a) => { - return { serverConfig: { ...mainStore.serverConfig }}; - }); + dataSubscribe(); diff --git a/pages/tool.vue b/pages/tool.vue index 09cdc9d..cab7e96 100644 --- a/pages/tool.vue +++ b/pages/tool.vue @@ -477,19 +477,6 @@ } }; - dataSubscribe(async (...a) => { - return { - winIpBcAutoStart: mainStore.winIpBcAutoStart, - winipBcStart: mainStore.winipBcStart, - winipBcPid: mainStore.winipBcPid, - forceBindIpBit: mainStore.forceBindIpBit, - delayInjectDll: mainStore.delayInjectDll, - forceBindInput: mainStore.forceBindInput, - forceBindFile: mainStore.forceBindFile, - config: { ...mainStore.config } - }; - }); - const getGuids = async () => { const guids = await invoke("get_network_adapter_guids"); data.guids = guids && guids.length > 0 ? guids : []; @@ -505,5 +492,6 @@ data.pingIp = mainStore.config.ipv4; initStartWinIpBroadcast(); getGuids(); + dataSubscribe(); });