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:
@@ -116,3 +116,17 @@ test('A failed write reaches the caller', async () => {
|
||||
handlers.set('settings_write', () => { throw 'Disk full'; });
|
||||
await assert.rejects(store.writeSection('customThemes', []), /Disk full/);
|
||||
});
|
||||
|
||||
test('The same data in a different key order is the same settings value', () => {
|
||||
const saved = { theme: 'harvest-moon', mode: 'dark', transparency: { enabled: true, amount: 18 }, targets: ['app', 'pm-all'] };
|
||||
// The host sends the keys back in alphabetical order.
|
||||
const fromHost = { mode: 'dark', targets: ['app', 'pm-all'], theme: 'harvest-moon', transparency: { amount: 18, enabled: true } };
|
||||
assert.notEqual(JSON.stringify(saved), JSON.stringify(fromHost));
|
||||
assert.ok(store.sameSettingsValue(saved, fromHost));
|
||||
assert.ok(!store.sameSettingsValue(saved, { ...fromHost, mode: 'light' }));
|
||||
assert.ok(!store.sameSettingsValue(saved, { ...fromHost, targets: ['pm-all', 'app'] }), 'The order of an array counts');
|
||||
assert.ok(!store.sameSettingsValue(saved, { ...fromHost, extra: 1 }));
|
||||
assert.ok(store.sameSettingsValue('en', 'en'));
|
||||
assert.ok(store.sameSettingsValue(null, null));
|
||||
assert.ok(!store.sameSettingsValue(null, {}));
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user