diff --git a/src/main/config/controledMihomo.ts b/src/main/config/controledMihomo.ts index 9250deb..312ecfb 100644 --- a/src/main/config/controledMihomo.ts +++ b/src/main/config/controledMihomo.ts @@ -11,6 +11,7 @@ import { createLogger } from '../utils/logger' const controledMihomoLogger = createLogger('ControledMihomo') let controledMihomoConfig: Partial // mihomo.yaml +let controledMihomoWriteQueue: Promise = Promise.resolve() export async function getControledMihomoConfig(force = false): Promise> { if (force || !controledMihomoConfig) { @@ -39,29 +40,32 @@ export async function getControledMihomoConfig(force = false): Promise): Promise { - const { controlDns = true, controlSniff = true } = await getAppConfig() + controledMihomoWriteQueue = controledMihomoWriteQueue.then(async () => { + const { controlDns = true, controlSniff = true } = await getAppConfig() - if (patch.hosts) { - controledMihomoConfig.hosts = patch.hosts - } - if (patch.dns?.['nameserver-policy']) { - controledMihomoConfig.dns = controledMihomoConfig.dns || {} - controledMihomoConfig.dns['nameserver-policy'] = patch.dns['nameserver-policy'] - } - controledMihomoConfig = deepMerge(controledMihomoConfig, patch) + if (patch.hosts) { + controledMihomoConfig.hosts = patch.hosts + } + if (patch.dns?.['nameserver-policy']) { + controledMihomoConfig.dns = controledMihomoConfig.dns || {} + controledMihomoConfig.dns['nameserver-policy'] = patch.dns['nameserver-policy'] + } + controledMihomoConfig = deepMerge(controledMihomoConfig, patch) - // 从不接管状态恢复 - if (controlDns) { - // 确保 DNS 配置包含所有必要的默认字段,特别是新增的 fallback 等 - controledMihomoConfig.dns = deepMerge( - defaultControledMihomoConfig.dns || {}, - controledMihomoConfig.dns || {} - ) - } - if (controlSniff && !controledMihomoConfig.sniffer) { - controledMihomoConfig.sniffer = defaultControledMihomoConfig.sniffer - } + // 从不接管状态恢复 + if (controlDns) { + // 确保 DNS 配置包含所有必要的默认字段,特别是新增的 fallback 等 + controledMihomoConfig.dns = deepMerge( + defaultControledMihomoConfig.dns || {}, + controledMihomoConfig.dns || {} + ) + } + if (controlSniff && !controledMihomoConfig.sniffer) { + controledMihomoConfig.sniffer = defaultControledMihomoConfig.sniffer + } - await generateProfile() - await writeFile(controledMihomoConfigPath(), stringify(controledMihomoConfig), 'utf-8') + await generateProfile() + await writeFile(controledMihomoConfigPath(), stringify(controledMihomoConfig), 'utf-8') + }) + await controledMihomoWriteQueue } diff --git a/src/main/config/profile.ts b/src/main/config/profile.ts index 23719d0..ea695cd 100644 --- a/src/main/config/profile.ts +++ b/src/main/config/profile.ts @@ -20,7 +20,7 @@ const profileLogger = createLogger('Profile') let profileConfig: IProfileConfig let profileConfigWriteQueue: Promise = Promise.resolve() -let targetProfileId: string | null = null +let changeProfileQueue: Promise = Promise.resolve() export async function getProfileConfig(force = false): Promise { if (force || !profileConfig) { @@ -63,37 +63,27 @@ export async function getProfileItem(id: string | undefined): Promise { - const { current } = await getProfileConfig() + // 使用队列确保 profile 切换串行执行,避免竞态条件 + changeProfileQueue = changeProfileQueue.then(async () => { + const { current } = await getProfileConfig() + if (current === id) return - if (current === id && targetProfileId !== id) { - return - } - - targetProfileId = id - - try { - await updateProfileConfig((config) => { - config.current = id - return config - }) - - if (targetProfileId !== id) { - return - } - await restartCore() - if (targetProfileId === id) { - targetProfileId = null - } - } catch (e) { - if (targetProfileId === id) { + try { + await updateProfileConfig((config) => { + config.current = id + return config + }) + await restartCore() + } catch (e) { + // 回滚配置 await updateProfileConfig((config) => { config.current = current return config }) - targetProfileId = null throw e } - } + }) + await changeProfileQueue } export async function updateProfileItem(item: IProfileItem): Promise { diff --git a/src/main/window.ts b/src/main/window.ts index 9dcbfc9..8a4e3bf 100644 --- a/src/main/window.ts +++ b/src/main/window.ts @@ -6,7 +6,7 @@ import { getAppConfig } from './config' import { quitWithoutCore, stopCore } from './core/manager' import { triggerSysProxy } from './sys/sysproxy' import { hideDockIcon, showDockIcon } from './resolve/tray' -import icon from '../resources/icon.png?asset' +import icon from '../../resources/icon.png?asset' export let mainWindow: BrowserWindow | null = null let quitTimeout: NodeJS.Timeout | null = null @@ -78,7 +78,8 @@ function setupWindowEvents( scheduleQuitWithoutCore(autoQuitWithoutCoreDelay) } - if (!silentStart) { + // 开发模式下始终显示窗口 + if (!silentStart || is.dev) { clearQuitTimeout() window.show() window.focusOnWebView()