@valbuild/tanstack 0.150.23 → 0.152.0

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.
Files changed (32) hide show
  1. package/CHANGELOG.md +199 -0
  2. package/README.md +11 -11
  3. package/client/dist/valbuild-tanstack-client.cjs.dev.js +100 -293
  4. package/client/dist/valbuild-tanstack-client.cjs.prod.js +100 -293
  5. package/client/dist/valbuild-tanstack-client.esm.js +99 -292
  6. package/dist/ValOverlayContext-1e50a557.cjs.prod.js +47 -0
  7. package/dist/ValOverlayContext-6826b4d9.esm.js +40 -0
  8. package/dist/ValOverlayContext-b18c2f18.cjs.dev.js +47 -0
  9. package/dist/declarations/src/ValDraft.d.ts +6 -50
  10. package/dist/declarations/src/ValModulesClient.d.ts +1 -3
  11. package/dist/declarations/src/ValProvider.d.ts +1 -1
  12. package/dist/declarations/src/client/initValClient.d.ts +25 -3
  13. package/dist/declarations/src/server/initValContent.d.ts +55 -169
  14. package/dist/{routeFromVal-06590f50.esm.js → routeFromVal-124235a5.esm.js} +3 -153
  15. package/dist/{routeFromVal-3c3915e2.cjs.dev.js → routeFromVal-2e834397.cjs.prod.js} +1 -156
  16. package/dist/{routeFromVal-b31a9e00.cjs.prod.js → routeFromVal-3d4d7c30.cjs.dev.js} +1 -156
  17. package/dist/{typeof-66e2d869.esm.js → typeof-3e1a1d11.esm.js} +1 -1
  18. package/dist/{typeof-8998e126.cjs.prod.js → typeof-56656438.cjs.prod.js} +0 -2
  19. package/dist/{typeof-dd0246a1.cjs.dev.js → typeof-bd6ec82c.cjs.dev.js} +0 -2
  20. package/dist/valbuild-tanstack.cjs.dev.js +162 -282
  21. package/dist/valbuild-tanstack.cjs.prod.js +162 -282
  22. package/dist/valbuild-tanstack.esm.js +162 -281
  23. package/dist/{version-30535975.cjs.prod.js → version-96339a5a.cjs.prod.js} +1 -19
  24. package/dist/{version-278a8d18.cjs.dev.js → version-c27a565d.cjs.dev.js} +1 -19
  25. package/dist/{version-3d755ee7.esm.js → version-f05a8296.esm.js} +2 -19
  26. package/package.json +8 -8
  27. package/server/dist/valbuild-tanstack-server.cjs.dev.js +278 -1140
  28. package/server/dist/valbuild-tanstack-server.cjs.prod.js +278 -1140
  29. package/server/dist/valbuild-tanstack-server.esm.js +279 -1141
  30. package/dist/ValOverlayContext-198b4bd1.cjs.dev.js +0 -301
  31. package/dist/ValOverlayContext-6411bbe3.esm.js +0 -293
  32. package/dist/ValOverlayContext-d9b22d1a.cjs.prod.js +0 -301
@@ -3,19 +3,18 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var asyncToGenerator = require('./asyncToGenerator-8e5c36c8.cjs.prod.js');
6
- var _typeof = require('./typeof-8998e126.cjs.prod.js');
6
+ var _typeof = require('./typeof-56656438.cjs.prod.js');
7
7
  var core = require('@valbuild/core');
8
8
  var stega = require('@valbuild/react/stega');
9
- var version = require('./version-30535975.cjs.prod.js');
9
+ var version = require('./version-96339a5a.cjs.prod.js');
10
10
  var internal = require('@valbuild/react/internal');
11
11
  var ui = require('@valbuild/ui');
12
12
  var reactRouter = require('@tanstack/react-router');
13
13
  var React = require('react');
14
- var ValOverlayContext = require('./ValOverlayContext-d9b22d1a.cjs.prod.js');
14
+ var ValOverlayContext = require('./ValOverlayContext-1e50a557.cjs.prod.js');
15
15
  var client = require('@valbuild/shared/client');
16
16
  var jsxRuntime = require('react/jsx-runtime');
17
17
  var demoServiceWorker = require('./demoServiceWorker-994710f7.cjs.prod.js');
18
- require('@valbuild/core/patch');
19
18
 
20
19
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
21
20
 
