@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.10

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 (219) hide show
  1. package/dist/action-context.js +1 -1
  2. package/dist/actions.d.ts +7 -0
  3. package/dist/actions.js +34 -10
  4. package/dist/after.d.ts +28 -0
  5. package/dist/after.js +48 -0
  6. package/dist/assets-static.d.ts +10 -1
  7. package/dist/assets-static.js +10 -2
  8. package/dist/base-path.d.ts +7 -0
  9. package/dist/base-path.js +20 -0
  10. package/dist/boundary.js +1 -1
  11. package/dist/builtin-tags.d.ts +12 -0
  12. package/dist/builtin-tags.js +42 -0
  13. package/dist/cache-adapter.js +1 -1
  14. package/dist/cache-scope.d.ts +8 -0
  15. package/dist/cache-scope.js +14 -1
  16. package/dist/cloudflare-workers.d.ts +2 -0
  17. package/dist/cloudflare-workers.js +27 -0
  18. package/dist/context.d.ts +5 -0
  19. package/dist/context.js +28 -17
  20. package/dist/dev-overlay.d.ts +54 -3
  21. package/dist/dev-overlay.js +496 -91
  22. package/dist/dom.d.ts +4 -0
  23. package/dist/dom.js +62 -16
  24. package/dist/effects.js +43 -10
  25. package/dist/events.d.ts +2 -0
  26. package/dist/events.js +18 -0
  27. package/dist/flight-props.d.ts +7 -0
  28. package/dist/flight-props.js +105 -0
  29. package/dist/handler-html.js +4 -4
  30. package/dist/handler-routing.d.ts +2 -23
  31. package/dist/handler-routing.js +9 -190
  32. package/dist/handler-segments.d.ts +6 -0
  33. package/dist/handler-segments.js +18 -4
  34. package/dist/head-tags.d.ts +9 -0
  35. package/dist/head-tags.js +93 -6
  36. package/dist/highlight-bind.js +1 -1
  37. package/dist/highlight.d.ts +3 -2
  38. package/dist/highlight.js +17 -12
  39. package/dist/hints.d.ts +25 -2
  40. package/dist/hints.js +147 -74
  41. package/dist/index.d.ts +2 -0
  42. package/dist/index.js +3 -0
  43. package/dist/island-swap.d.ts +47 -0
  44. package/dist/island-swap.js +635 -0
  45. package/dist/jsx-dev-runtime.d.ts +1 -1
  46. package/dist/jsx-types.d.ts +99 -3
  47. package/dist/krate-core.js +1 -1
  48. package/dist/krate-dev.js +407 -80
  49. package/dist/krate-f-actions.js +1 -1
  50. package/dist/krate-f-classes.js +1 -1
  51. package/dist/krate-f-highlight.js +1 -1
  52. package/dist/krate-f-list.js +1 -1
  53. package/dist/krate-f-navigation.js +1 -1
  54. package/dist/krate-f-router-lite.js +1 -1
  55. package/dist/krate-f-router.js +1 -1
  56. package/dist/krate-features.json +455 -0
  57. package/dist/krate-hydrate-lite.js +5 -5
  58. package/dist/krate-hydrate.js +5 -5
  59. package/dist/krate-island-swap.js +1 -0
  60. package/dist/krate-runtime.js +5 -5
  61. package/dist/krate-suspense.js +1 -1
  62. package/dist/list.d.ts +14 -4
  63. package/dist/list.js +30 -23
  64. package/dist/metadata.js +5 -2
  65. package/dist/navigation-react.d.ts +13 -0
  66. package/dist/navigation-react.js +86 -0
  67. package/dist/navigation.d.ts +19 -0
  68. package/dist/navigation.js +35 -0
  69. package/dist/node-server.js +3 -3
  70. package/dist/og.js +1 -1
  71. package/dist/platform.d.ts +52 -0
  72. package/dist/platform.js +57 -0
  73. package/dist/react-compat.d.ts +39 -0
  74. package/dist/react-compat.js +359 -0
  75. package/dist/react-server.d.ts +63 -0
  76. package/dist/react-server.js +125 -0
  77. package/dist/region-wire.d.ts +10 -0
  78. package/dist/region-wire.js +154 -0
  79. package/dist/request-context.js +1 -1
  80. package/dist/request-extras.d.ts +4 -0
  81. package/dist/request-extras.js +51 -0
  82. package/dist/resp.js +1 -1
  83. package/dist/route-source.d.ts +39 -0
  84. package/dist/route-source.js +532 -0
  85. package/dist/router-lite.js +3 -9
  86. package/dist/router.d.ts +1 -1
  87. package/dist/router.js +675 -100
  88. package/dist/server-handler.d.ts +69 -1
  89. package/dist/server-handler.js +515 -96
  90. package/dist/server-renderer.js +16 -7
  91. package/dist/server.d.ts +73 -16
  92. package/dist/server.js +619 -102
  93. package/dist/sha256.js +9 -9
  94. package/dist/signal.js +47 -20
  95. package/dist/site-config.d.ts +15 -0
  96. package/dist/site-config.js +28 -0
  97. package/dist/slot.d.ts +12 -0
  98. package/dist/slot.js +23 -0
  99. package/dist/ssr-core.d.ts +2 -0
  100. package/dist/ssr-core.js +12 -4
  101. package/dist/suspense.js +34 -2
  102. package/dist/testing.d.ts +62 -0
  103. package/dist/testing.js +90 -0
  104. package/dist/use-id.js +1 -1
  105. package/package.json +28 -3
  106. package/dist/action-context.test.d.ts +0 -1
  107. package/dist/action-context.test.js +0 -72
  108. package/dist/actions.test.d.ts +0 -1
  109. package/dist/actions.test.js +0 -477
  110. package/dist/cache-adapter.test.d.ts +0 -1
  111. package/dist/cache-adapter.test.js +0 -244
  112. package/dist/cache.test.d.ts +0 -1
  113. package/dist/cache.test.js +0 -233
  114. package/dist/class-component.test.d.ts +0 -1
  115. package/dist/class-component.test.js +0 -462
  116. package/dist/class-helpers.test.d.ts +0 -1
  117. package/dist/class-helpers.test.js +0 -169
  118. package/dist/context.test.d.ts +0 -1
  119. package/dist/context.test.js +0 -57
  120. package/dist/derived.test.d.ts +0 -1
  121. package/dist/derived.test.js +0 -140
  122. package/dist/dev-overlay.test.d.ts +0 -1
  123. package/dist/dev-overlay.test.js +0 -152
  124. package/dist/discrete.test.d.ts +0 -1
  125. package/dist/discrete.test.js +0 -100
  126. package/dist/dom.test.d.ts +0 -1
  127. package/dist/dom.test.js +0 -31
  128. package/dist/effect-errors.test.d.ts +0 -1
  129. package/dist/effect-errors.test.js +0 -163
  130. package/dist/effects.test.d.ts +0 -1
  131. package/dist/effects.test.js +0 -485
  132. package/dist/events.test.d.ts +0 -1
  133. package/dist/events.test.js +0 -447
  134. package/dist/handler-html.test.d.ts +0 -1
  135. package/dist/handler-html.test.js +0 -256
  136. package/dist/handler-routing.test.d.ts +0 -1
  137. package/dist/handler-routing.test.js +0 -99
  138. package/dist/handler-segments.test.d.ts +0 -1
  139. package/dist/handler-segments.test.js +0 -217
  140. package/dist/head-tags.test.d.ts +0 -1
  141. package/dist/head-tags.test.js +0 -131
  142. package/dist/highlight-bind.test.d.ts +0 -1
  143. package/dist/highlight-bind.test.js +0 -124
  144. package/dist/highlight.test.d.ts +0 -1
  145. package/dist/highlight.test.js +0 -56
  146. package/dist/lanes.test.d.ts +0 -1
  147. package/dist/lanes.test.js +0 -59
  148. package/dist/list.test.d.ts +0 -1
  149. package/dist/list.test.js +0 -344
  150. package/dist/metadata.test.d.ts +0 -1
  151. package/dist/metadata.test.js +0 -167
  152. package/dist/navigation.test.d.ts +0 -1
  153. package/dist/navigation.test.js +0 -130
  154. package/dist/node-server.test.d.ts +0 -1
  155. package/dist/node-server.test.js +0 -492
  156. package/dist/optimistic-lanes.test.d.ts +0 -1
  157. package/dist/optimistic-lanes.test.js +0 -45
  158. package/dist/patterns.test.d.ts +0 -1
  159. package/dist/patterns.test.js +0 -98
  160. package/dist/portal.test.d.ts +0 -1
  161. package/dist/portal.test.js +0 -99
  162. package/dist/react-effects.test.d.ts +0 -1
  163. package/dist/react-effects.test.js +0 -96
  164. package/dist/react-suspense.test.d.ts +0 -1
  165. package/dist/react-suspense.test.js +0 -194
  166. package/dist/react.test.d.ts +0 -1
  167. package/dist/react.test.js +0 -60
  168. package/dist/react19.test.d.ts +0 -1
  169. package/dist/react19.test.js +0 -176
  170. package/dist/reactivity-core.test.d.ts +0 -1
  171. package/dist/reactivity-core.test.js +0 -152
  172. package/dist/reconcile.test.d.ts +0 -1
  173. package/dist/reconcile.test.js +0 -110
  174. package/dist/refs.test.d.ts +0 -1
  175. package/dist/refs.test.js +0 -257
  176. package/dist/router-layout.test.d.ts +0 -1
  177. package/dist/router-layout.test.js +0 -249
  178. package/dist/router-nav.test.d.ts +0 -1
  179. package/dist/router-nav.test.js +0 -464
  180. package/dist/router-slots.test.d.ts +0 -1
  181. package/dist/router-slots.test.js +0 -144
  182. package/dist/router-speculation.test.d.ts +0 -1
  183. package/dist/router-speculation.test.js +0 -25
  184. package/dist/router-styles.test.d.ts +0 -1
  185. package/dist/router-styles.test.js +0 -98
  186. package/dist/router.test.d.ts +0 -1
  187. package/dist/router.test.js +0 -36
  188. package/dist/server-async.test.d.ts +0 -1
  189. package/dist/server-async.test.js +0 -216
  190. package/dist/server-handler.test.d.ts +0 -1
  191. package/dist/server-handler.test.js +0 -535
  192. package/dist/server-highlight.test.d.ts +0 -1
  193. package/dist/server-highlight.test.js +0 -32
  194. package/dist/server-renderer.test.d.ts +0 -1
  195. package/dist/server-renderer.test.js +0 -266
  196. package/dist/server.test.d.ts +0 -1
  197. package/dist/server.test.js +0 -88
  198. package/dist/sha256.test.d.ts +0 -1
  199. package/dist/sha256.test.js +0 -13
  200. package/dist/signal-lanes.test.d.ts +0 -1
  201. package/dist/signal-lanes.test.js +0 -32
  202. package/dist/signal.test.d.ts +0 -1
  203. package/dist/signal.test.js +0 -68
  204. package/dist/slots.test.d.ts +0 -1
  205. package/dist/slots.test.js +0 -169
  206. package/dist/snapshots.test.d.ts +0 -1
  207. package/dist/snapshots.test.js +0 -208
  208. package/dist/ssr-core-behavior.test.d.ts +0 -1
  209. package/dist/ssr-core-behavior.test.js +0 -620
  210. package/dist/ssr-core.test.d.ts +0 -1
  211. package/dist/ssr-core.test.js +0 -324
  212. package/dist/static-memory.test.d.ts +0 -1
  213. package/dist/static-memory.test.js +0 -26
  214. package/dist/storage.test.d.ts +0 -1
  215. package/dist/storage.test.js +0 -46
  216. package/dist/suspense-actions.test.d.ts +0 -1
  217. package/dist/suspense-actions.test.js +0 -72
  218. package/dist/suspense.test.d.ts +0 -1
  219. package/dist/suspense.test.js +0 -518
