@valbuild/next 0.150.22 → 0.151.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 (77) hide show
  1. package/CHANGELOG.md +174 -0
  2. package/README.md +82 -1
  3. package/client/dist/valbuild-next-client.cjs.dev.js +123 -256
  4. package/client/dist/valbuild-next-client.cjs.prod.js +123 -256
  5. package/client/dist/valbuild-next-client.esm.js +122 -255
  6. package/dist/{ValApp-a88a9ded.esm.js → ValApp-69cd599c.esm.js} +3 -3
  7. package/dist/{ValApp-c718d1a6.cjs.dev.js → ValApp-826dca6c.cjs.dev.js} +3 -3
  8. package/dist/ValApp-866089f6.cjs.js +7 -0
  9. package/dist/{ValApp-6c3da222.cjs.prod.js → ValApp-866089f6.cjs.prod.js} +3 -3
  10. package/dist/{ValCanvasBridge-9d221d32.cjs.dev.js → ValCanvasBridge-4b81f653.cjs.dev.js} +46 -13
  11. package/dist/ValCanvasBridge-6f568f1d.cjs.js +7 -0
  12. package/dist/{ValCanvasBridge-134c3ae1.cjs.prod.js → ValCanvasBridge-6f568f1d.cjs.prod.js} +46 -13
  13. package/dist/{ValCanvasBridge-4a5ef73c.esm.js → ValCanvasBridge-badc186b.esm.js} +46 -13
  14. package/dist/ValNextProvider-41d622a7.cjs.js +7 -0
  15. package/dist/{ValNextProvider-35d0b521.cjs.prod.js → ValNextProvider-41d622a7.cjs.prod.js} +124 -234
  16. package/dist/{ValNextProvider-a85a1d33.cjs.dev.js → ValNextProvider-4b9bcfc0.cjs.dev.js} +124 -234
  17. package/dist/{ValNextProvider-df6f351e.esm.js → ValNextProvider-745ed99e.esm.js} +124 -234
  18. package/dist/ValOverlayContext-0c29b6a7.cjs.dev.js +51 -0
  19. package/dist/ValOverlayContext-0f51d111.cjs.js +7 -0
  20. package/dist/ValOverlayContext-0f51d111.cjs.prod.js +51 -0
  21. package/dist/ValOverlayContext-178db68e.esm.js +37 -0
  22. package/dist/ValVideo-02f43a7c.cjs.js +7 -0
  23. package/dist/{ValVideo-dcd2b9f8.cjs.prod.js → ValVideo-02f43a7c.cjs.prod.js} +1 -2
  24. package/dist/{ValVideo-6e410ad8.cjs.dev.js → ValVideo-15e5c4fa.cjs.dev.js} +1 -2
  25. package/dist/{ValVideo-d377f06e.esm.js → ValVideo-8acf31b6.esm.js} +1 -2
  26. package/dist/{createForOfIteratorHelper-d4afcad8.cjs.prod.js → createForOfIteratorHelper-8459fbc7.cjs.prod.js} +1 -1
  27. package/dist/{createForOfIteratorHelper-5758a730.esm.js → createForOfIteratorHelper-9209e9dd.esm.js} +1 -1
  28. package/dist/{createForOfIteratorHelper-0445603c.cjs.dev.js → createForOfIteratorHelper-9c6aa3c7.cjs.dev.js} +1 -1
  29. package/dist/declarations/src/ValProvider.d.ts +1 -0
  30. package/dist/declarations/src/client/initValClient.d.ts +25 -3
  31. package/dist/declarations/src/external_exempt_from_val_quickjs.d.ts +1 -0
  32. package/dist/declarations/src/rsc/index.d.ts +1 -0
  33. package/dist/declarations/src/rsc/initValRsc.d.ts +56 -118
  34. package/dist/declarations/src/server/initValServer.d.ts +2 -1
  35. package/dist/{fallbackColors-149dfbc9.esm.js → fallbackColors-0d915c59.esm.js} +1 -1
  36. package/dist/{fallbackColors-080c1bbc.cjs.dev.js → fallbackColors-38f01ad2.cjs.dev.js} +1 -1
  37. package/dist/{fallbackColors-e1d73b36.cjs.prod.js → fallbackColors-d9a7b81e.cjs.prod.js} +1 -1
  38. package/dist/{objectSpread2-13f847a9.cjs.prod.js → objectSpread2-469273e9.cjs.prod.js} +25 -2
  39. package/dist/{objectSpread2-58024783.cjs.dev.js → objectSpread2-58726b14.cjs.dev.js} +25 -2
  40. package/dist/{objectSpread2-60d1bd93.esm.js → objectSpread2-f1f0d77a.esm.js} +24 -1
  41. package/dist/{routeFromVal-64cba2a6.esm.js → routeFromVal-80976677.esm.js} +5 -155
  42. package/dist/{routeFromVal-4df1157a.cjs.dev.js → routeFromVal-a1e0a8be.cjs.dev.js} +3 -159
  43. package/dist/{routeFromVal-812be6ea.cjs.prod.js → routeFromVal-c4dace91.cjs.prod.js} +3 -159
  44. package/dist/{slicedToArray-ce613de6.cjs.prod.js → slicedToArray-0fc63719.cjs.prod.js} +1 -1
  45. package/dist/{slicedToArray-aa291011.esm.js → slicedToArray-5ff32315.esm.js} +1 -1
  46. package/dist/{slicedToArray-44036a76.cjs.dev.js → slicedToArray-ed812733.cjs.dev.js} +1 -1
  47. package/dist/{unsupportedIterableToArray-c8ab77c9.cjs.dev.js → unsupportedIterableToArray-1aadf9fb.cjs.prod.js} +1 -0
  48. package/dist/{unsupportedIterableToArray-0d2087a2.cjs.prod.js → unsupportedIterableToArray-20a9209f.cjs.dev.js} +1 -0
  49. package/dist/{unsupportedIterableToArray-5baabfdc.esm.js → unsupportedIterableToArray-f57eb659.esm.js} +1 -1
  50. package/dist/useHydrated-8e90c7fa.cjs.js +7 -0
  51. package/dist/useHydrated-8e90c7fa.cjs.prod.js +34 -0
  52. package/dist/useHydrated-afcdbb66.esm.js +26 -0
  53. package/dist/useHydrated-ff733777.cjs.dev.js +34 -0
  54. package/dist/valbuild-next.cjs.dev.js +7 -8
  55. package/dist/valbuild-next.cjs.prod.js +7 -8
  56. package/dist/valbuild-next.esm.js +7 -8
  57. package/dist/{version-18c11a6d.cjs.prod.js → version-114c45b6.cjs.prod.js} +1 -1
  58. package/dist/{version-70b7333d.cjs.dev.js → version-cc1bc4e5.cjs.dev.js} +1 -1
  59. package/dist/{version-a2218191.esm.js → version-d8f64e54.esm.js} +1 -1
  60. package/package.json +8 -8
  61. package/rsc/dist/valbuild-next-rsc.cjs.dev.js +243 -872
  62. package/rsc/dist/valbuild-next-rsc.cjs.prod.js +243 -872
  63. package/rsc/dist/valbuild-next-rsc.esm.js +243 -872
  64. package/server/dist/valbuild-next-server.cjs.dev.js +4 -5
  65. package/server/dist/valbuild-next-server.cjs.prod.js +4 -5
  66. package/server/dist/valbuild-next-server.esm.js +4 -5
  67. package/dist/ValApp-6c3da222.cjs.js +0 -7
  68. package/dist/ValCanvasBridge-134c3ae1.cjs.js +0 -7
  69. package/dist/ValNextProvider-35d0b521.cjs.js +0 -7
  70. package/dist/ValOverlayContext-1a45e03d.esm.js +0 -290
  71. package/dist/ValOverlayContext-262899b3.cjs.dev.js +0 -301
  72. package/dist/ValOverlayContext-405ca70c.cjs.js +0 -7
  73. package/dist/ValOverlayContext-405ca70c.cjs.prod.js +0 -301
  74. package/dist/ValVideo-dcd2b9f8.cjs.js +0 -7
  75. package/dist/defineProperty-8951f469.cjs.prod.js +0 -29
  76. package/dist/defineProperty-cca5affa.esm.js +0 -26
  77. package/dist/defineProperty-f90345d7.cjs.dev.js +0 -29