@@ -234,6 +233,22 @@ var floatDarkBg = "#131316";
234
233
  /** `--bg-float`, light. */
235
234
  var floatLightBg = "#fcfcfc";
236
235
 
236
+ function _arrayWithoutHoles(r) {
237
+ if (Array.isArray(r)) return _typeof._arrayLikeToArray(r);
238
+ }
239
+
240
+ function _iterableToArray(r) {
241
+ if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
242
+ }
243
+
244
+ function _nonIterableSpread() {
245
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
246
+ }
247
+
248
+ function _toConsumableArray(r) {
249
+ return _arrayWithoutHoles(r) || _iterableToArray(r) || _typeof._unsupportedIterableToArray(r) || _nonIterableSpread();
250
+ }
251
+
237
252
  var DEFAULT_SELECTION = {
238
253
  selection: client.DEFAULT_CANVAS_SELECTION,
239
254
  selectionSoft: client.DEFAULT_CANVAS_SELECTION_SOFT
@@ -262,7 +277,9 @@ var RESCAN_INTERVAL_MS = 5000;
262
277
  function ValCanvasBridge(_ref) {
263
278
  var _elementPicking$modif;
264
279
  var draftMode = _ref.draftMode,
265
- isRefreshing = _ref.isRefreshing;
280
+ isRefreshing = _ref.isRefreshing,
281
+ holds = _ref.holds,
282
+ holdsVersions = _ref.holdsVersions;
266
283
  var _React$useState = React__default["default"].useState(false),
267
284
  _React$useState2 = _typeof._slicedToArray(_React$useState, 2),
268
285
  picking = _React$useState2[0],
@@ -405,13 +422,17 @@ function ValCanvasBridge(_ref) {
405
422
 
406
423
  // Announce the frame, and say whether it is actually showing draft content.
407
424
  React__default["default"].useEffect(function () {
408
- post({
425
+ post(_typeof._objectSpread2(_typeof._objectSpread2({
409
426
  val: client.VAL_CANVAS_MESSAGE,
410
427
  type: "ready",
411
428
  draftMode: draftMode,
412
429
  url: window.location.href
413
- });
414
- }, [post, draftMode]);
430
+ }, holds !== undefined && holds.length > 0 ? {
431
+ holds: _toConsumableArray(holds)
432
+ } : {}), holdsVersions !== undefined && Object.keys(holdsVersions).length > 0 ? {
433
+ holdsVersions: holdsVersions
434
+ } : {}));
435
+ }, [post, draftMode, holds, holdsVersions]);
415
436
 
416
437
  /**
417
438
  * Re-scan whenever the page could have moved.
@@ -659,16 +680,17 @@ function ValCanvasBridge(_ref) {
659
680
  * `ValTanStackProvider` listens for this event on its own window to make an
660
681
  * inline edit visible behind the overlay; the only thing different here
661
682
  * is that the edit was made in another window. Re-dispatching rather
662
- * than reaching into the store keeps one path into the page — including
663
- * the `router.invalidate()` it throttles behind it, which is what brings a
664
- * server component's own re-read across.
683
+ * than reaching into the store keeps one path into the page. (What
684
+ * re-reads the server's own content is `confirmed`, below.)
665
685
  */
666
686
  window.dispatchEvent(new CustomEvent("val-event", {
667
- detail: {
687
+ detail: _typeof._objectSpread2({
668
688
  type: "source-update",
669
689
  moduleFilePath: message.moduleFilePath,
670
690
  source: message.source
671
- }
691
+ }, message.read !== undefined ? {
692
+ read: message.read
693
+ } : {})
672
694
  }));
673
695
  return;
674
696
  }
@@ -682,6 +704,17 @@ function ValCanvasBridge(_ref) {
682
704
  }));
683
705
  return;
684
706
  }
707
+ if (message.type === "confirmed") {
708
+ // How far the server holds the editor's changes: what the provider
709
+ // re-reads its server-rendered content on. Relayed like the two above.
710
+ window.dispatchEvent(new CustomEvent("val-event", {
711
+ detail: {
712
+ type: "confirmed",
713
+ seq: message.seq
714
+ }
715
+ }));
716
+ return;
717
+ }
685
718
  setHighlighted(message.path);
