Stop the settings write loop between windows

Two or more windows saved the appearance section to each other without end. The host stores object keys in alphabetical order. ThemeSync compared the stored value and the normalized value as JSON text, so a different key order started a new save in each window. Each save replaced settings.json, about 150 times each second. On a SATA SSD without a DRAM cache, these writes stalled all disk access for 6 to 8 seconds, about every 40 seconds.

- ThemeSync compares the two values with sameSettingsValue, which ignores the key order.
- The host skips a write when the section already holds the value, and sends no settings-changed event. This also stops a similar loop in another section.
- New tests: key order in sameSettingsValue, an appearance from the host that needs no new save, and an unchanged host write.
This commit is contained in:
Knotty Wyvern
2026-11-25 07:32:37 -07:00
parent b52eeb14a1
commit 1f3179facc
5 changed files with 97 additions and 5 deletions
+17
View File
@@ -3,6 +3,7 @@ import { test } from 'node:test';
import { parseCustomTheme } from '../../../src/lib/theme/customThemes.ts';
import { SYNTAX_ROLES } from '../../../src/lib/theme/roles.ts';
import { parseAppearance, normalizeAppearance, exportAppearanceTheme, BUILTIN_THEMES, THEMES, themeModes } from '../../../src/lib/theme/appearance.ts';
import { sameSettingsValue } from '../../../src/lib/settings/store.ts';
test('Removed palettes preserve the saved mode with the default palette', () => {
for (const theme of ['gpt', 'terminal', 'unknown', 'renamon', 'asuka-school']) {
@@ -84,3 +85,19 @@ test('Background image references clamp the overlay and discard obsolete colors'
assert.deepEqual(normalizeAppearance({ newTabBackground: { imageId, overlay: NaN } }).newTabBackground, { imageId, overlay: 0, ...layout });
assert.equal(normalizeAppearance({ newTabBackground: { imageId: 'invalid' } }).newTabBackground, undefined);
});
test('A valid appearance from the host needs no new save, although the host sorted its keys', () => {
// ThemeSync saves the normalized appearance only when it differs from the stored value.
// A key-order difference made two windows save the same value to each other without end.
const imageId = 'd10adda6-a2ec-4d73-a0a6-2cb1ed43bebc';
const fromHost = [
{ mode: 'dark', theme: 'harvest-moon', transparency: { amount: 18, enabled: true } },
{ mode: 'light', newTabBackground: { imageId, overlay: 0.2, position: 'center', size: 'cover' }, theme: 'default' },
];
for (const stored of fromHost) {
const normalized = normalizeAppearance(stored);
assert.notEqual(JSON.stringify(normalized), JSON.stringify(stored), 'The fixture has a different key order');
assert.ok(sameSettingsValue(stored, normalized), JSON.stringify(stored));
}
assert.ok(!sameSettingsValue({ mode: 'dark', theme: 'holo' }, normalizeAppearance({ mode: 'dark', theme: 'holo' })), 'A renamed theme still gets saved');
});