mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2026-02-10 11:40:28 +08:00
fix: add config write queue and prevent ipc listener accumulation
This commit is contained in:
parent
b60c01bb4c
commit
818b546817
@ -5,6 +5,7 @@ import { deepMerge } from '../utils/merge'
|
||||
import { defaultConfig } from '../utils/template'
|
||||
|
||||
let appConfig: IAppConfig // config.yaml
|
||||
let appConfigWriteQueue: Promise<void> = Promise.resolve()
|
||||
|
||||
export async function getAppConfig(force = false): Promise<IAppConfig> {
|
||||
if (force || !appConfig) {
|
||||
@ -22,9 +23,12 @@ export async function getAppConfig(force = false): Promise<IAppConfig> {
|
||||
}
|
||||
|
||||
export async function patchAppConfig(patch: Partial<IAppConfig>): Promise<void> {
|
||||
if (patch.nameserverPolicy) {
|
||||
appConfig.nameserverPolicy = patch.nameserverPolicy
|
||||
}
|
||||
appConfig = deepMerge(appConfig, patch)
|
||||
await writeFile(appConfigPath(), stringify(appConfig))
|
||||
appConfigWriteQueue = appConfigWriteQueue.then(async () => {
|
||||
if (patch.nameserverPolicy) {
|
||||
appConfig.nameserverPolicy = patch.nameserverPolicy
|
||||
}
|
||||
appConfig = deepMerge(appConfig, patch)
|
||||
await writeFile(appConfigPath(), stringify(appConfig))
|
||||
})
|
||||
await appConfigWriteQueue
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import * as chromeRequest from '../utils/chromeRequest'
|
||||
import { parse, stringify } from '../utils/yaml'
|
||||
|
||||
let overrideConfig: IOverrideConfig // override.yaml
|
||||
let overrideConfigWriteQueue: Promise<void> = Promise.resolve()
|
||||
|
||||
export async function getOverrideConfig(force = false): Promise<IOverrideConfig> {
|
||||
if (force || !overrideConfig) {
|
||||
@ -17,8 +18,11 @@ export async function getOverrideConfig(force = false): Promise<IOverrideConfig>
|
||||
}
|
||||
|
||||
export async function setOverrideConfig(config: IOverrideConfig): Promise<void> {
|
||||
overrideConfig = config
|
||||
await writeFile(overrideConfigPath(), stringify(overrideConfig), 'utf-8')
|
||||
overrideConfigWriteQueue = overrideConfigWriteQueue.then(async () => {
|
||||
overrideConfig = config
|
||||
await writeFile(overrideConfigPath(), stringify(overrideConfig), 'utf-8')
|
||||
})
|
||||
await overrideConfigWriteQueue
|
||||
}
|
||||
|
||||
export async function getOverrideItem(id: string | undefined): Promise<IOverrideItem | undefined> {
|
||||
|
||||
@ -16,6 +16,7 @@ import { mihomoUpgradeConfig } from '../core/mihomoApi'
|
||||
import i18next from 'i18next'
|
||||
|
||||
let profileConfig: IProfileConfig // profile.yaml
|
||||
let profileConfigWriteQueue: Promise<void> = Promise.resolve()
|
||||
// 最终选中订阅ID
|
||||
let targetProfileId: string | null = null
|
||||
|
||||
@ -29,8 +30,11 @@ export async function getProfileConfig(force = false): Promise<IProfileConfig> {
|
||||
}
|
||||
|
||||
export async function setProfileConfig(config: IProfileConfig): Promise<void> {
|
||||
profileConfig = config
|
||||
await writeFile(profileConfigPath(), stringify(config), 'utf-8')
|
||||
profileConfigWriteQueue = profileConfigWriteQueue.then(async () => {
|
||||
profileConfig = config
|
||||
await writeFile(profileConfigPath(), stringify(config), 'utf-8')
|
||||
})
|
||||
await profileConfigWriteQueue
|
||||
}
|
||||
|
||||
export async function getProfileItem(id: string | undefined): Promise<IProfileItem | undefined> {
|
||||
|
||||
@ -390,6 +390,8 @@ export async function createTray(): Promise<void> {
|
||||
if (!useDockIcon) {
|
||||
hideDockIcon()
|
||||
}
|
||||
// 移除旧监听器防止累积
|
||||
ipcMain.removeAllListeners('trayIconUpdate')
|
||||
ipcMain.on('trayIconUpdate', async (_, png: string) => {
|
||||
const image = nativeImage.createFromDataURL(png).resize({ height: 16 })
|
||||
image.setTemplateImage(true)
|
||||
@ -435,6 +437,8 @@ export async function createTray(): Promise<void> {
|
||||
triggerMainWindow()
|
||||
}
|
||||
})
|
||||
// 移除旧监听器防止累积
|
||||
ipcMain.removeAllListeners('updateTrayMenu')
|
||||
ipcMain.on('updateTrayMenu', async () => {
|
||||
await updateTrayMenu()
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user