686
719
  if (message.path !== null && message.scrollIntoView) {
687
720
  var target = findByPath(message.path);
@@ -1109,52 +1142,10 @@ function readPageReply(message) {
1109
1142
  }
1110
1143
 
1111
1144
  /**
1112
- * Whether the page should re-read the server on its own, right now.
1113
- *
1114
- * The page cannot know when its own edit has been persisted. An edit is applied
1115
- * to the client store immediately and written to the server asynchronously, so
1116
- * a `router.invalidate()` fired on the edit can reach a server that has not saved
1117
- * the patch yet: the payload comes back with the old content, and nothing
1118
- * schedules another look. What an editor sees is a canvas that flickered and did
1119
- * not change — which is indistinguishable from an edit that did not work.
1120
- *
1121
- * So the page asks again a little later. This is the "should I bother" half of
1122
- * that, kept out of the effect and pure so the reasons NOT to are checkable:
1123
- * every one of them is a whole-route request avoided, and in development Next
1124
- * re-renders (and sometimes recompiles) the page for each one.
1125
- */
1126
- function shouldSafetyRefresh(state) {
1127
- // Never edited: a preview someone is only looking at has nothing to catch up
1128
- // on, and polling it would cost a route render per tick for nothing.
1129
- if (state.lastEditAt === 0) return false;
1130
- // The editing stopped long enough ago that anything in flight has landed.
1131
- if (state.now - state.lastEditAt > state.windowMs) return false;
1132
- // Nobody is looking. The caller refreshes once on the way back instead.
1133
- if (state.hidden) return false;
1134
- // Already on its way. Stacking whole-route requests makes the slowness this
1135
- // exists to paper over worse, not better.
1136
- if (state.isRefreshing) return false;
1137
- // One just went out — usually the edit-driven refresh, a moment ago.
1138
- if (state.now - state.lastRefreshAt < state.minIntervalMs) return false;
1139
- return true;
1140
- }
1141
-
1142
- /**
1143
- * What a draft page shows, read by the server for the request that renders it.
1144
- *
1145
- * The source of every module the editor's draft CHANGES -- the ones with
1146
- * patches applied, published-not-yet-built ones included -- and nothing
1147
- * else: a module without changes is the build's own source, which the page
1148
- * already has. Handed to `<ValProvider draft>` so the server's render and the
1149
- * browser's first render resolve the same text, and the page never shows the
1150
- * published version first and the draft a moment later.
1151
- *
1152
- * A `.jsonValues()` module is in the shape the Studio's own store gives it: the
1153
- * entries the draft edits hold their content, and every other entry is the
1154
- * thunkless marker the hooks resolve from the bundle.
1155
- *
1156
- * Plain JSON, because it travels from a server function to the browser in the
1157
- * page's loader data. `fetchValDraft` in `@valbuild/tanstack/server` reads it.
1145
+ * What a draft page shows, read by the server for the request that renders
1146
+ * it: see `ValDraft` in `@valbuild/react` (its `read`), where it moved when
1147
+ * `@valbuild/next`'s `ValProvider` took a `draft` too. Exported from here as
1148
+ * it always was.
1158
1149
  */
1159
1150
 
1160
1151
  /**
@@ -1306,51 +1297,6 @@ function PlatformAnalytics(_ref) {
1306
1297
  });
1307
1298
  }
1308
1299
 
1309
- var REFRESH_INTERVAL_MS = 500;
1310
-
1311
- /**
1312
- * How often the page re-reads the server on its own, without being told to.
1313
- *
1314
- * A safety net for a race no amount of care on this side can close. An edit is
1315
- * applied to the client store immediately and saved to the server
1316
- * asynchronously, so `router.invalidate()` fired on the edit can reach a server
1317
- * that has not written the patch yet: the loaders come back with the old
1318
- * content, the counter has already been cleared, and nothing schedules another
1319
- * look. What you see is a canvas that flickered and did not change — which is
1320
- * indistinguishable from an edit that did not work.
1321
- *
1322
- * The honest fix would be for the page to know when its patch has been
1323
- * persisted, and it has no way to ask. So it asks again a little later instead.
1324
- */
1325
- var SAFETY_REFRESH_MS = 10000;
1326
-
1327
- /**
1328
- * How long after an edit the safety net keeps looking.
1329
- *
1330
- * The reason to stop is that this re-runs every loader on the page: in
1331
- * development that is a server round trip and a recompile, and a preview
1332
- * nobody is editing has nothing to catch up on. So the net is armed by editing
1333
- * and disarms itself when the editing stops — long enough after the last
1334
- * keystroke that any write still in flight has certainly landed.
1335
- */
1336
- var SAFETY_REFRESH_WINDOW_MS = 30000;
1337
-
1338
- /**
1339
- * Fired on this window when an edit has been applied to the client store.
1340
- *
1341
- * The refresh loop listens for it so it can start the round trip immediately
1342
- * rather than on its next tick. A DOM event rather than state, because the
1343
- * counter it accompanies is a ref and the loop lives in an effect that must not
1344
- * be torn down and rebuilt per keystroke.
1345
- */
1346
- var VAL_EDIT_LANDED = "val-edit-landed";
1347
-
1348
- /**
1349
- * How often `/draft/stat` is asked when nothing is in progress.
1350
- *
1351
- * Draft mode changes when someone toggles it, which the handshake below already
1352
- * covers — so this is only there to notice it being changed somewhere else.
1353
- */
1354
1300
  var DRAFT_IDLE_POLL_MS = 20000;
1355
1301
 
1356
1302
  /**
@@ -1431,7 +1377,7 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1431
1377
  * draft mode is on, and one that is ONLY a snapshot (`draftMode: false`) is
1432
1378
  * not a draft: it must not turn draft mode on or say the draft is synced.
1433
1379
  */
1434
- var hasDraft = initialDraft !== null && initialDraft.draftMode !== false;
1380
+ var hasDraft = internal.isDraft(initialDraft);
1435
1381
  /*
1436
1382
  * As the page was rendered. A ref, not the prop, for the reason
1437
1383
  * `initialDraft` is one: a later render's prop is a client navigation's
@@ -1459,29 +1405,24 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1459
1405
  var contentVersion = analyticsSetting === null || analyticsSetting === void 0 ? void 0 : analyticsSetting.contentVersion;
1460
1406
  var snapshot = initialDraft === null || initialDraft === void 0 ? void 0 : initialDraft.snapshot;
1461
1407
  var liveSitePage = isLiveSitePage(initialDraft);
1462
- /*
1463
- * The base the Studio's overlay must start from, where it is not the
1464
- * bundle's (`ValDraft.base`): set while rendering, not in an effect, so it
1465
- * is in place before the overlay -- loaded after this page hydrates -- takes
1466
- * its modules in. The same value every render, from a ref, so writing it
1467
- * again is harmless.
1468
- */
1469
- if (typeof window !== "undefined") {
1470
- window.__VAL_BASE__ = hasDraft ? initialDraft.base : undefined;
1471
- }
1472
1408
  // TODO: move below into react package
1473
1409
  var valStore = React__default["default"].useMemo(function () {
1474
- var store = new ValOverlayContext.ValExternalStore();
1475
- if (hasDraft) {
1476
- for (var _i = 0, _Object$entries = Object.entries(initialDraft.sources); _i < _Object$entries.length; _i++) {
1477
- var _Object$entries$_i = _typeof._slicedToArray(_Object$entries[_i], 2),
1478
- path = _Object$entries$_i[0],
1479
- source = _Object$entries$_i[1];
1480
- store.update(path, source);
1481
- }
1482
- }
1483
- return store;
1484
- }, [initialDraft, hasDraft]);
1410
+ return internal.createDraftStore(initialDraft);
1411
+ }, [initialDraft]);
1412
+ /**
1413
+ * What this page reports to the Studio when it connects: the modules it
1414
+ * was rendered with a draft of (`draftHolds`). The Studio sends each of
1415
+ * them in its catch-up, so a draft that stopped being the draft before the
1416
+ * Studio was there -- discarded, published -- does not stay on the page.
1417
+ */
1418
+ var holds = React__default["default"].useMemo(function () {
1419
+ return internal.draftHolds(initialDraft);
1420
+ }, [initialDraft]);
1421
+ // And the version of each, where the draft says: one the Studio would send
1422
+ // at the same version is the same content, and is not sent again.
1423
+ var holdsVersions = React__default["default"].useMemo(function () {
1424
+ return internal.draftHoldsVersions(initialDraft);
1425
+ }, [initialDraft]);
1485
1426
  // Whether useValStega should actually suspend. False during SSR and the