@@ -3,23 +3,23 @@
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
5
5
 
6
- var slicedToArray = require('./slicedToArray-44036a76.cjs.dev.js');
6
+ var slicedToArray = require('./slicedToArray-ed812733.cjs.dev.js');
7
7
  var asyncToGenerator = require('./asyncToGenerator-c3823d62.cjs.dev.js');
8
- var objectSpread2 = require('./objectSpread2-58024783.cjs.dev.js');
8
+ var objectSpread2 = require('./objectSpread2-58726b14.cjs.dev.js');
9
9
  var core = require('@valbuild/core');
10
10
  var ui = require('@valbuild/ui');
11
11
  var navigation = require('next/navigation');
12
12
  var Script = require('next/script');
13
13
  var React = require('react');
14
- var ValOverlayContext = require('./ValOverlayContext-262899b3.cjs.dev.js');
14
+ var ValOverlayContext = require('./ValOverlayContext-0c29b6a7.cjs.dev.js');
15
15
  var stega = require('@valbuild/react/stega');
16
- var fallbackColors = require('./fallbackColors-080c1bbc.cjs.dev.js');
16
+ var internal = require('@valbuild/react/internal');
17
+ var fallbackColors = require('./fallbackColors-38f01ad2.cjs.dev.js');
17
18
  var client = require('@valbuild/shared/client');
