diff --git a/src/main/config/app.ts b/src/main/config/app.ts index 443c843..5418932 100644 --- a/src/main/config/app.ts +++ b/src/main/config/app.ts @@ -9,14 +9,16 @@ let appConfigWriteQueue: Promise = Promise.resolve() export async function getAppConfig(force = false): Promise { if (force || !appConfig) { - const data = await readFile(appConfigPath(), 'utf-8') - const parsedConfig = parse(data) - const mergedConfig = deepMerge({ ...defaultConfig }, parsedConfig || {}) - if (JSON.stringify(mergedConfig) !== JSON.stringify(parsedConfig)) { - await writeFile(appConfigPath(), stringify(mergedConfig)) - } - - appConfig = mergedConfig + appConfigWriteQueue = appConfigWriteQueue.then(async () => { + const data = await readFile(appConfigPath(), 'utf-8') + const parsedConfig = parse(data) + const mergedConfig = deepMerge({ ...defaultConfig }, parsedConfig || {}) + if (JSON.stringify(mergedConfig) !== JSON.stringify(parsedConfig)) { + await writeFile(appConfigPath(), stringify(mergedConfig)) + } + appConfig = mergedConfig + }) + await appConfigWriteQueue } if (typeof appConfig !== 'object') appConfig = defaultConfig return appConfig diff --git a/src/main/core/profileUpdater.ts b/src/main/core/profileUpdater.ts index f3f3aee..b500e74 100644 --- a/src/main/core/profileUpdater.ts +++ b/src/main/core/profileUpdater.ts @@ -3,6 +3,7 @@ import { Cron } from 'croner' import { logger } from '../utils/logger' const intervalPool: Record = {} +const delayedUpdatePool: Record = {} async function updateProfile(id: string): Promise { const item = await getProfileItem(id) @@ -61,8 +62,9 @@ export async function initProfileUpdater(): Promise { currentItem.interval * 60 * 1000 ) - setTimeout( + delayedUpdatePool[currentId] = setTimeout( async () => { + delete delayedUpdatePool[currentId] try { await updateProfile(currentId) } catch (e) { @@ -132,4 +134,8 @@ export async function removeProfileUpdater(id: string): Promise { } delete intervalPool[id] } + if (delayedUpdatePool[id]) { + clearTimeout(delayedUpdatePool[id]) + delete delayedUpdatePool[id] + } } diff --git a/src/main/sys/ssid.ts b/src/main/sys/ssid.ts index 823d089..82c4e0e 100644 --- a/src/main/sys/ssid.ts +++ b/src/main/sys/ssid.ts @@ -32,6 +32,8 @@ export async function getCurrentSSID(): Promise { } let lastSSID: string | undefined +let ssidCheckInterval: NodeJS.Timeout | null = null + export async function checkSSID(): Promise { try { const { pauseSSID = [] } = await getAppConfig() @@ -56,8 +58,18 @@ export async function checkSSID(): Promise { } export async function startSSIDCheck(): Promise { + if (ssidCheckInterval) { + clearInterval(ssidCheckInterval) + } await checkSSID() - setInterval(checkSSID, 30000) + ssidCheckInterval = setInterval(checkSSID, 30000) +} + +export function stopSSIDCheck(): void { + if (ssidCheckInterval) { + clearInterval(ssidCheckInterval) + ssidCheckInterval = null + } } async function getSSIDByAirport(): Promise {