Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions apps/web/src/hooks/useSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
persistClientSettingsPatch,
persistClientSettingsUpdate,
prepareModelPreferencesPatch,
subscribeClientSettings,
subscribeClientSettingsHydration,
} from "./useSettings";
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";

Expand All @@ -42,6 +44,31 @@ describe("client settings hydration", () => {
const onboardingCompletedAt = "2026-09-05T12:00:00.000Z";
const complete = (current: ClientSettings) => ({ ...current, onboardingCompletedAt });

it.each([
["settings", subscribeClientSettings],
["hydration", subscribeClientSettingsHydration],
] as const)("visits a re-subscribing %s listener once per update", async (_, subscribe) => {
persistenceMocks.getClientSettings.mockResolvedValueOnce(savedSettings);
let unsubscribe = () => {};
let calls = 0;

const register = () =>
subscribe(() => {
unsubscribe();
// Bound the test on the old implementation, which would otherwise loop forever.
if (++calls === 1) unsubscribe = register();
});

try {
unsubscribe = register();
await ensureClientSettingsHydrated();
expect(calls).toBe(1);
expect(getClientSettings()).toEqual(savedSettings);
} finally {
unsubscribe();
}
});

it("waits for saved model preferences before applying the first server edit", async () => {
const provider = ProviderInstanceId.make("codex_work");
let finishRead!: (settings: ClientSettings) => void;
Expand Down
10 changes: 6 additions & 4 deletions apps/web/src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ let deferredClientSettingsPatchCount = 0;
let unsubscribeOtherTabClientSettings: (() => void) | null = null;

function emitClientSettingsChange() {
for (const listener of clientSettingsListeners) {
listener();
// Refreshing an atom can synchronously replace its subscription.
// Visit only listeners registered when this notification began.
for (const listener of [...clientSettingsListeners]) {
if (clientSettingsListeners.has(listener)) listener();
}
}

function emitClientSettingsHydrationChange() {
for (const listener of clientSettingsHydrationListeners) {
listener();
for (const listener of [...clientSettingsHydrationListeners]) {
if (clientSettingsHydrationListeners.has(listener)) listener();
}
}

Expand Down
Loading