diff --git a/frontend/src/lib/lastSession.ts b/frontend/src/lib/lastSession.ts index fc5f5cf279..7e9779ddd3 100644 --- a/frontend/src/lib/lastSession.ts +++ b/frontend/src/lib/lastSession.ts @@ -27,6 +27,18 @@ export function readSessionHint(cookies?: string): string | undefined { // it renders as. Another tab logging in swaps the session under this one, whose user stays // the old one until its next `whoami`: until then, nothing may be read or written as them. let confirmedHint: string | undefined +const confirmationListeners = new Set<() => void>() + +function setConfirmedHint(hint: string | undefined) { + if (hint === confirmedHint) return + confirmedHint = hint + for (const cb of confirmationListeners) cb() +} + +/** Called whenever the session this tab's user is confirmed under changes. */ +export function onSessionConfirmationChange(cb: () => void): void { + confirmationListeners.add(cb) +} /** Whether the session the browser holds is still the one this tab's user was confirmed under. */ export function sessionStillConfirmed(): boolean { @@ -50,7 +62,7 @@ function currentLastUser(): UserExt | undefined { export function optimisticUser(workspace: string): UserExt | undefined { const user = currentLastUser() if (user?.workspace_id !== workspace) return undefined - confirmedHint = readSessionHint() + setConfirmedHint(readSessionHint()) return user } @@ -68,7 +80,7 @@ export function rememberUser(user: UserExt, sentWith: string | undefined): void try { const hint = readSessionHint() // Who answered is only known for the session the request went out with. - confirmedHint = hint === sentWith ? hint : undefined + setConfirmedHint(hint === sentWith ? hint : undefined) if (hint && hint === sentWith) localStorage.setItem(KEY, JSON.stringify({ hint, user } satisfies LastSession)) else localStorage.removeItem(KEY) @@ -76,7 +88,7 @@ export function rememberUser(user: UserExt, sentWith: string | undefined): void } export function forgetLastSession(): void { - confirmedHint = undefined + setConfirmedHint(undefined) try { localStorage.removeItem(KEY) } catch {} diff --git a/frontend/src/lib/persistedCache.svelte.ts b/frontend/src/lib/persistedCache.svelte.ts index ce4480be56..2414a8d74c 100644 --- a/frontend/src/lib/persistedCache.svelte.ts +++ b/frontend/src/lib/persistedCache.svelte.ts @@ -6,7 +6,11 @@ import { OpenAPI } from '$lib/gen' import { userStore } from '$lib/stores' import { userScopedDb } from '$lib/userScopedDb' import { getCurrentUserEmail, onUserChange } from '$lib/userScopedStorage' -import { rememberedEmail, sessionStillConfirmed } from '$lib/lastSession' +import { + onSessionConfirmationChange, + rememberedEmail, + sessionStillConfirmed +} from '$lib/lastSession' // Last-known API responses, kept so a page can paint them before its requests return. Every // row in them was filtered by row-level security for one user, so an entry is only ever served @@ -284,7 +288,9 @@ if (BROWSER) { readSnapshot() // Loads the whole (bounded) store into memory once the user is known, and keeps only what // belongs to them: the snapshot's entries for the same identity survive. - onUserChange(() => { + // The identity is known once both the user and the session they hold are: either can come + // second, and a key derived before must follow. + const onIdentityChange = () => { userTick++ // No user yet (the call at registration, as the app boots): nothing can be read without // an identity, and the snapshot just loaded is waiting for its user. Logout clears memory @@ -294,5 +300,7 @@ if (BROWSER) { const mine = `[${JSON.stringify(identity)},` for (const key of [...memory.keys()]) if (!key.startsWith(mine)) memory.delete(key) void hydrate() - }) + } + onUserChange(onIdentityChange) + onSessionConfirmationChange(onIdentityChange) } diff --git a/frontend/src/lib/persistedResource.svelte.ts b/frontend/src/lib/persistedResource.svelte.ts index ffef3b349e..582687a043 100644 --- a/frontend/src/lib/persistedResource.svelte.ts +++ b/frontend/src/lib/persistedResource.svelte.ts @@ -67,7 +67,11 @@ export function persistedResource( let latest = 0 let inFlight = $state(false) let error = $state(undefined) - const res = resource>(source, async (args) => { + // The identity is a source too: a response is tagged with the keys of its request, and once + // the identity changes no response tagged before matches the keys shown, so without a new + // request nothing would be shown until the arguments next change. + const sourceWithIdentity = () => ({ args: source(), identity: cacheIdentity() }) + const res = resource(sourceWithIdentity, async ({ args }): Promise> => { const id = ++latest inFlight = true try {