Ignore stale theme loads after preference changes

- Add preference equality guard in theme hook
- Prevent outdated async theme loads from overriding newer selections
This commit is contained in:
Ben Davis committed 2026-05-06 02:21:19 -07:00
1 parent 2b7dcd0295
commit 90c0d7d95a
1 file changed
+11
+11
View File
@@ -111,6 +111,16 @@ function setStoredPreference(preference: ThemePreference) {
localStorage.setItem(STORAGE_KEY, preferenceToBuiltInTheme(preference));
}
function isSamePreference(a: ThemePreference, b: ThemePreference) {
if (a.mode !== b.mode) return false;
if (a.mode === "builtin") return a.theme === (b.mode === "builtin" ? b.theme : null);
if (a.mode === "external") return a.themeId === (b.mode === "external" ? b.themeId : null);
if (a.mode === "follow-editor") {
return a.source === (b.mode === "follow-editor" ? b.source : null);
}
return true;
}
function ensureThemeColorMetaTag(): HTMLMetaElement {
let element = document.querySelector<HTMLMetaElement>(DYNAMIC_THEME_COLOR_SELECTOR);
if (element) return element;
@@ -278,6 +288,7 @@ async function loadPreference(preference: ThemePreference, suppressTransitions =
recomputeSnapshot({ preference, status: "loading", message: null });
const theme = await bridge.loadColorTheme(preference.themeId);
if (!isSamePreference(state.preference, preference)) return;
if (!theme) {
setStoredPreference(DEFAULT_THEME_PREFERENCE);
recomputeSnapshot({