/** * The shared current-space store (API §12.2). ONE reactive resolver per Shell, so * useCurrentSpace / useApps / useSpaces.selected all observe the SAME selected * space: a `switch` in any window re-themes every consumer in this window at once, * with a manual optimistic override applied ahead of the round-trip (§1.6). This * closes the same-window cross-hook divergence; cross-WINDOW liveness still waits * on the memory-shape move (§12 flag). */ import type { Shell, SpaceNode } from '@astrale-os/shell' export interface CurrentSpaceSnapshot { readonly space: SpaceNode | null readonly pending: boolean readonly error: unknown } // Stable idles for when the OS plane is unavailable (§12.2) — a fixed snapshot // and a no-op subscription keep `useSyncExternalStore` tear-free without touching // (and re-triggering a fetch on) the real current-space store. export const IDLE_SNAPSHOT: CurrentSpaceSnapshot = { space: null, pending: false, error: undefined } export const idleSnapshot = (): CurrentSpaceSnapshot => IDLE_SNAPSHOT export const noopSubscribe = (): (() => void) => () => {} export interface CurrentSpaceStore { subscribe(cb: () => void): () => void getSnapshot(): CurrentSpaceSnapshot /** Kick a first load if the confirmed value was never fetched. */ ensure(): void /** Refetch the confirmed selected space; resolves when it lands. */ refresh(): Promise /** Apply an optimistic override (a switch in flight) — every consumer re-renders. */ setOptimistic(space: SpaceNode | null): void /** Drop the optimistic override, revealing confirmed truth again. */ clearOptimistic(): void } function createStore(load: () => Promise): CurrentSpaceStore { let confirmed: SpaceNode | null = null let loaded = false let pending = false let error: unknown = undefined let override: { space: SpaceNode | null } | null = null let seq = 0 let snap: CurrentSpaceSnapshot = { space: null, pending: false, error: undefined } const listeners = new Set<() => void>() /** Recompute the projection (override wins) and notify only on a real change. */ function rebuild(): void { const space = override !== null ? override.space : confirmed if (space === snap.space && pending === snap.pending && error === snap.error) return snap = { space, pending, error } for (const l of listeners) l() } function fetch(): Promise { const my = ++seq pending = true rebuild() return load().then( (space) => { if (my !== seq) return confirmed = space loaded = true pending = false error = undefined rebuild() }, (e: unknown) => { if (my !== seq) return pending = false error = e rebuild() }, ) } return { subscribe(cb) { listeners.add(cb) if (!loaded && !pending) void fetch() return () => listeners.delete(cb) }, getSnapshot: () => snap, ensure() { if (!loaded && !pending) void fetch() }, refresh: () => fetch(), setOptimistic(space) { override = { space } rebuild() }, clearOptimistic() { if (override === null) return override = null rebuild() }, } } const stores = new WeakMap() /** The one current-space store for a Shell (created on first touch). */ export function currentSpaceStore(shell: Shell): CurrentSpaceStore { let s = stores.get(shell) if (s === undefined) { s = createStore(() => shell.user.currentSpace()) stores.set(shell, s) } return s }