fix(settings): prevent refresh cycle from reverting unsaved toggle changes
CI / Build & Test (push) Successful in 2m55s
CI / Build & Test (push) Successful in 2m55s
Track a _settingsDirty flag that is set whenever the user interacts with a settings toggle or number input. updateSettingsUI() skips the sync while dirty so the 2-second refresh cycle cannot revert in-progress edits. The flag is cleared after a successful save or reset.
This commit is contained in:
@@ -4,11 +4,25 @@
|
||||
* Depends on: core/utils.js ($), core/state.js (settings, SETTINGS_DEFAULTS), ui/toast.js (showToast)
|
||||
*/
|
||||
|
||||
/**
|
||||
* True while the user has made unsaved changes to settings controls.
|
||||
* Prevents the 2-second refresh cycle from overwriting in-progress edits.
|
||||
*/
|
||||
let _settingsDirty = false;
|
||||
|
||||
/** Mark settings as having unsaved changes. */
|
||||
function _markSettingsDirty() { _settingsDirty = true; }
|
||||
|
||||
/** Clear the dirty flag (called after a successful save or reset). */
|
||||
function _clearSettingsDirty() { _settingsDirty = false; }
|
||||
|
||||
/**
|
||||
* Sync all settings form controls with the current `settings` object.
|
||||
* Called on every refresh cycle to keep the UI in sync with persisted state.
|
||||
* Skipped while the user has unsaved changes to avoid reverting their edits.
|
||||
*/
|
||||
function updateSettingsUI() {
|
||||
if (_settingsDirty) return;
|
||||
$('toggleNotify')?.classList.toggle('active', settings.notifyOnDisconnect === true);
|
||||
$('toggleNotifyTunnelError')?.classList.toggle('active', settings.notifyOnTunnelError !== false);
|
||||
$('toggleTunnelAutoReconnect')?.classList.toggle('active', settings.tunnelAutoReconnect === true);
|
||||
@@ -49,6 +63,7 @@ function saveSettings() {
|
||||
if (chrome.runtime.lastError) { showToast('Settings save failed: ' + chrome.runtime.lastError.message, 'error'); return; }
|
||||
if (response && response.ok) {
|
||||
if (response.settings) settings = { ...SETTINGS_DEFAULTS, ...response.settings };
|
||||
_clearSettingsDirty();
|
||||
// Restart the ping interval so the new interval / enabled state takes effect immediately
|
||||
if (typeof window._restartPingInterval === 'function') window._restartPingInterval();
|
||||
if (response.requiresRestart) {
|
||||
|
||||
Reference in New Issue
Block a user