// Reload boundary: state that must survive a /reload inside one parent // process. Extension modules re-execute on /reload, so any collaborator whose // lifetime spans sessions is kept behind a process-global keyed by a stable // symbol; a fresh module instance revives the same state instead of replacing // it. Per-load state stays in ordinary module bindings. See CONTEXT.md // "Supported reload boundary". /** * Retain state across /reload: revive the retained value for this key, or * create it once and retain it for every later module instance. */ export function retainAcrossReload( key: symbol, create: () => T, ): T { const scope = globalThis as typeof globalThis & { [key: symbol]: unknown }; const existing = scope[key]; if (existing !== undefined) return existing as T; const created = create(); scope[key] = created; return created; }