/**
* Per-tab session state the views share: the active identity (set by boot)
* and the selfroom handle once its heavy stack finished loading in the
* background. Views must tolerate selfroom === null (first seconds of boot,
* or a mailbox-less offline session).
*/
import type { AudienceService } from "./audience";
import type { CallService } from "./calls";
import type { ContactsService } from "./contacts";
import type { ChatService } from "./conversations";
import type { HomeLang } from "./home-log";
import type { ActiveIdentity } from "./identity-store";
import type { SelfRoom } from "./selfroom";
export const Session: {
identity: ActiveIdentity | null;
selfroom: SelfRoom | null;
/** Contact/group service over the selfroom; non-null once selfroom is up. */
contacts: ContactsService | null;
/** Conversation registry + chat rooms (M3); non-null once selfroom is up. */
chat: ChatService | null;
/** Audience-restriction keys/grants (M5b); non-null once selfroom is up. */
audience: AudienceService | null;
/** 1:1 audio/video calls (P4); non-null once selfroom is up. */
calls: CallService | null;
/**
* Lang chosen before the selfroom was up, stamped with the CLICK-time ts —
* LWW must order the intent at the moment the user acted, not at the later
* flush time. Flushed (and awaited) by main.ts when the room starts.
*/
pendingLang: { lang: HomeLang; ts: number } | null;
} = {
identity: null,
selfroom: null,
contacts: null,
chat: null,
audience: null,
calls: null,
pendingLang: null,
};
/**
* Record a user-made language change into the home log (settings op) so it
* reaches the user's other devices. i18n's setLang() is the caller's job —
* this only handles the replication side, and buffers when the room isn't
* up yet.
*/
export function recordLangChange(lang: HomeLang): void {
if (Session.selfroom) {
Session.pendingLang = null;
void Session.selfroom.setSettings({ lang }).catch(() => {});
} else {
Session.pendingLang = { lang, ts: Date.now() };
}
}