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