{"version":3,"file":"debug-channel.mjs","sources":["../src/debug-channel.ts"],"sourcesContent":["/**\n * Debug channel — the order-independent handshake between the SDK core and the\n * optional `@colyseus/sdk/debug` overlay.\n *\n * Publishers (a joined room in {@link Client}, a `Predict` instance) call\n * {@link publishDebug} whenever there's something the overlay could surface. If\n * the overlay is already loaded it renders immediately; if it loads LATER — the\n * usual dev-only `import(\"@colyseus/sdk/debug\")` races the first room join —\n * {@link setDebugReceiver} REPLAYS what was buffered. No app-side ordering\n * (awaiting the import) is required, and a publisher that fires before the\n * overlay loads is never dropped.\n *\n * Module-private on purpose: rooms are reachable only by importing the overlay.\n * Nothing here may read or write a global — a page script could then read a\n * buffered room, or plant its own receiver and be handed every join.\n *\n * Production (overlay never imported) stays effectively free: the buffer holds\n * WeakRefs so nothing is pinned, and is bounded — an app that never loads the\n * overlay leaks neither memory nor room references.\n *\n * The script-tag builds share no module graph, so rollup swaps this file for\n * `debug-channel.umd.ts` there.\n */\n\nexport type DebugReceiver = (channel: string, handle: object) => void;\n\ninterface WeakRefLike { deref(): object | undefined; }\n\n// Old embedded runtimes (some Cocos/Construct targets) lack WeakRef; fall back to\n// a strong ref there — still bounded, so the worst case is a few pinned handles in\n// a debug-less session, never an unbounded leak.\nconst makeRef: (h: object) => WeakRefLike =\n    typeof WeakRef !== \"undefined\" ? (h) => new WeakRef(h) : (h) => ({ deref: () => h });\n\n// A handful of rooms/Predict handles is plenty of replay depth; the bound keeps a\n// debug-less prod session from growing the buffer without limit.\nconst MAX_BUFFERED = 64;\n\nconst buffer: Array<[string, WeakRefLike]> = [];\nlet receiver: DebugReceiver | undefined;\n\n/**\n * Publish a `handle` to the overlay `channel` (`\"room\"` | `\"predict\"` | …).\n * Prod fast path: a live call (overlay loaded) or a bounded WeakRef append\n * (buffered for replay on install).\n */\nexport function publishDebug(channel: string, handle: object): void {\n    if (receiver) {\n        receiver(channel, handle);\n        return;\n    }\n    buffer.push([channel, makeRef(handle)]);\n    if (buffer.length > MAX_BUFFERED) { buffer.shift(); }\n}\n\n/**\n * Connect the overlay and replay everything buffered before it loaded. `undefined`\n * disconnects it again.\n */\nexport function setDebugReceiver(next: DebugReceiver | undefined): void {\n    receiver = next;\n    if (!next) { return; }\n    for (const [channel, ref] of buffer.splice(0)) {\n        const handle = ref.deref();\n        if (handle) { next(channel, handle); }\n    }\n}\n\n/**\n * True only when the overlay is connected. Diagnostics that must stay free in\n * production (reconciler drift bookkeeping) gate on this.\n */\nexport function debugOverlayActive(): boolean {\n    return receiver !== undefined;\n}\n"],"names":[],"mappings":";;;;;;AAAA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AAMH;AACA;AACA;AACA,MAAM,OAAO,GACT,OAAO,OAAO,KAAK,WAAW,GAAG,CAAC,CAAC,KAAK,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC;AAExF;AACA;AACA,MAAM,YAAY,GAAG,EAAE;AAEvB,MAAM,MAAM,GAAiC,EAAE;AAC/C,IAAI,QAAmC;AAEvC;;;;AAIG;AACG,SAAU,YAAY,CAAC,OAAe,EAAE,MAAc,EAAA;IACxD,IAAI,QAAQ,EAAE;AACV,QAAA,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QACzB;IACJ;AACA,IAAA,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;AACvC,IAAA,IAAI,MAAM,CAAC,MAAM,GAAG,YAAY,EAAE;QAAE,MAAM,CAAC,KAAK,EAAE;IAAE;AACxD;AAEA;;;AAGG;AACG,SAAU,gBAAgB,CAAC,IAA+B,EAAA;IAC5D,QAAQ,GAAG,IAAI;IACf,IAAI,CAAC,IAAI,EAAE;QAAE;IAAQ;AACrB,IAAA,KAAK,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE;AAC3C,QAAA,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,EAAE;QAC1B,IAAI,MAAM,EAAE;AAAE,YAAA,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC;QAAE;IACzC;AACJ;AAEA;;;AAGG;SACa,kBAAkB,GAAA;IAC9B,OAAO,QAAQ,KAAK,SAAS;AACjC;;;;"}