typebulb 0.57.1 → 0.57.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/render.js CHANGED
@@ -1446,7 +1446,20 @@ ${hp("tbStreamUrl")}
1446
1446
  } catch (err) { location.replace('about:blank'); }
1447
1447
  }, 250);
1448
1448
  });
1449
- tbOn('message', async (e) => {
1449
+ // The stream attaches during this script's own eval, in <head> \u2014 before the bulb's module (and
1450
+ // its import graph) have run, so before a single \`tb.onMessage\` handler exists. The server now
1451
+ // delivers the instant the stream attaches (it holds a send for the page it opened), so a
1452
+ // message can beat the page's own code by the width of a CDN fetch. A page that is attached but
1453
+ // has not yet run is not a page with no handlers, so hold the message until the deferred module
1454
+ // scripts have executed (DOMContentLoaded waits for them). Page-side and bounded by one load:
1455
+ // the server still never buffers, and a page that is awake handles inline as before.
1456
+ let pageAwake = document.readyState !== 'loading';
1457
+ const beforeAwake = [];
1458
+ if (!pageAwake) document.addEventListener('DOMContentLoaded', () => {
1459
+ pageAwake = true;
1460
+ for (const run of beforeAwake.splice(0)) run();
1461
+ });
1462
+ const onMessageEvent = async (e) => {
1450
1463
  // Wire envelope { id?, payload } (JSON \u2014 SSE-line-safe). tb:* payloads are the shim's reserved
1451
1464
  // namespace (TB-Interrogation.md): answered by a built-in handler, never delivered to
1452
1465
  // tb.onMessage. Each settled non-undefined return is a reply candidate \u2014 the CLI enforces the
@@ -1488,6 +1501,10 @@ ${hp("tbStreamUrl")}
1488
1501
  if (env.id !== undefined) {
1489
1502
  try { fetch('/__send-reply', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: env.id, results, errors }) }); } catch {}
1490
1503
  }
1504
+ };
1505
+ tbOn('message', (e) => {
1506
+ if (pageAwake) { void onMessageEvent(e); return; }
1507
+ beforeAwake.push(() => { void onMessageEvent(e); });
1491
1508
  });
1492
1509
  }
1493
1510
  })();