18
- var createForOfIteratorHelper = require('./createForOfIteratorHelper-0445603c.cjs.dev.js');
19
- var ValCanvasBridge = require('./ValCanvasBridge-9d221d32.cjs.dev.js');
19
+ var createForOfIteratorHelper = require('./createForOfIteratorHelper-9c6aa3c7.cjs.dev.js');
20
+ var ValCanvasBridge = require('./ValCanvasBridge-4b81f653.cjs.dev.js');
20
21
  var jsxRuntime = require('react/jsx-runtime');
21
- require('./unsupportedIterableToArray-c8ab77c9.cjs.dev.js');
22
- require('./defineProperty-f90345d7.cjs.dev.js');
22
+ require('./unsupportedIterableToArray-20a9209f.cjs.dev.js');
23
23
 
24
24
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
25
25
 
@@ -96,82 +96,6 @@ function hasValEnableCookie(cookieString) {
96
96
  return false;
97
97
  }
98
98
 
99
- /**
100
- * Whether the page should re-read the server on its own, right now.
101
- *
102
- * The page cannot know when its own edit has been persisted. An edit is applied
103
- * to the client store immediately and written to the server asynchronously, so
104
- * a `router.refresh()` fired on the edit can reach a server that has not saved
105
- * the patch yet: the payload comes back with the old content, and nothing
106
- * schedules another look. What an editor sees is a canvas that flickered and did
107
- * not change — which is indistinguishable from an edit that did not work.
108
- *
109
- * So the page asks again a little later. This is the "should I bother" half of
110
- * that, kept out of the effect and pure so the reasons NOT to are checkable:
111
- * every one of them is a whole-route request avoided, and in development Next
112
- * re-renders (and sometimes recompiles) the page for each one.
113
- */
114
- function shouldSafetyRefresh(state) {
115
- // Never edited: a preview someone is only looking at has nothing to catch up
116
- // on, and polling it would cost a route render per tick for nothing.
117
- if (state.lastEditAt === 0) return false;
118
- // The editing stopped long enough ago that anything in flight has landed.
119
- if (state.now - state.lastEditAt > state.windowMs) return false;
120
- // Nobody is looking. The caller refreshes once on the way back instead.
121
- if (state.hidden) return false;
122
- // Already on its way. Stacking whole-route requests makes the slowness this
123
- // exists to paper over worse, not better.
124
- if (state.isRefreshing) return false;
125
- // One just went out — usually the edit-driven refresh, a moment ago.
126
- if (state.now - state.lastRefreshAt < state.minIntervalMs) return false;
127
- return true;
128
- }
129
-
130
- var REFRESH_INTERVAL_MS = 500;
131
-
132
- /**
133
- * How often the page re-reads the server on its own, without being told to.
134
- *
135
- * A safety net for a race no amount of care on this side can close. An edit is
136
- * applied to the client store immediately and saved to the server
137
- * asynchronously, so `router.refresh()` fired on the edit can reach a server
138
- * that has not written the patch yet: the RSC payload comes back with the old
139
- * content, the counter has already been cleared, and nothing schedules another
140
- * look. What you see is a canvas that flickered and did not change — which is
141
- * indistinguishable from an edit that did not work.
142
- *
143
- * The honest fix would be for the page to know when its patch has been
144
- * persisted, and it has no way to ask. So it asks again a little later instead.
145
- */
146
- var SAFETY_REFRESH_MS = 10000;
147
-
148
- /**
149
- * How long after an edit the safety net keeps looking.
150
- *
151
- * The reason to stop is that this is a whole-route request: in development Next
152
- * re-renders (and sometimes recompiles) the page for every one, and a preview
153
- * nobody is editing has nothing to catch up on. So the net is armed by editing
154
- * and disarms itself when the editing stops — long enough after the last
155
- * keystroke that any write still in flight has certainly landed.
156
- */
157
- var SAFETY_REFRESH_WINDOW_MS = 30000;
158
-
159
- /**
160
- * Fired on this window when an edit has been applied to the client store.
161
- *
162
- * The refresh loop listens for it so it can start the round trip immediately
163
- * rather than on its next tick. A DOM event rather than state, because the
164
- * counter it accompanies is a ref and the loop lives in an effect that must not
165
- * be torn down and rebuilt per keystroke.
166
- */
167
- var VAL_EDIT_LANDED = "val-edit-landed";
168
-
169
- /**
170
- * How often `/draft/stat` is asked when nothing is in progress.
171
- *
172
- * Draft mode changes when someone toggles it, which the handshake below already
173
- * covers — so this is only there to notice it being changed somewhere else.
174
- */
175
99
  var DRAFT_IDLE_POLL_MS = 20000;