@@ -0,0 +1,635 @@
1
+ // Island-preserving DOM swaps for the SPA router.
2
+ //
3
+ // A router swap (a navigation, router.refresh(), the refresh after a server
4
+ // action that revalidated) replaces server-rendered markup with the markup of
5
+ // the new document. Client islands in that markup are not replaced: an island
6
+ // mounted in the live DOM that has a counterpart in the new markup keeps its
7
+ // nodes and its mounted root, and is re-rendered in place with the new server
8
+ // props (state, DOM nodes and focus survive), as React keeps a mounted client
9
+ // component across a refresh. An island the swap really removes is unmounted
10
+ // before its markup goes, so its effects and portals are cleaned up.
11
+ //
12
+ // Islands are the ones the island loader lists in window.__krateIslands: a
13
+ // Preact island is the nodes between <!--krate-island:{m,e,s,p}--> and
14
+ // <!--/krate-island-->, a real-React island a <krate-island data-src> element
15
+ // (props in the JSON script before it). The loader puts each island's record
16
+ // on its open comment or element (__krateIsland); a mounted record carries the
17
+ // handle its chunk's mount returned: update(props, pool), unmount() and, for a
18
+ // Preact island, the server children (slots) it adopted from the document.
19
+ //
20
+ // Keying: islands are matched per container (the swapped region, or one
21
+ // server-children slot of a kept island), outermost islands only, in document
22
+ // order by module and export: the n-th island of a component in the live
23
+ // region pairs with the n-th one in the new markup. A navigation to another
24
+ // pathname only keeps an island whose server props are unchanged too (a
25
+ // layout's island); one whose props differ belongs to the other page and is
26
+ // mounted anew, as React remounts a page's components.
27
+ //
28
+ // The live ancestors of a kept island stay in place as long as the new markup
29
+ // has elements of the same tags on the same path (their attributes follow the
30
+ // new markup), so the island is never detached; everything else outside the
31
+ // islands is replaced by the new markup.
32
+ const OPEN = 'krate-island:';
33
+ const CLOSE = '/krate-island';
34
+ const OPEN_TAG = '<!--' + OPEN;
35
+ const CLOSE_TAG = '<!--' + CLOSE + '-->';
36
+ const SLOT_OPEN = /^krate-slot:([0-9a-z]+)$/;
37
+ function isOpen(n) {
38
+ return n.nodeType === 8 && n.data.indexOf(OPEN) === 0;
39
+ }
40
+ function isClose(n) {
41
+ return n.nodeType === 8 && n.data === CLOSE;
42
+ }
43
+ function isHost(n) {
44
+ return n.nodeType === 1 && n.localName === 'krate-island' && n.hasAttribute('data-src');
45
+ }
46
+ function closeOf(start) {
47
+ let d = 0;
48
+ for (let n = start.nextSibling; n; n = n.nextSibling) {
49
+ if (isOpen(n))
50
+ d++;
51
+ else if (isClose(n)) {
52
+ if (!d)
53
+ return n;
54
+ d--;
55
+ }
56
+ }
57
+ return null;
58
+ }
59
+ function rangeNodes(start, end) {
60
+ const out = [start];
61
+ for (let n = start.nextSibling; n && n !== end; n = n.nextSibling)
62
+ out.push(n);
63
+ out.push(end);
64
+ return out;
65
+ }
66
+ function nodesOf(i) {
67
+ return i.el ? [i.el] : rangeNodes(i.start, i.end);
68
+ }
69
+ function propsText(el) {
70
+ if (el.hasAttribute('data-props'))
71
+ return el.getAttribute('data-props') || '';
72
+ const s = el.previousElementSibling;
73
+ return s && s.localName === 'script' && s.hasAttribute('data-krate-props') ? s.textContent || '' : '';
74
+ }
75
+ /** The outermost islands in list, in document order. */
76
+ function scan(list, chain = [], out = []) {
77
+ for (let i = 0; i < list.length; i++) {
78
+ const n = list[i];
79
+ if (isOpen(n)) {
80
+ const end = closeOf(n);
81
+ let p = null;
82
+ try {
83
+ p = JSON.parse(n.data.slice(OPEN.length));
84
+ }
85
+ catch {
86
+ p = null;
87
+ }
88
+ if (!end || !p)
89
+ continue;
90
+ out.push({ key: p.m + '#' + p.e, start: n, end, json: n.data.slice(OPEN.length), chain, rec: n.__krateIsland });
91
+ while (i < list.length - 1 && list[i] !== end)
92
+ i++;
93
+ }
94
+ else if (isHost(n)) {
95
+ out.push({
96
+ key: n.getAttribute('data-module') + '#' + n.getAttribute('data-export'),
97
+ el: n,
98
+ json: propsText(n),
99
+ chain,
100
+ rec: n.__krateIsland,
101
+ });
102
+ }
103
+ else if (n.nodeType === 1) {
104
+ scan(n.childNodes, chain.concat(n), out);
105
+ }
106
+ }
107
+ return out;
108
+ }
109
+ function mounted(rec) {
110
+ const h = rec && rec.handle;
111
+ return !!rec && !rec.gone && rec.status === 'hydrated' && !!h && typeof h.update === 'function' && typeof h.unmount === 'function';
112
+ }
113
+ /** Calls fn on every slot id referenced in props (server children adopted from the document). */
114
+ function eachSlotRef(v, fn) {
115
+ if (Array.isArray(v))
116
+ return v.map((x) => eachSlotRef(x, fn));
117
+ if (!v || typeof v !== 'object')
118
+ return v;
119
+ const out = {};
120
+ for (const k in v) {
121
+ const x = v[k];
122
+ if (k === '__krateSlotRef' && typeof x === 'string')
123
+ out[k] = fn(x);
124
+ else if (k === 'r' && v.$ === 'h' && typeof x === 'string')
125
+ out[k] = fn(x);
126
+ else
127
+ out[k] = eachSlotRef(x, fn);
128
+ }
129
+ return out;
130
+ }
131
+ function parse(json) {
132
+ try {
133
+ return JSON.parse(json);
134
+ }
135
+ catch {
136
+ return undefined;
137
+ }
138
+ }
139
+ /** The server props of an island, with slot ids (random per render) blanked out. */
140
+ function propsKey(i) {
141
+ const v = parse(i.json);
142
+ return JSON.stringify(eachSlotRef(i.el ? v : v && v.p, () => ''));
143
+ }
144
+ /** Serializes nodes as the island chunks snapshot a slot's server HTML. */
145
+ export function nodesHTML(nodes) {
146
+ let s = '';
147
+ for (let i = 0; i < nodes.length; i++) {
148
+ const n = nodes[i];
149
+ s += n.nodeType === 1 ? n.outerHTML : n.nodeType === 8 ? '<!--' + n.data + '-->' : n.textContent || '';
150
+ }
151
+ return s;
152
+ }
153
+ /** The next real-React island element (one with data-src) in html from i, or -1. */
154
+ function hostAt(html, i) {
155
+ for (let h = html.indexOf('<krate-island ', i); h >= 0; h = html.indexOf('<krate-island ', h + 1)) {
156
+ if (/\sdata-src=/.test(html.slice(h, html.indexOf('>', h))))
157
+ return h;
158
+ }
159
+ return -1;
160
+ }
161
+ /**
162
+ * HTML with every island replaced by a marker naming its component, so two
163
+ * renders compare equal when only the islands (their props, or what they
164
+ * rendered since) differ.
165
+ */
166
+ export function maskIslands(html) {
167
+ let out = '';
168
+ let i = 0;
169
+ for (;;) {
170
+ const a = html.indexOf(OPEN_TAG, i);
171
+ const h = hostAt(html, i);
172
+ if (a === -1 && h < 0)
173
+ return out + html.slice(i);
174
+ if (h >= 0 && (a === -1 || h < a)) {
175
+ const e = html.indexOf('</krate-island>', h);
176
+ if (e === -1)
177
+ return out + html.slice(i);
178
+ out += html.slice(i, h) + '<!--i:' + (/data-module="([^"]*)"/.exec(html.slice(h, html.indexOf('>', h))) || ['', ''])[1] + '-->';
179
+ i = e + 15;
180
+ continue;
181
+ }
182
+ const headEnd = html.indexOf('-->', a);
183
+ if (headEnd === -1)
184
+ return out + html.slice(i);
185
+ const p = parse(html.slice(a + OPEN_TAG.length, headEnd));
186
+ let j = headEnd + 3;
187
+ let d = 0;
188
+ for (;;) {
189
+ const o = html.indexOf(OPEN_TAG, j);
190
+ const c = html.indexOf(CLOSE_TAG, j);
191
+ if (c === -1)
192
+ return out + html.slice(i);
193
+ if (o !== -1 && o < c) {
194
+ d++;
195
+ j = o + OPEN_TAG.length;
196
+ continue;
197
+ }
198
+ j = c + CLOSE_TAG.length;
199
+ if (!d)
200
+ break;
201
+ d--;
202
+ }
203
+ out += html.slice(i, a) + '<!--i:' + (p ? p.m + '#' + p.e : '') + '-->';
204
+ i = j;
205
+ }
206
+ }
207
+ /**
208
+ * Every island record in nodes (nested ones included), in document order. An
209
+ * island's open comment counts with the nodes now between its comments,
210
+ * whether nodes lists them or not: a slot's set lists a nested island as its
211
+ * two comments only.
212
+ */
213
+ function recordsIn(nodes) {
214
+ const out = [];
215
+ const seen = new Set();
216
+ const visit = (n) => {
217
+ if (seen.has(n))
218
+ return;
219
+ seen.add(n);
220
+ const r = n.__krateIsland;
221
+ if (r && (isOpen(n) || isHost(n)))
222
+ out.push(r);
223
+ if (isOpen(n)) {
224
+ const end = closeOf(n);
225
+ if (end)
226
+ for (let x = n.nextSibling; x && x !== end; x = x.nextSibling)
227
+ visit(x);
228
+ // A set out of the document keeps them on the open comment.
229
+ const inner = n.__krateInner;
230
+ if (inner)
231
+ for (const x of inner)
232
+ visit(x);
233
+ return;
234
+ }
235
+ if (n.nodeType !== 1)
236
+ return;
237
+ for (let c = n.firstChild; c; c = c.nextSibling)
238
+ visit(c);
239
+ };
240
+ for (let i = 0; i < nodes.length; i++)
241
+ visit(nodes[i]);
242
+ return out;
243
+ }
244
+ /**
245
+ * Unmounts every island in nodes, innermost first, so their effects and
246
+ * portals are cleaned up before the nodes leave the document. An island still
247
+ * loading is marked gone (the loader unmounts it once its chunk mounted).
248
+ */
249
+ export function unmountIslandsIn(nodes) {
250
+ const recs = recordsIn(nodes);
251
+ for (let i = recs.length - 1; i >= 0; i--) {
252
+ const r = recs[i];
253
+ if (r.gone)
254
+ continue;
255
+ r.gone = true;
256
+ const h = r.handle;
257
+ if (!h || typeof h.unmount !== 'function')
258
+ continue;
259
+ try {
260
+ h.unmount();
261
+ }
262
+ catch (err) {
263
+ console.error(err);
264
+ }
265
+ }
266
+ }
267
+ function syncAttrs(live, next) {
268
+ for (const a of Array.from(live.attributes))
269
+ if (!next.hasAttribute(a.name))
270
+ live.removeAttribute(a.name);
271
+ for (const a of Array.from(next.attributes)) {
272
+ if (live.getAttribute(a.name) === a.value)
273
+ continue;
274
+ if (a.namespaceURI)
275
+ live.setAttributeNS(a.namespaceURI, a.name, a.value);
276
+ else
277
+ live.setAttribute(a.name, a.value);
278
+ }
279
+ }
280
+ function insert(parent, node, ref) {
281
+ // moveBefore keeps a connected node's state (focus, iframes, animations)
282
+ // where the browser supports it.
283
+ const mv = parent.moveBefore;
284
+ if (typeof mv === 'function' && node.isConnected && parent.isConnected && node.parentNode) {
285
+ try {
286
+ mv.call(parent, node, ref);
287
+ return;
288
+ }
289
+ catch {
290
+ /* fall back to a plain insert */
291
+ }
292
+ }
293
+ parent.insertBefore(node, ref);
294
+ }
295
+ /**
296
+ * Makes the children of parent between start and end (exclusive; the whole
297
+ * child list when both are null) the nodes of want, in order: nodes not in
298
+ * want are removed (and disposed), kept nodes stay where they are, the others
299
+ * are inserted around them.
300
+ */
301
+ function place(parent, want, start, end) {
302
+ const keep = new Set(want);
303
+ const dispose = globalThis.disposeNode;
304
+ for (let n = start ? start.nextSibling : parent.firstChild; n && n !== end;) {
305
+ const next = n.nextSibling;
306
+ // A kept node that moves elsewhere stays until it is moved, so it is
307
+ // never detached on the way (moveBefore keeps its focus).
308
+ if (!keep.has(n) && !(pinned && pinned.has(n))) {
309
+ parent.removeChild(n);
310
+ if (typeof dispose === 'function')
311
+ dispose(n);
312
+ }
313
+ n = next;
314
+ }
315
+ let cur = start ? start.nextSibling : parent.firstChild;
316
+ for (const n of want) {
317
+ while (cur && cur !== end && cur !== n && pinned && pinned.has(cur) && !keep.has(cur))
318
+ cur = cur.nextSibling;
319
+ if (n === cur)
320
+ cur = cur.nextSibling;
321
+ else
322
+ insert(parent, n, cur);
323
+ }
324
+ }
325
+ /** Live nodes a swap keeps (islands and their ancestors), while it runs. */
326
+ let pinned = null;
327
+ function withPinned(p, fn) {
328
+ const prev = pinned;
329
+ const set = new Set(prev || []);
330
+ p.pairs.forEach((l) => nodesOf(l).forEach((n) => set.add(n)));
331
+ p.map.forEach((l) => set.add(l));
332
+ pinned = set;
333
+ try {
334
+ fn();
335
+ }
336
+ finally {
337
+ pinned = prev;
338
+ }
339
+ }
340
+ function pairIslands(liveList, newList, allow) {
341
+ const live = scan(liveList);
342
+ const next = scan(newList);
343
+ const pairs = new Map();
344
+ let from = 0;
345
+ for (const n of next) {
346
+ for (let j = from; j < live.length; j++) {
347
+ if (live[j].key !== n.key)
348
+ continue;
349
+ from = j + 1;
350
+ if (mounted(live[j].rec) && (!allow || allow(live[j], n)))
351
+ pairs.set(n, live[j]);
352
+ break;
353
+ }
354
+ }
355
+ const kept = new Set(pairs.values());
356
+ const byStart = new Map();
357
+ pairs.forEach((_, n) => byStart.set(n.el || n.start, n));
358
+ return { pairs, byStart, map: new Map(), carry: new Set(), gone: live.filter((l) => !kept.has(l)), live, next };
359
+ }
360
+ /** Maps the ancestors of each paired new island onto the live ones (same tags, top down). */
361
+ function mapChains(p) {
362
+ const used = new Set(p.map.values());
363
+ p.pairs.forEach((l, n) => {
364
+ let ok = true;
365
+ n.chain.forEach((ne, i) => {
366
+ p.carry.add(ne);
367
+ if (!ok)
368
+ return;
369
+ const le = l.chain[i];
370
+ const cur = p.map.get(ne);
371
+ if (cur) {
372
+ ok = cur === le;
373
+ return;
374
+ }
375
+ if (le && le.localName === ne.localName && !used.has(le)) {
376
+ p.map.set(ne, le);
377
+ used.add(le);
378
+ }
379
+ else
380
+ ok = false;
381
+ });
382
+ });
383
+ }
384
+ /**
385
+ * The nodes that take the place of the new list: kept live islands for their
386
+ * counterparts, kept live ancestors (whose children are updated) for the new
387
+ * elements mapped onto them, and the new nodes otherwise.
388
+ */
389
+ function resolve(p, list) {
390
+ const out = [];
391
+ for (let i = 0; i < list.length; i++) {
392
+ const n = list[i];
393
+ const isl = p.byStart.get(n);
394
+ if (isl) {
395
+ out.push(...nodesOf(p.pairs.get(isl)));
396
+ if (isl.end)
397
+ while (i < list.length - 1 && list[i] !== isl.end)
398
+ i++;
399
+ continue;
400
+ }
401
+ const live = p.map.get(n);
402
+ if (live) {
403
+ syncAttrs(live, n);
404
+ place(live, resolve(p, Array.from(n.childNodes)), null, null);
405
+ out.push(live);
406
+ continue;
407
+ }
408
+ if (p.carry.has(n))
409
+ place(n, resolve(p, Array.from(n.childNodes)), null, null);
410
+ out.push(n);
411
+ }
412
+ return out;
413
+ }
414
+ /** Whether every island on both sides is paired (nothing to mount or unmount). */
415
+ function allPaired(p) {
416
+ return p.gone.length === 0 && p.pairs.size === p.next.length;
417
+ }
418
+ /**
419
+ * Replaces the live children of parent between start and end (exclusive;
420
+ * all children when both are null) with newNodes (imported into the
421
+ * document, detached), keeping matched islands mounted. Returns the pending
422
+ * work of island updates (a re-render that loads more chunks first).
423
+ */
424
+ export function swapPreservingIslands(parent, start, end, newNodes, opts = {}) {
425
+ const liveNodes = [];
426
+ for (let n = start ? start.nextSibling : parent.firstChild; n && n !== end; n = n.nextSibling)
427
+ liveNodes.push(n);
428
+ const jobs = [];
429
+ const p = pairIslands(liveNodes, newNodes, opts.sameProps ? (l, n) => propsKey(l) === propsKey(n) : null);
430
+ if (p.pairs.size > 0 && allPaired(p) && maskIslands(nodesHTML(liveNodes)) === maskIslands(nodesHTML(newNodes))) {
431
+ // Only the islands differ: the rest of the live DOM is left untouched.
432
+ p.pairs.forEach((l, n) => updateIsland(l, n, jobs));
433
+ return jobs;
434
+ }
435
+ for (const g of p.gone)
436
+ unmountIslandsIn(nodesOf(g));
437
+ mapChains(p);
438
+ withPinned(p, () => place(parent, resolve(p, newNodes), start, end));
439
+ p.pairs.forEach((l, n) => updateIsland(l, n, jobs));
440
+ return jobs;
441
+ }
442
+ /** The server-children slots directly in a new island's markup, in document order. */
443
+ function slotsOf(i) {
444
+ const out = [];
445
+ let depth = 0;
446
+ let inner = [];
447
+ const see = (c) => {
448
+ if (isOpen(c))
449
+ return void depth++;
450
+ if (isClose(c))
451
+ return void depth--;
452
+ if (depth || c.nodeType !== 8)
453
+ return;
454
+ const m = SLOT_OPEN.exec(c.data);
455
+ if (!m)
456
+ return;
457
+ // A slot inside a real-React island's element belongs to that island.
458
+ const host = c.parentElement && c.parentElement.closest('krate-island');
459
+ if (host && inner.some((x) => x === host || x.contains(host)))
460
+ return;
461
+ const close = '/krate-slot:' + m[1];
462
+ const nodes = [];
463
+ let n = c.nextSibling;
464
+ for (; n && !(n.nodeType === 8 && n.data === close); n = n.nextSibling)
465
+ nodes.push(n);
466
+ if (n)
467
+ out.push({ id: m[1], nodes });
468
+ };
469
+ inner = rangeNodes(i.start, i.end).slice(1, -1);
470
+ for (const n of inner) {
471
+ if (n.nodeType === 8) {
472
+ see(n);
473
+ continue;
474
+ }
475
+ if (n.nodeType !== 1)
476
+ continue;
477
+ const w = (n.ownerDocument || document).createTreeWalker(n, 128 /* SHOW_COMMENT */);
478
+ for (let c = w.nextNode(); c; c = w.nextNode())
479
+ see(c);
480
+ }
481
+ return out;
482
+ }
483
+ /**
484
+ * Brings a kept server-children slot up to date with the new markup without
485
+ * replacing its top-level nodes (the island renders them): text and comments
486
+ * take the new data, elements keep their identity and get the new attributes
487
+ * and children, nested islands are kept. Returns false (changing nothing)
488
+ * when the top-level shapes differ.
489
+ */
490
+ function morphSlot(set, nodes, jobs) {
491
+ const p = pairIslands(set, nodes, null);
492
+ const units = (list, isl) => {
493
+ const top = new Map();
494
+ for (const x of isl)
495
+ if (x.chain.length === 0)
496
+ top.set(x.el || x.start, x);
497
+ const out = [];
498
+ for (let i = 0; i < list.length; i++) {
499
+ const x = top.get(list[i]);
500
+ out.push({ node: list[i], isl: x });
501
+ if (x && x.end)
502
+ while (i < list.length - 1 && list[i] !== x.end)
503
+ i++;
504
+ }
505
+ return out;
506
+ };
507
+ const a = units(set, p.live);
508
+ const b = units(nodes, p.next);
509
+ if (a.length !== b.length)
510
+ return false;
511
+ for (let i = 0; i < a.length; i++) {
512
+ const x = a[i];
513
+ const y = b[i];
514
+ if (x.isl || y.isl) {
515
+ if (!x.isl || !y.isl || p.pairs.get(y.isl) !== x.isl)
516
+ return false;
517
+ }
518
+ else if (x.node.nodeType !== y.node.nodeType || (x.node.nodeType === 1 && x.node.localName !== y.node.localName)) {
519
+ return false;
520
+ }
521
+ }
522
+ for (const g of p.gone)
523
+ unmountIslandsIn(nodesOf(g));
524
+ for (let i = 0; i < a.length; i++)
525
+ if (!a[i].isl && a[i].node.nodeType === 1)
526
+ p.map.set(b[i].node, a[i].node);
527
+ mapChains(p);
528
+ withPinned(p, () => {
529
+ for (let i = 0; i < a.length; i++) {
530
+ const x = a[i].node;
531
+ const y = b[i].node;
532
+ if (a[i].isl)
533
+ continue;
534
+ if (x.nodeType === 1) {
535
+ syncAttrs(x, y);
536
+ place(x, resolve(p, Array.from(y.childNodes)), null, null);
537
+ }
538
+ else if (x.data !== y.data) {
539
+ x.data = y.data;
540
+ }
541
+ }
542
+ });
543
+ p.pairs.forEach((l, n) => updateIsland(l, n, jobs));
544
+ return true;
545
+ }
546
+ function escapeComment(json) {
547
+ return json.replace(/</g, '\\u003c').replace(/>/g, '\\u003e').replace(/--/g, '-\\u002d');
548
+ }
549
+ /**
550
+ * Brings a kept island up to date with its counterpart in the new markup:
551
+ * its server-children slots follow the new server HTML (in place where they
552
+ * can), and it re-renders with the new props when they changed.
553
+ */
554
+ function updateIsland(l, n, jobs) {
555
+ const h = l.rec.handle;
556
+ const raw = parse(n.json);
557
+ if (raw === undefined)
558
+ return;
559
+ if (l.el) {
560
+ if (n.json === l.json)
561
+ return;
562
+ try {
563
+ const r = h.update(raw);
564
+ if (r && typeof r.then === 'function')
565
+ jobs.push(r.catch((e) => console.error(e)));
566
+ }
567
+ catch (err) {
568
+ console.error(err);
569
+ }
570
+ return;
571
+ }
572
+ const rename = {};
573
+ const pool = {};
574
+ let retaken = false;
575
+ const olds = h.slots || [];
576
+ const news = slotsOf(n);
577
+ const slots = [];
578
+ news.forEach((s, i) => {
579
+ const o = olds.length === news.length ? olds[i] : undefined;
580
+ const html = nodesHTML(s.nodes);
581
+ if (o) {
582
+ if (maskIslands(html) === maskIslands(o.html)) {
583
+ const q = pairIslands(o.set, s.nodes, null);
584
+ if (allPaired(q)) {
585
+ q.pairs.forEach((ll, nn) => updateIsland(ll, nn, jobs));
586
+ rename[s.id] = o.id;
587
+ slots.push(o);
588
+ return;
589
+ }
590
+ }
591
+ if (morphSlot(o.set, s.nodes, jobs)) {
592
+ rename[s.id] = o.id;
593
+ o.html = html;
594
+ slots.push(o);
595
+ return;
596
+ }
597
+ }
598
+ // The slot is rendered anew from the new nodes: the islands in its old
599
+ // content go with it.
600
+ if (o)
601
+ unmountIslandsIn(o.set);
602
+ pool[s.id] = [s.nodes];
603
+ retaken = true;
604
+ slots.push({ id: s.id, html, set: s.nodes });
605
+ });
606
+ if (olds.length !== news.length)
607
+ for (const o of olds)
608
+ unmountIslandsIn(o.set);
609
+ h.slots = slots;
610
+ const props = eachSlotRef(raw.p || {}, (id) => rename[id] || id);
611
+ const old = parse(l.json);
612
+ const changed = retaken || JSON.stringify(props) !== JSON.stringify(old ? old.p || {} : null);
613
+ raw.p = props;
614
+ l.start.data = OPEN + escapeComment(JSON.stringify(raw));
615
+ // Nested islands in its slots may have replaced their nodes: the sets
616
+ // follow the document before the island renders again.
617
+ if (typeof h.sync === 'function') {
618
+ try {
619
+ h.sync();
620
+ }
621
+ catch (err) {
622
+ console.error(err);
623
+ }
624
+ }
625
+ if (!changed)
626
+ return;
627
+ try {
628
+ const r = retaken ? h.update(props, pool) : h.update(props);
629
+ if (r && typeof r.then === 'function')
630
+ jobs.push(r.catch((e) => console.error(e)));
631
+ }
632
+ catch (err) {
633
+ console.error(err);
634
+ }
635
+ }
@@ -1,4 +1,4 @@
1
- import { type Component } from './dom.js';
1
+ import type { Component } from './dom.js';
2
2
  export { jsx, jsxs, Fragment } from './jsx-runtime.js';
3
3
  export type { Component } from './dom.js';
4
4
  export declare function jsxDEV<P extends {}>(type: string | Component<P>, props: P | null, key?: string | null, _isStaticChildren?: boolean, _source?: {