Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Window prefs: no import cycle through the preference modules

milind-sonib1638c3aaf65
diff --git a/src/lib/chat-width.ts b/src/lib/chat-width.ts
index e63af945c4..bb76a1c72d 100644
--- a/src/lib/chat-width.ts
+++ b/src/lib/chat-width.ts
@@ -1,5 +1,5 @@
import { CHAT_WIDTH_IDS } from "../../shared/app-settings";
-import { reportWindowPrefs } from "./window-prefs";
+import { windowPrefChanged } from "./window-pref-changes";
export { CHAT_WIDTH_IDS };
export type ChatWidthId = (typeof CHAT_WIDTH_IDS)[number];
@@ -37,5 +37,5 @@ export function applyChatWidth(id: ChatWidthId): void {
} catch {
// Storage can be full or blocked in private mode; the width still applies this session.
}
- reportWindowPrefs();
+ windowPrefChanged();
}
diff --git a/src/lib/fonts.ts b/src/lib/fonts.ts
index faa70f0fe9..1bb0ec1af2 100644
--- a/src/lib/fonts.ts
+++ b/src/lib/fonts.ts
@@ -1,5 +1,5 @@
import { FONT_IDS } from "../../shared/app-settings";
-import { reportWindowPrefs } from "./window-prefs";
+import { windowPrefChanged } from "./window-pref-changes";
export { FONT_IDS };
export type FontId = (typeof FONT_IDS)[number];
@@ -36,5 +36,5 @@ export function applyFont(id: FontId): void {
getStore()?.setItem(FONT_KEY, id);
} catch {
}
- reportWindowPrefs();
+ windowPrefChanged();
}
diff --git a/src/lib/interface-mode.ts b/src/lib/interface-mode.ts
index 275946035c..186322d642 100644
--- a/src/lib/interface-mode.ts
+++ b/src/lib/interface-mode.ts
@@ -2,7 +2,7 @@ import { useSyncExternalStore } from "react";
import { hasPriorRun } from "./prior-run";
import { SIMPLE_ONLY } from "./simple-only";
-import { reportWindowPrefs } from "./window-prefs";
+import { windowPrefChanged } from "./window-pref-changes";
export const ADVANCED_MODE_KEY = "omb-advanced-mode";
@@ -70,7 +70,7 @@ export function setAdvancedMode(enabled: boolean): void {
} catch {
}
notify();
- reportWindowPrefs();
+ windowPrefChanged();
}
export function useAdvancedMode(): boolean {
diff --git a/src/lib/notification-preferences.ts b/src/lib/notification-preferences.ts
index 76011300e3..032434a37f 100644
--- a/src/lib/notification-preferences.ts
+++ b/src/lib/notification-preferences.ts
@@ -1,5 +1,5 @@
import { useSyncExternalStore } from "react";
-import { reportWindowPrefs } from "./window-prefs";
+import { windowPrefChanged } from "./window-pref-changes";
export const NOTIFICATION_SOUNDS_KEY = "omb-notification-sounds";
export const NOTIFICATION_FEED_KEY = "omb-notification-feed";
@@ -64,7 +64,7 @@ export function setNotificationSounds(enabled: boolean): void {
} catch {
}
notify();
- reportWindowPrefs();
+ windowPrefChanged();
}
export function useNotificationSounds(): boolean {
diff --git a/src/lib/send-key.ts b/src/lib/send-key.ts
index 5f332b2b4b..8a590d5531 100644
--- a/src/lib/send-key.ts
+++ b/src/lib/send-key.ts
@@ -3,7 +3,7 @@ import { useSyncExternalStore } from "react";
import { SEND_KEYS } from "../../shared/app-settings";
import { activeLocale } from "./i18n";
import { isMacPlatform } from "./keyboard-shortcuts";
-import { reportWindowPrefs } from "./window-prefs";
+import { windowPrefChanged } from "./window-pref-changes";
export const SEND_KEY_KEY = "omb-send-key";
@@ -87,7 +87,7 @@ export function setSendKey(mode: SendKey): void {
} catch {
}
notify();
- reportWindowPrefs();
+ windowPrefChanged();
}
export function useSendKey(): SendKey {
diff --git a/src/lib/skins.ts b/src/lib/skins.ts
index be0494d05e..579e3df062 100644
--- a/src/lib/skins.ts
+++ b/src/lib/skins.ts
@@ -1,7 +1,7 @@
import { SKIN_IDS } from "../../shared/app-settings";
import { paintAuroraField } from "./aurora-field";
import { hasPriorRun } from "./prior-run";
-import { reportWindowPrefs } from "./window-prefs";
+import { windowPrefChanged } from "./window-pref-changes";
export { SKIN_IDS };
export type SkinId = (typeof SKIN_IDS)[number];
@@ -134,7 +134,7 @@ export function applySkinChoice(choice: SkinChoice): void {
}
followSystem(choice === "system");
paintSkin(resolveSkin(choice));
- reportWindowPrefs();
+ windowPrefChanged();
}
export function applySkin(id: SkinId): void {
diff --git a/src/lib/window-pref-changes.ts b/src/lib/window-pref-changes.ts
new file mode 100644
index 0000000000..a7e81a481e
--- /dev/null
+++ b/src/lib/window-pref-changes.ts
@@ -0,0 +1,17 @@
+// Imports nothing, so a preference's own module can say it changed without loading every other preference module.
+let listener: (() => void) | null = null;
+let missed = false;
+
+export function windowPrefChanged(): void {
+ if (listener) listener();
+ else missed = true;
+}
+
+/** A change said before the listener came is heard once it comes. */
+export function onWindowPrefChanged(next: () => void): void {
+ listener = next;
+ if (missed) {
+ missed = false;
+ next();
+ }
+}
diff --git a/src/lib/window-prefs.ts b/src/lib/window-prefs.ts
index 1952c2fc65..c634ad824a 100644
--- a/src/lib/window-prefs.ts
+++ b/src/lib/window-prefs.ts
@@ -20,6 +20,7 @@ import { setShowThreads, showThreads } from "./thread-preferences";
import { readColorUserBubbles, setColorUserBubbles } from "./user-bubble-preference";
import { latestWebPanels, setWebPanels } from "./web-panel-preferences";
import { newWebPanelId, validateWebPanel, type WebPanel } from "./web-panels";
+import { onWindowPrefChanged } from "./window-pref-changes";
type Pref<I extends WindowPrefId> = { read(): unknown; write(value: WindowPrefValue<I>): unknown };
const ogb = () => (typeof window === "undefined" ? undefined : window.ogb);
@@ -36,12 +37,12 @@ const route = (name: CompanionRoute): { read(): unknown; write(on: boolean): unk
// Called, never referenced at load: many tests mock these modules with only the exports they use.
const PREFS: { [I in WindowPrefId]: Pref<I> } = {
- "appearance.skin": { read: readSkinChoice, write: applySkinChoice },
- "appearance.advanced": { read: readAdvancedMode, write: setAdvancedMode },
- "appearance.font": { read: readFont, write: applyFont },
- "appearance.chatWidth": { read: readChatWidth, write: applyChatWidth },
- "general.sendKey": { read: readSendKey, write: setSendKey },
- "general.notificationSounds": { read: notificationSoundsEnabled, write: setNotificationSounds },
+ "appearance.skin": { read: () => readSkinChoice(), write: (choice) => applySkinChoice(choice) },
+ "appearance.advanced": { read: () => readAdvancedMode(), write: (on) => setAdvancedMode(on) },
+ "appearance.font": { read: () => readFont(), write: (font) => applyFont(font) },
+ "appearance.chatWidth": { read: () => readChatWidth(), write: (width) => applyChatWidth(width) },
+ "general.sendKey": { read: () => readSendKey(), write: (key) => setSendKey(key) },
+ "general.notificationSounds": { read: () => notificationSoundsEnabled(), write: (on) => setNotificationSounds(on) },
"appearance.userBubbles": { read: () => readColorUserBubbles(), write: (on) => setColorUserBubbles(on) },
"appearance.runCard": { read: () => showRunCard(), write: (on) => setShowRunCard(on) },
"appearance.showThreads": { read: () => showThreads(), write: (on) => setShowThreads(on) },
@@ -121,3 +122,5 @@ export function reportWindowPrefs(): void {
.catch(() => undefined);
}, 200);
}
+
+onWindowPrefChanged(reportWindowPrefs);