176
100
 
177
101
  /**
@@ -198,7 +122,7 @@ var DRAFT_HANDSHAKE_POLL_MS = 100;
198
122
  */
199
123
  var DRAFT_HANDSHAKE_TIMEOUT_MS = 10000;
200
124
  var ValNextProvider = function ValNextProvider(props) {
201
- var _draftModeReady$curre2;
125
+ var _props$draft, _draftModeReady$curre2;
202
126
  // TODO: use config:
203
127
  var route = "/api/val";
204
128
  /**
@@ -238,10 +162,32 @@ var ValNextProvider = function ValNextProvider(props) {
238
162
  }, _callee);
239
163
  })), [route]);
240
164
 
165
+ /**
166
+ * The draft the page was rendered with, as it was at mount. A ref, not the
167
+ * prop: the server and the browser's first render must start from the
168
+ * same thing, and a later prop must not reset a store the overlay has been
169
+ * updating since. One that is not a draft (`draftMode: false`) is not one.
170
+ */
171
+ var initialDraft = React__default["default"].useRef((_props$draft = props.draft) !== null && _props$draft !== void 0 ? _props$draft : null).current;
172
+ var hasDraft = internal.isDraft(initialDraft);
241
173
  // TODO: move below into react package
242
174
  var valStore = React__default["default"].useMemo(function () {
243
- return new ValOverlayContext.ValExternalStore();
244
- }, []);
175
+ return internal.createDraftStore(initialDraft);
176
+ }, [initialDraft]);
177
+ /**
178
+ * What this page reports to the Studio when it connects: the modules it
179
+ * was rendered with a draft of (`draftHolds`). The Studio sends each of
180
+ * them in its catch-up, so a draft that stopped being the draft before the
181
+ * Studio was there -- discarded, published -- does not stay on the page.
182
+ */
183
+ var holds = React__default["default"].useMemo(function () {
184
+ return internal.draftHolds(initialDraft);
185
+ }, [initialDraft]);
186
+ // And the version of each, where the draft says: one the Studio would send
187
+ // at the same version is the same content, and is not sent again.
188
+ var holdsVersions = React__default["default"].useMemo(function () {
189
+ return internal.draftHoldsVersions(initialDraft);
190
+ }, [initialDraft]);
245
191
  // Whether useValStega should actually suspend. False during SSR and the
246
192
  // hydration render — the server store is never populated (draft data
247
193
  // arrives via browser CustomEvents only), so suspending there would just