1486
1427
  // hydration render. Two paths: without `draft` the server store is empty
1487
1428
  // (draft data arrives via browser CustomEvents only), so suspending there
@@ -1537,7 +1478,25 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1537
1478
  var _React$useState11 = React__default["default"].useState(hasDraft ? true : null),
1538
1479
  _React$useState12 = _typeof._slicedToArray(_React$useState11, 2),
1539
1480
  draftMode = _React$useState12[0],
1540
- setDraftMode = _React$useState12[1];
1481
+ setDraftModeState = _React$useState12[1];
1482
+ /**
1483
+ * `draftMode` as last set: what an answer from `/draft/stat` is compared
1484
+ * with, to know whether it MOVED draft mode and so whether to refresh.
1485
+ *
1486
+ * Compared here, and the refresh sent after `setDraftMode`, rather than in
1487
+ * a `setDraftMode` updater: React may call an updater twice (StrictMode)
1488
+ * or during a render, and a refresh is a side effect -- run twice it is
1489
+ * two refreshes, and run in a render it is a `startTransition` while
1490
+ * rendering, which React refuses with a warning.
1491
+ */
1492
+ var draftModeRef = React__default["default"].useRef(draftMode);
1493
+ /** Set draft mode; `true` when that moved it. */
1494
+ var setDraftMode = function setDraftMode(next) {
1495
+ var moved = draftModeRef.current !== next;
1496
+ draftModeRef.current = next;
1497
+ setDraftModeState(next);
1498
+ return moved;
1499
+ };
1541
1500
  /**
1542
1501
  * Resolves when `draftMode` stops being unknown.
1543
1502
  *
@@ -1582,15 +1541,11 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1582
1541
  isRefreshing = _React$useTransition2[0],
1583
1542
  startTransition = _React$useTransition2[1];
1584
1543
  /**
1585
- * The same answer as `isRefreshing`, readable from the refresh effect.
1586
- *
1587
- * The effect must not be rebuilt every time a refresh starts and ends — it
1588
- * owns the timers and the "when did we last edit" bookkeeping, and tearing
1589
- * that down mid-flurry loses it. A ref updated on render is the value without
1590
- * the dependency.
1544
+ * The page's refresh loop, while the overlay is up: the `val-event`
1545
+ * listener tells it what the Studio says. A ref, so the listener's effect
1546
+ * is not rebuilt with it.
1591
1547
  */
