You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.1 KiB

/**
* Svelte context bridge for the Nexo runtime: the `DatingAppHandle`
* (App + API) plus a session cell owned by the shell layout. Nested
* pages and components consume both via `getNexoContext()` instead of
* each rebuilding their own.
*
* Symbol-keyed (instead of `setContext('app', ...)`) so it can't
* collide with whatever else the surrounding SvelteKit app registers.
*
* `session` is exposed as a getter — pages call `nexo.session()` so
* `$derived(...)` and `$effect(...)` track session-aware UI without
* each page re-issuing its own `/api/session` probe.
*/
import { getContext, setContext } from 'svelte';
import type { DatingAppHandle } from './app.ts';
import type { DatingRealtimeHandle } from './realtime.ts';
import type { DatingSessionResponse } from './types.ts';
export interface NexoContext extends DatingAppHandle {
/** Current session snapshot. `null` while the initial probe is in flight. */
session(): DatingSessionResponse | null;
/** Replace the session cell after a login / logout / refresh. */
setSession(next: DatingSessionResponse | null): void;
/** Re-issue `/api/session`; layout uses it on mount, pages on demand. */
refreshSession(): Promise<DatingSessionResponse | null>;
/**
* Shared WebSocket subscription to `/api/realtime`. Pages call
* `realtime().subscribe(...)` to react to server-pushed events
* (new chat messages, match updates, …). The layout owns the
* single underlying connection.
*/
realtime(): DatingRealtimeHandle;
}
const NEXO_CONTEXT_KEY = Symbol('nexo.context');
export function setNexoContext(value: NexoContext): NexoContext {
return setContext(NEXO_CONTEXT_KEY, value);
}
export function getNexoContext(): NexoContext {
const value = getContext<NexoContext | undefined>(NEXO_CONTEXT_KEY);
if (value === undefined) {
throw new Error('Nexo context is not set. Did you mount /dating without its layout?');
}
return value;
}
// Back-compat aliases — pages that only need the App handle keep the
// terser names. New code uses `getNexoContext()` for the session
// surface too.
export const setNexoApp = setNexoContext;
export const getNexoApp = getNexoContext;

Powered by TurnKey Linux.