@@ -263,7 +209,9 @@ var ValNextProvider = function ValNextProvider(props) {
263
209
  * protocol. Without this the page could only wait out `waitForLoad`'s timeout,
264
210
  * ten seconds per module nobody had edited.
265
211
  */
266
- var _React$useState3 = React__default["default"].useState(false),
212
+ // A server-read draft is everything there is: a module missing from it has
213
+ // no changes, so there is nothing more to wait for.
214
+ var _React$useState3 = React__default["default"].useState(hasDraft),
267
215
  _React$useState4 = slicedToArray._slicedToArray(_React$useState3, 2),
268
216
  draftSourcesSynced = _React$useState4[0],
269
217
  setDraftSourcesSynced = _React$useState4[1];
@@ -288,10 +236,30 @@ var ValNextProvider = function ValNextProvider(props) {
288
236
  _React$useState8 = slicedToArray._slicedToArray(_React$useState7, 2),
289
237
  isCanvas = _React$useState8[0],
290
238
  setIsCanvas = _React$useState8[1];
291
- var _React$useState9 = React__default["default"].useState(null),
239
+ // Known on the server when it read a draft: a draft is only read in draft
240
+ // mode. Otherwise unknown until `/draft/stat` answers.
241
+ var _React$useState9 = React__default["default"].useState(hasDraft ? true : null),
292
242
  _React$useState0 = slicedToArray._slicedToArray(_React$useState9, 2),
293
243
  draftMode = _React$useState0[0],
294
- setDraftMode = _React$useState0[1];
244
+ setDraftModeState = _React$useState0[1];
245
+ /**
246
+ * `draftMode` as last set: what an answer from `/draft/stat` is compared
247
+ * with, to know whether it MOVED draft mode and so whether to refresh.
248
+ *
249
+ * Compared here, and the refresh sent after `setDraftMode`, rather than in
250
+ * a `setDraftMode` updater: React may call an updater twice (StrictMode)
251
+ * or during a render, and a refresh is a side effect -- run twice it is
252
+ * two refreshes, and run in a render it is a `startTransition` while
253
+ * rendering, which React refuses with a warning.
254
+ */
255
+ var draftModeRef = React__default["default"].useRef(draftMode);
256
+ /** Set draft mode; `true` when that moved it. */
257
+ var setDraftMode = function setDraftMode(next) {
258
+ var moved = draftModeRef.current !== next;
259
+ draftModeRef.current = next;
260
+ setDraftModeState(next);
261
+ return moved;
262
+ };
295
263
  /**
296
264
  * Resolves when `draftMode` stops being unknown.
297
265
  *
@@ -336,21 +304,16 @@ var ValNextProvider = function ValNextProvider(props) {
336
304
  isRefreshing = _React$useTransition2[0],
337
305
  startTransition = _React$useTransition2[1];
338
306
  /**
339
- * The same answer as `isRefreshing`, readable from the refresh effect.
340
- *
341
- * The effect must not be rebuilt every time a refresh starts and ends — it
342
- * owns the timers and the "when did we last edit" bookkeeping, and tearing
343
- * that down mid-flurry loses it. A ref updated on render is the value without
344
- * the dependency.
307
+ * The page's refresh loop, while the overlay is up: the `val-event`
308
+ * listener tells it what the Studio says. A ref, so the listener's effect
309
+ * is not rebuilt with it.
345
310
  */
346
- var isRefreshingRef = React__default["default"].useRef(false);
347
- var rerenderCounterRef = React__default["default"].useRef(0);
311
+ var refreshLoopRef = React__default["default"].useRef(null);
348
312
  var _React$useState11 = React__default["default"].useState(null),
349
313
  _React$useState12 = slicedToArray._slicedToArray(_React$useState11, 2),
350
314
  iframeSrc = _React$useState12[0],
351
315
  setIframeSrc = _React$useState12[1];
352
316
  var pathname = navigation.usePathname();
353
- isRefreshingRef.current = isRefreshing;
354
317
  React.useEffect(function () {
355
318
  window.dispatchEvent(new CustomEvent("val-provider:pathname", {
356
319
  detail: pathname
@@ -375,7 +338,7 @@ var ValNextProvider = function ValNextProvider(props) {
375
338
  // suspends — no fallback flash — and the swap to draft data commits as a
376
339
  // normal update instead of a hydration mismatch. Never deactivated:
377
340
  // components must not stop suspending across renders (the draft-mode-off
378
- // release valve lives in useValStega instead).
341
+ // release valve lives in `overlaySource` instead).
379
342
  if (props.suspend && shouldEnableVal()) {
380
343
  startTransition(function () {
381
344
  setSuspendActive(true);
@@ -384,99 +347,30 @@ var ValNextProvider = function ValNextProvider(props) {
384
347
  }, [props.suspend]);
385
348
 
386
349
  /**
387
- * Re-render the server tree when an edit lands, then at most every 500ms.
350
+ * Re-render the server tree when the server has the editor's changes: once
351
+ * per version the Studio says it has confirmed (`startRefreshLoop`), never
352
+ * on a timer and never when an edit merely arrives -- arrived, it raced
353
+ * the write, and the server components came back without the edit.
388
354
  *
389
- * Leading edge, which is the whole point. This polled every 500ms and only
390
- * refreshed on the tick, so a keystroke waited up to half a second before the
391
- * round trip even began — pure dead time on top of a request that is already
392
- * the slow part in development. Refreshing on arrival and *then* enforcing the
393
- * gap keeps the same ceiling on requests while removing the wait.
394
- *
395
- * Still a counter rather than a boolean: edits that arrive during the gap have
396
- * to be picked up by the next refresh, and the count is what says whether
397
- * there are any.
355
+ * `router.refresh()` does not say when it is done, so a version confirmed
356
+ * while one is on its way is a refresh of its own; the transition is what
357
+ * the canvas shows as busy (`isRefreshing`).
398
358
  */
399
359
  React__default["default"].useEffect(function () {
400
360
  if (!mountOverlay || props.disableRefresh) {
401
361
  return;
402
362
  }
403
- var timer = null;
404
- var lastRefreshAt = 0;
405
- /** When an edit last landed here. 0 means this page has never been edited. */
406
- var lastEditAt = 0;
407
- /** Whether a safety tick was skipped because nobody was looking. */
408
- var missedWhileHidden = false;
409
- var refresh = function refresh() {
410
- rerenderCounterRef.current = 0;
411
- lastRefreshAt = Date.now();
412
- startTransition(function () {
413
- router.refresh();
414
- });
415
- };
416
- var _tick = function tick() {
417
- timer = null;
418
- if (rerenderCounterRef.current === 0) return;
419
- var since = Date.now() - lastRefreshAt;
420
- if (since >= REFRESH_INTERVAL_MS) {
421
- refresh();
422
- } else {
423
- timer = setTimeout(_tick, REFRESH_INTERVAL_MS - since);
424
- }
425
- };
426
- var onEdit = function onEdit() {
427
- lastEditAt = Date.now();
428
- if (timer !== null) return;
429
- timer = setTimeout(_tick, 0);
430
- };
431
- window.addEventListener(VAL_EDIT_LANDED, onEdit);
432
- // A backstop for the counter being bumped without the event — the two are
433
- // incremented in the same places today, but the poll is what made this
434
- // correct regardless of who bumped it.
435
- var interval = setInterval(_tick, REFRESH_INTERVAL_MS);
436
-
437
- /**
438
- * The safety net: look again, whether or not anything told us to.
439
- *
440
- * The reasons not to are in `shouldSafetyRefresh`, where they can be tested.
441
- * `hidden` is the one this side has to remember something about: a skipped
442
- * tick is worth one look when the tab comes back.
443
- */
444
- var safetyTick = function safetyTick() {
445
- if (document.hidden) {
446
- missedWhileHidden = true;
447
- return;
363
+ var loop = internal.startRefreshLoop({
364
+ refresh: function refresh() {
365
+ startTransition(function () {
366
+ router.refresh();
367
+ });
448
368
  }
449
- var go = shouldSafetyRefresh({
450
- now: Date.now(),
451
- lastEditAt: lastEditAt,
452
- lastRefreshAt: lastRefreshAt,
453
- hidden: false,
454
- isRefreshing: isRefreshingRef.current,
455
- windowMs: SAFETY_REFRESH_WINDOW_MS,
456
- minIntervalMs: REFRESH_INTERVAL_MS
457
- });
458
- if (go) refresh();
459
- };
460
- var safety = setInterval(safetyTick, SAFETY_REFRESH_MS);
461
- /**
462
- * Coming back to the tab is worth one look.
463
- *
464
- * Otherwise a background tab skips its ticks and then has to wait out a
465
- * whole interval on return — which is exactly the moment someone is looking
466
- * at the page to see whether their edit took.
467
- */
468
- var onVisibilityChange = function onVisibilityChange() {
469
- if (document.hidden || !missedWhileHidden) return;
470
- missedWhileHidden = false;
471
- safetyTick();
472
- };
473
- document.addEventListener("visibilitychange", onVisibilityChange);
369
+ });
370
+ refreshLoopRef.current = loop;
474
371
  return function () {
475
- window.removeEventListener(VAL_EDIT_LANDED, onEdit);
476
- document.removeEventListener("visibilitychange", onVisibilityChange);
477
- clearInterval(interval);
478
- clearInterval(safety);
479
- if (timer !== null) clearTimeout(timer);
372
+ refreshLoopRef.current = null;
373
+ loop.stop();
480
374
  };
481
375
  }, [mountOverlay, props.disableRefresh]);
482
376
  React__default["default"].useEffect(function () {
@@ -560,14 +454,11 @@ var ValNextProvider = function ValNextProvider(props) {
560
454
  core.Internal.log.error("could not get draft mode status", res);
561
455
  return;
562
456
  }
563
- setDraftMode(function (prev) {
564
- if (prev !== res.json.draftMode) {
565
- rerenderCounterRef.current++;
566
- window.dispatchEvent(new Event(VAL_EDIT_LANDED));
567
- return res.json.draftMode;
568
- }
569
- return prev;
570
- });
457
+ if (setDraftMode(res.json.draftMode)) {
458
+ var _refreshLoopRef$curre;
459
+ // What the server components read moved with it.
460
+ (_refreshLoopRef$curre = refreshLoopRef.current) === null || _refreshLoopRef$curre === void 0 || _refreshLoopRef$curre.refresh();
461
+ }
571
462
  })["catch"](function (err) {
572
463
  core.Internal.log.error("could not get draft mode status", err);
573
464
  })["finally"](function () {
@@ -613,10 +504,12 @@ var ValNextProvider = function ValNextProvider(props) {
613
504
  window.dispatchEvent(new CustomEvent("val-overlay-spa", {
614
505
  detail: {
615
506
  type: "draftMode",
616
- value: draftMode !== null && draftMode !== void 0 ? draftMode : false
507
+ value: draftMode !== null && draftMode !== void 0 ? draftMode : false,
508
+ holds: holds,
509
+ holdsVersions: holdsVersions
617
510
  }
618
511
  }));
619
- }, [mountOverlay, draftMode, spaReady]);
512
+ }, [mountOverlay, draftMode, spaReady, holds, holdsVersions]);
620
513
  React__default["default"].useEffect(function () {
621
514
  if (!mountOverlay) {
622
515
  stega.SET_AUTO_TAG_JSX_ENABLED(false);
@@ -624,45 +517,39 @@ var ValNextProvider = function ValNextProvider(props) {
624
517
  if (draftMode) {
625
518
  stega.SET_AUTO_TAG_JSX_ENABLED(true);
626
519
  var reactServerComponentRefreshListener = function reactServerComponentRefreshListener(event) {
627
- if (event instanceof CustomEvent) {
628
- var _event$detail, _event$detail2;
629
- if (((_event$detail = event.detail) === null || _event$detail === void 0 ? void 0 : _event$detail.type) === "sources-synced") {
630
- setDraftSourcesSynced(true);
631
- return;
632
- }
633
- if (((_event$detail2 = event.detail) === null || _event$detail2 === void 0 ? void 0 : _event$detail2.type) === "source-update") {
634
- var _event$detail3, _event$detail4;
635
- var moduleFilePath = (_event$detail3 = event.detail) === null || _event$detail3 === void 0 ? void 0 : _event$detail3.moduleFilePath;
636
- var source = (_event$detail4 = event.detail) === null || _event$detail4 === void 0 ? void 0 : _event$detail4.source;
637
- if (typeof moduleFilePath === "string" && source !== undefined) {
638
- var changed = valStore.update(moduleFilePath, source);
639
- /**
640
- * Armed by a CHANGE, not by a message.
641
- *
642
- * The editor re-sends everything it holds whenever this page
643
- * becomes a new document — a reload, or the one `next dev` does
644
- * when a publish rewrites the `.val.ts` files — and most of that
645
- * snapshot is content this page already has. Counting those
646
- * bought a whole-route request per publish whether or not the
647
- * page was out of date, which with auto-save on is one per pause
648
- * in typing.
649
- *
650
- * The catch-up that MATTERS still refreshes: a page whose server
651
- * render predates the write has no value for that module, so the
652
- * first arrival counts as a change.
653
- */
654
- if (changed && !props.disableRefresh) {
655
- rerenderCounterRef.current++;
656
- window.dispatchEvent(new Event(VAL_EDIT_LANDED));
657
- }
658
- } else {
659
- core.Internal.log.error("invalid event detail", event.detail);
660
- }
520
+ if (!(event instanceof CustomEvent)) {
521
+ core.Internal.log.error("invalid custom event (val-event)", event);
522
+ return;
523
+ }
524
+ var outcome = internal.applyValEvent(valStore, event.detail);
525
+ if (outcome.type === "synced") {
526
+ setDraftSourcesSynced(true);
527
+ return;
528
+ }
529
+ if (outcome.type === "confirmed") {
530
+ var _refreshLoopRef$curre2;
531
+ (_refreshLoopRef$curre2 = refreshLoopRef.current) === null || _refreshLoopRef$curre2 === void 0 || _refreshLoopRef$curre2.confirmed(outcome.seq);
532
+ return;
533
+ }
534
+ if (outcome.type === "invalid") {
535
+ core.Internal.log.error("invalid custom event details (val-event)", outcome.detail);
536
+ return;
537
+ }
538
+ /**
539
+ * Not a refresh: the server may not have it yet. Only, before the
540
+ * Studio's first `confirmed`, that this page's render is behind
541
+ * what the Studio holds (`startRefreshLoop`). A module re-sent that
542
+ * the page already shows does not count.
543
+ */
544
+ if (outcome.changed) {
545
+ if (outcome.fromPreviousStudio === true) {
546
+ var _refreshLoopRef$curre3;
547
+ // A Studio of the release before: it never confirms.
548
+ (_refreshLoopRef$curre3 = refreshLoopRef.current) === null || _refreshLoopRef$curre3 === void 0 || _refreshLoopRef$curre3.changedByPreviousStudio();
661
549
  } else {
662
- core.Internal.log.error("invalid custom event details (val-event)", event.detail);
550
+ var _refreshLoopRef$curre4;
551
+ (_refreshLoopRef$curre4 = refreshLoopRef.current) === null || _refreshLoopRef$curre4 === void 0 || _refreshLoopRef$curre4.changed();
663
552
  }
664
- } else {
665
- core.Internal.log.error("invalid custom event (val-event)", event);
666
553
  }
667
554
  };
668
555
  window.addEventListener("val-event", reactServerComponentRefreshListener);
@@ -671,7 +558,7 @@ var ValNextProvider = function ValNextProvider(props) {
671
558
  };
672
559
  }
673
560
  }
674
- }, [mountOverlay, draftMode, props.disableRefresh]);
561
+ }, [mountOverlay, draftMode]);
675
562
  React__default["default"].useEffect(function () {
676
563
  if (!mountOverlay) {
677
564
  return;
@@ -755,6 +642,7 @@ var ValNextProvider = function ValNextProvider(props) {
755
642
  draftMode: draftMode,
756
643
  draftModeReady: (_draftModeReady$curre2 = draftModeReady.current) === null || _draftModeReady$curre2 === void 0 ? void 0 : _draftModeReady$curre2.promise,
757
644
  draftSourcesSynced: draftSourcesSynced,
645
+ serverDraft: hasDraft,
758
646
  suspend: suspendActive,
759
647
  store: valStore,
760
648
  children: [props.children, dropZone !== null && !spaLoaded && mountOverlay &&
@@ -813,7 +701,9 @@ var ValNextProvider = function ValNextProvider(props) {
813
701
  })]
814
702
  }), mountOverlay && isCanvas && /*#__PURE__*/jsxRuntime.jsx(ValCanvasBridge.ValCanvasBridge, {
815
703
  draftMode: draftMode === true,
816
- isRefreshing: isRefreshing
704
+ isRefreshing: isRefreshing,
705
+ holds: holds,
706
+ holdsVersions: holdsVersions
817
707
  }), mountOverlay && iframeSrc && /*#__PURE__*/jsxRuntime.jsx("iframe", {
818
708
  loading: "eager",
819
709
  style: {