fix: persist master switch before reloading tabs

This commit is contained in:
SethBurkart123 committed 2026-10-06 11:58:06 +11:00
1 parent d97cc86ab3
commit 5928414a1e
3 files changed
+48 -19

No files matched your search

@@ -0,0 +1,39 @@
/** @jest-environment jsdom */
describe("master extension setting", () => {
afterEach(() => jest.useRealTimers());
it.each([false, true])("persists %s and queued preferences before reloading SEQTA tabs", async (enabled) => {
jest.useFakeTimers();
let browser: any;
let settings: typeof import("./SettingsState");
jest.isolateModules(() => {
browser = require("webextension-polyfill").default;
settings = require("./SettingsState");
});
await browser.storage.local.set({ onoff: !enabled });
await settings!.initializeSettingsState();
browser.runtime.sendMessage.mockClear();
const originalSet = browser.storage.local.set.getMockImplementation();
let finishWrite: () => Promise<void> = async () => {};
browser.storage.local.set.mockImplementationOnce((patch: Record<string, unknown>) => new Promise<void>((resolve) => {
finishWrite = async () => {
await originalSet(patch);
resolve();
};
}));
browser.runtime.sendMessage.mockImplementation(async () => {
expect(await browser.storage.local.get(null)).toMatchObject({ onoff: enabled, selectedFont: "inter" });
});
settings!.settingsState.selectedFont = "inter";
settings!.settingsState.onoff = enabled;
jest.advanceTimersByTime(300);
expect(browser.runtime.sendMessage).not.toHaveBeenCalled();
await finishWrite();
await Promise.resolve();
await Promise.resolve();
expect(browser.runtime.sendMessage).toHaveBeenCalledWith({ type: "reloadTabs" });
});
});
+9 -12
View File
@@ -107,18 +107,7 @@ class StorageManager {
return Reflect.get(target.data, prop);
},
set: (target, prop: keyof SettingsState, value) => {
if (typeof prop === "string" && isExcludedSettingsKey(prop)) {
void browser.storage.local.set({ [prop]: value });
return true;
}
const oldValue = target.data[prop];
// Only save if the reference actually changed
if (oldValue !== value) {
Reflect.set(target.data, prop, value);
void target.saveToStorage([prop as string]);
target.notifySettingChange(prop as string, value, oldValue);
}
target.setKey(prop, value);
return true;
},
deleteProperty: (target, prop: keyof SettingsState) => {
@@ -186,6 +175,13 @@ class StorageManager {
this.data[key] = value;
void this.saveToStorage([key as string]);
this.notifySettingChange(key as string, value, oldValue);
if (key === "onoff" && typeof value === "boolean") {
// Reload only after this switch and any queued preferences reach storage.
// This also covers Settings opened while the extension is disabled.
void this.flushPendingPatch()
.then(() => browser.runtime.sendMessage({ type: "reloadTabs" }))
.catch((error) => console.error("[BetterSEQTA+] Failed to save master setting", error));
}
}
}
@@ -247,6 +243,7 @@ class StorageManager {
}
private async flushPendingPatch(): Promise<void> {
if (this.saveTimeout) clearTimeout(this.saveTimeout);
this.saveTimeout = null;
if (this.bootstrapping || this.suppressWrites) return;
@@ -10,7 +10,6 @@ import { applyMenuItemVisibility, scheduleMenuItemVisibility } from "@/seqta/uti
import { ChangeMenuItemPositions } from "@/seqta/utils/Openers/menuOrder";
import { syncPerformanceModeEffects } from "@/seqta/utils/performanceMode";
import browser from "webextension-polyfill";
import type { CustomShortcut } from "@/types/storage";
export class StorageChangeHandler {
@@ -27,7 +26,6 @@ export class StorageChangeHandler {
);
settingsState.register("selectedTheme", () => void updateAllColors());
settingsState.register("DarkMode", this.handleDarkModeChange.bind(this));
settingsState.register("onoff", this.handleOnOffChange.bind(this));
settingsState.register("shortcuts", this.handleShortcutsChange.bind(this));
settingsState.register(
"customshortcuts",
@@ -72,11 +70,6 @@ export class StorageChangeHandler {
void updateAllColors();
}
private handleOnOffChange(newValue: boolean) {
if (newValue) return;
browser.runtime.sendMessage({ type: "reloadTabs" });
}
private handleCustomShortcutsChange(newValue: CustomShortcut[] | undefined) {
if (!Array.isArray(newValue)) return;
renderShortcuts();