From 1edafc637a070b1d14860e1a75911c221069355f Mon Sep 17 00:00:00 2001 From: hkfires <10558748+hkfires@users.noreply.github.com> Date: Fri, 21 Nov 2025 11:34:12 +0800 Subject: [PATCH] feat: centralize config refresh handling and prevent races --- app.js | 28 ++++++++++++++++++++++++++++ src/modules/config-editor.js | 4 +++- src/modules/language.js | 14 ++++++++++++-- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index df7694d..f903604 100644 --- a/app.js +++ b/app.js @@ -68,6 +68,9 @@ class CLIProxyManager { // 状态更新定时器 this.statusUpdateTimer = null; this.lastConnectionStatusEmitted = null; + this.isGlobalRefreshInProgress = false; + + this.registerCoreEventHandlers(); // 日志自动刷新定时器 this.logsRefreshTimer = null; @@ -149,6 +152,7 @@ class CLIProxyManager { init() { this.initUiVersion(); this.initializeTheme(); + this.registerCoreEventHandlers(); this.registerSettingsListeners(); this.registerUsageListeners(); if (typeof this.registerLogsListeners === 'function') { @@ -173,6 +177,30 @@ class CLIProxyManager { } } + registerCoreEventHandlers() { + if (!this.events || typeof this.events.on !== 'function') { + return; + } + this.events.on('config:refresh-requested', async (event) => { + const detail = event?.detail || {}; + const forceRefresh = detail.forceRefresh !== false; + // 避免并发触发导致重复请求 + if (this.isGlobalRefreshInProgress) { + return; + } + await this.runGlobalRefresh(forceRefresh); + }); + } + + async runGlobalRefresh(forceRefresh = false) { + this.isGlobalRefreshInProgress = true; + try { + await this.loadAllData(forceRefresh); + } finally { + this.isGlobalRefreshInProgress = false; + } + } + // 检查主机名并隐藏 OAuth 登录框 checkHostAndHideOAuth() { const hostname = window.location.hostname; diff --git a/src/modules/config-editor.js b/src/modules/config-editor.js index 7834bac..aac7928 100644 --- a/src/modules/config-editor.js +++ b/src/modules/config-editor.js @@ -209,7 +209,9 @@ export const configEditorModule = { this.showNotification(i18n.t('config_management.save_success'), 'success'); this.updateConfigEditorStatus('success', i18n.t('config_management.status_saved')); this.clearCache(); - await this.loadAllData(true); + if (this.events && typeof this.events.emit === 'function') { + this.events.emit('config:refresh-requested', { forceRefresh: true }); + } } catch (error) { const errorMessage = `${i18n.t('config_management.status_save_failed')}: ${error.message}`; this.updateConfigEditorStatus('error', errorMessage); diff --git a/src/modules/language.js b/src/modules/language.js index 122c3f7..553623c 100644 --- a/src/modules/language.js +++ b/src/modules/language.js @@ -12,6 +12,11 @@ export const languageModule = { }, toggleLanguage() { + if (this.isLanguageRefreshInProgress) { + return; + } + this.isLanguageRefreshInProgress = true; + const currentLang = i18n.currentLanguage; const newLang = currentLang === 'zh-CN' ? 'en-US' : 'zh-CN'; i18n.setLanguage(newLang); @@ -19,8 +24,13 @@ export const languageModule = { this.updateThemeButtons(); this.updateConnectionStatus(); - if (this.isLoggedIn && this.isConnected) { - this.loadAllData(true); + if (this.isLoggedIn && this.isConnected && this.events && typeof this.events.emit === 'function') { + this.events.emit('config:refresh-requested', { forceRefresh: true }); } + + // 简单释放锁,避免短时间内的重复触发 + setTimeout(() => { + this.isLanguageRefreshInProgress = false; + }, 500); } };