1592
- var isRefreshingRef = React__default["default"].useRef(false);
1593
- var rerenderCounterRef = React__default["default"].useRef(0);
1548
+ var refreshLoopRef = React__default["default"].useRef(null);
1594
1549
  var _React$useState15 = React__default["default"].useState(null),
1595
1550
  _React$useState16 = _typeof._slicedToArray(_React$useState15, 2),
1596
1551
  iframeSrc = _React$useState16[0],
@@ -1600,7 +1555,6 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1600
1555
  return state.location.pathname;
1601
1556
  }
1602
1557
  });
1603
- isRefreshingRef.current = isRefreshing;
1604
1558
  React.useEffect(function () {
1605
1559
  window.dispatchEvent(new CustomEvent("val-provider:pathname", {
1606
1560
  detail: pathname
@@ -1708,7 +1662,7 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1708
1662
  // suspends — no fallback flash — and the swap to draft data commits as a
1709
1663
  // normal update instead of a hydration mismatch. Never deactivated:
1710
1664
  // components must not stop suspending across renders (the draft-mode-off
1711
- // release valve lives in useValStega instead).
1665
+ // release valve lives in `overlaySource` instead).
1712
1666
  /*
1713
1667
  * Gated on `mountOverlay`, not on the cookie directly — the ORDER is the
1714
1668
  * point.
@@ -1733,17 +1687,10 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1733
1687
  }, [props.suspend, mountOverlay]);
1734
1688
 
1735
1689
  /**
1736
- * Re-render the server tree when an edit lands, then at most every 500ms.
1737
- *
1738
- * Leading edge, which is the whole point. This polled every 500ms and only
1739
- * refreshed on the tick, so a keystroke waited up to half a second before the
1740
- * round trip even began — pure dead time on top of a request that is already
1741
- * the slow part in development. Refreshing on arrival and *then* enforcing the
1742
- * gap keeps the same ceiling on requests while removing the wait.
1743
- *
1744
- * Still a counter rather than a boolean: edits that arrive during the gap have
1745
- * to be picked up by the next refresh, and the count is what says whether
1746
- * there are any.
1690
+ * Re-run the loaders when the server has the editor's changes: once per
1691
+ * version the Studio says it has confirmed (`startRefreshLoop`), never on
1692
+ * a timer and never when an edit merely arrives -- arrived, it raced the
1693
+ * write and came back without the edit.
1747
1694
  */
1748
1695
  React__default["default"].useEffect(function () {
1749
1696
  // A demo's loaders read a server that never sees the tab's edits, so
@@ -1751,92 +1698,30 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1751
1698
  if (!mountOverlay || props.disableRefresh || props.demo) {
1752
1699
  return;
1753
1700
  }
1754
- var timer = null;
1755
- var lastRefreshAt = 0;
1756
- /** When an edit last landed here. 0 means this page has never been edited. */
1757
- var lastEditAt = 0;
1758
- /** Whether a safety tick was skipped because nobody was looking. */
1759
- var missedWhileHidden = false;
1760
- var refresh = function refresh() {
1761
- rerenderCounterRef.current = 0;
1762
- lastRefreshAt = Date.now();
1763
- startTransition(function () {
1764
- /*
1765
- * Re-run the loaders of every match, then re-render.
1766
- *
1767
- * TanStack's equivalent of `router.refresh()`. `invalidate()` marks the
1768
- * current matches stale and re-runs their loaders — which is where a
1769
- * TanStack Start app reads Val content on the server — so this is the
1770
- * round trip that brings an edit across. It resolves when the loaders
1771
- * have settled, which is what keeps `isRefreshing` honest.
1772
- */
1773
- void router.invalidate();
1774
- });
1775
- };
1776
- var _tick = function tick() {
1777
- timer = null;
1778
- if (rerenderCounterRef.current === 0) return;
1779
- var since = Date.now() - lastRefreshAt;
1780
- if (since >= REFRESH_INTERVAL_MS) {
1781
- refresh();
1782
- } else {
1783
- timer = setTimeout(_tick, REFRESH_INTERVAL_MS - since);
1784
- }
1785
- };
1786
- var onEdit = function onEdit() {
1787
- lastEditAt = Date.now();
1788
- if (timer !== null) return;
1789
- timer = setTimeout(_tick, 0);
1790
- };
1791
- window.addEventListener(VAL_EDIT_LANDED, onEdit);
1792
- // A backstop for the counter being bumped without the event — the two are
1793
- // incremented in the same places today, but the poll is what made this
1794
- // correct regardless of who bumped it.
1795
- var interval = setInterval(_tick, REFRESH_INTERVAL_MS);
1796
-
1797
- /**
1798
- * The safety net: look again, whether or not anything told us to.
1799
- *
1800
- * The reasons not to are in `shouldSafetyRefresh`, where they can be tested.
1801
- * `hidden` is the one this side has to remember something about: a skipped
1802
- * tick is worth one look when the tab comes back.
1803
- */
1804
- var safetyTick = function safetyTick() {
1805
- if (document.hidden) {
1806
- missedWhileHidden = true;
1807
- return;
1701
+ var loop = internal.startRefreshLoop({
1702
+ refresh: function refresh() {
1703
+ var done;
1704
+ startTransition(function () {
1705
+ /*
1706
+ * Re-run the loaders of every match, then re-render.
1707
+ *
1708
+ * TanStack's equivalent of `router.refresh()`. `invalidate()` marks
1709
+ * the current matches stale and re-runs their loaders — which is
1710
+ * where a TanStack Start app reads Val content on the server — so
1711
+ * this is the round trip that brings an edit across. It resolves
1712
+ * when the loaders have settled, which is what keeps `isRefreshing`
1713
+ * honest, and what lets the loop hold a newer version until this
1714
+ * one is in.
1715
+ */
1716
+ done = router.invalidate();
1717
+ });
1718
+ return done;
1808
1719
  }
1809
- var go = shouldSafetyRefresh({
1810
- now: Date.now(),
1811
- lastEditAt: lastEditAt,
1812
- lastRefreshAt: lastRefreshAt,
1813
- hidden: false,
1814
- isRefreshing: isRefreshingRef.current,
1815
- windowMs: SAFETY_REFRESH_WINDOW_MS,
1816
- minIntervalMs: REFRESH_INTERVAL_MS
1817
- });
1818
- if (go) refresh();
1819
- };
1820
- var safety = setInterval(safetyTick, SAFETY_REFRESH_MS);
1821
- /**
1822
- * Coming back to the tab is worth one look.
1823
- *
1824
- * Otherwise a background tab skips its ticks and then has to wait out a
1825
- * whole interval on return — which is exactly the moment someone is looking
1826
- * at the page to see whether their edit took.
1827
- */
1828
- var onVisibilityChange = function onVisibilityChange() {
1829
- if (document.hidden || !missedWhileHidden) return;
1830
- missedWhileHidden = false;
1831
- safetyTick();
1832
- };
1833
- document.addEventListener("visibilitychange", onVisibilityChange);
1720
+ });
1721
+ refreshLoopRef.current = loop;
1834
1722
  return function () {
1835
- window.removeEventListener(VAL_EDIT_LANDED, onEdit);
1836
- document.removeEventListener("visibilitychange", onVisibilityChange);
1837
- clearInterval(interval);
1838
- clearInterval(safety);
1839
- if (timer !== null) clearTimeout(timer);
1723
+ refreshLoopRef.current = null;
1724
+ loop.stop();
1840
1725
  };
1841
1726
  }, [mountOverlay, props.disableRefresh, props.demo]);
1842
1727
  React__default["default"].useEffect(function () {
@@ -1922,14 +1807,11 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1922
1807
  core.Internal.log.error("could not get draft mode status", res);
1923
1808
  return;
1924
1809
  }
1925
- setDraftMode(function (prev) {
1926
- if (prev !== res.json.draftMode) {
1927
- rerenderCounterRef.current++;
1928
- window.dispatchEvent(new Event(VAL_EDIT_LANDED));
1929
- return res.json.draftMode;
1930
- }
1931
- return prev;
1932
- });
1810
+ if (setDraftMode(res.json.draftMode)) {
1811
+ var _refreshLoopRef$curre;
1812
+ // What the loaders read moved with it.
1813
+ (_refreshLoopRef$curre = refreshLoopRef.current) === null || _refreshLoopRef$curre === void 0 || _refreshLoopRef$curre.refresh();
1814
+ }
1933
1815
  })["catch"](function (err) {
1934
1816
  core.Internal.log.error("could not get draft mode status", err);
1935
1817
  })["finally"](function () {
@@ -1975,10 +1857,12 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1975
1857
  window.dispatchEvent(new CustomEvent("val-overlay-spa", {
1976
1858
  detail: {
1977
1859
  type: "draftMode",
1978
- value: draftMode !== null && draftMode !== void 0 ? draftMode : false
1860
+ value: draftMode !== null && draftMode !== void 0 ? draftMode : false,
1861
+ holds: holds,
1862
+ holdsVersions: holdsVersions
1979
1863
  }
1980
1864
  }));
1981
- }, [mountOverlay, draftMode, spaReady]);
1865
+ }, [mountOverlay, draftMode, spaReady, holds, holdsVersions]);
1982
1866
  React__default["default"].useEffect(function () {
1983
1867
  if (!mountOverlay) {
1984
1868
  // A visitor: only analytics tags content, and only after hydration
@@ -1995,45 +1879,39 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
1995
1879
  // (`useEncodesEditTags`), so this cannot reach one still hydrating.
1996
1880
  stega.SET_AUTO_TAG_JSX_ENABLED(true);
1997
1881
  var reactServerComponentRefreshListener = function reactServerComponentRefreshListener(event) {
1998
- if (event instanceof CustomEvent) {
1999
- var _event$detail, _event$detail2;
2000
- if (((_event$detail = event.detail) === null || _event$detail === void 0 ? void 0 : _event$detail.type) === "sources-synced") {
2001
- setDraftSourcesSynced(true);
2002
- return;
2003
- }
2004
- if (((_event$detail2 = event.detail) === null || _event$detail2 === void 0 ? void 0 : _event$detail2.type) === "source-update") {
2005
- var _event$detail3, _event$detail4;
2006
- var moduleFilePath = (_event$detail3 = event.detail) === null || _event$detail3 === void 0 ? void 0 : _event$detail3.moduleFilePath;
2007
- var source = (_event$detail4 = event.detail) === null || _event$detail4 === void 0 ? void 0 : _event$detail4.source;
2008
- if (typeof moduleFilePath === "string" && source !== undefined) {
2009
- var changed = valStore.update(moduleFilePath, source);
2010
- /**
2011
- * Armed by a CHANGE, not by a message.
2012
- *
2013
- * The editor re-sends everything it holds whenever this page
2014
- * becomes a new document — a reload, or the one the dev server does
2015
- * when a publish rewrites the `.val.ts` files — and most of that
2016
- * snapshot is content this page already has. Counting those
2017
- * bought a whole-route request per publish whether or not the
2018
- * page was out of date, which with auto-save on is one per pause
2019
- * in typing.
2020
- *
2021
- * The catch-up that MATTERS still refreshes: a page whose server
2022
- * render predates the write has no value for that module, so the
2023
- * first arrival counts as a change.
2024
- */
2025
- if (changed && !props.disableRefresh) {
2026
- rerenderCounterRef.current++;
2027
- window.dispatchEvent(new Event(VAL_EDIT_LANDED));
2028
- }
2029
- } else {
2030
- core.Internal.log.error("invalid event detail", event.detail);
2031
- }
1882
+ if (!(event instanceof CustomEvent)) {
1883
+ core.Internal.log.error("invalid custom event (val-event)", event);
1884
+ return;
1885
+ }
1886
+ var outcome = internal.applyValEvent(valStore, event.detail);
1887
+ if (outcome.type === "synced") {
1888
+ setDraftSourcesSynced(true);
1889
+ return;
1890
+ }
1891
+ if (outcome.type === "confirmed") {
1892
+ var _refreshLoopRef$curre2;
1893
+ (_refreshLoopRef$curre2 = refreshLoopRef.current) === null || _refreshLoopRef$curre2 === void 0 || _refreshLoopRef$curre2.confirmed(outcome.seq);
1894
+ return;
1895
+ }
1896
+ if (outcome.type === "invalid") {
1897
+ core.Internal.log.error("invalid custom event details (val-event)", outcome.detail);
1898
+ return;
1899
+ }
1900
+ /**
1901
+ * Not a refresh: the server may not have it yet. Only, before the
1902
+ * Studio's first `confirmed`, that this page's render is behind
1903
+ * what the Studio holds (`startRefreshLoop`). A module re-sent that
1904
+ * the page already shows does not count.
1905
+ */
1906
+ if (outcome.changed) {
1907
+ if (outcome.fromPreviousStudio === true) {
1908
+ var _refreshLoopRef$curre3;
1909
+ // A Studio of the release before: it never confirms.
1910
+ (_refreshLoopRef$curre3 = refreshLoopRef.current) === null || _refreshLoopRef$curre3 === void 0 || _refreshLoopRef$curre3.changedByPreviousStudio();
2032
1911
  } else {
2033
- core.Internal.log.error("invalid custom event details (val-event)", event.detail);
1912
+ var _refreshLoopRef$curre4;
1913
+ (_refreshLoopRef$curre4 = refreshLoopRef.current) === null || _refreshLoopRef$curre4 === void 0 || _refreshLoopRef$curre4.changed();
2034
1914
  }
2035
- } else {
2036
- core.Internal.log.error("invalid custom event (val-event)", event);
2037
1915
  }
2038
1916
  };
2039
1917
  window.addEventListener("val-event", reactServerComponentRefreshListener);
@@ -2042,7 +1920,7 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
2042
1920
  };
2043
1921
  }
2044
1922
  }
2045
- }, [mountOverlay, draftMode, analytics, props.disableRefresh]);
1923
+ }, [mountOverlay, draftMode, analytics]);
2046
1924
  React__default["default"].useEffect(function () {
2047
1925
  if (!mountOverlay) {
2048
1926
  return;
@@ -2187,7 +2065,9 @@ var ValTanStackProvider = function ValTanStackProvider(props) {
2187
2065
  })]
2188
2066
  }), mountOverlay && isCanvas && /*#__PURE__*/jsxRuntime.jsx(ValCanvasBridge, {
2189
2067
  draftMode: draftMode === true,
2190
- isRefreshing: isRefreshing
2068
+ isRefreshing: isRefreshing,
2069
+ holds: holds,
2070
+ holdsVersions: holdsVersions
2191
2071
  }), mountOverlay && iframeSrc && /*#__PURE__*/jsxRuntime.jsx("iframe", {
2192
2072
  loading: "eager",
2193
2073
  style: {
@@ -2639,7 +2519,7 @@ function _answer() {
2639
2519
  body = _t;
2640
2520
  return _context.a(2, {
2641
2521
  status: res.status,
2642
- headers: version._toConsumableArray(res.headers),
2522
+ headers: _toConsumableArray(res.headers),
2643
2523
  body: body
2644
2524
  });
2645
2525
  }