@ivlev-dev/silent-core 0.5.0 → 0.7.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 (103) hide show
  1. package/dist/component-lite.d.ts +14 -0
  2. package/dist/component-lite.d.ts.map +1 -0
  3. package/dist/component-lite.js +138 -0
  4. package/dist/component-lite.js.map +1 -0
  5. package/dist/component-state.d.ts +42 -0
  6. package/dist/component-state.d.ts.map +1 -0
  7. package/dist/component-state.js +98 -0
  8. package/dist/component-state.js.map +1 -0
  9. package/dist/component-view.d.ts +5 -0
  10. package/dist/component-view.d.ts.map +1 -0
  11. package/dist/component-view.js +445 -0
  12. package/dist/component-view.js.map +1 -0
  13. package/dist/component.d.ts +4 -35
  14. package/dist/component.d.ts.map +1 -1
  15. package/dist/component.js +5 -588
  16. package/dist/component.js.map +1 -1
  17. package/dist/control-flow.d.ts.map +1 -1
  18. package/dist/control-flow.js +447 -161
  19. package/dist/control-flow.js.map +1 -1
  20. package/dist/decorators.d.ts.map +1 -1
  21. package/dist/decorators.js +4 -1
  22. package/dist/decorators.js.map +1 -1
  23. package/dist/dom-hydration-state.d.ts +21 -0
  24. package/dist/dom-hydration-state.d.ts.map +1 -0
  25. package/dist/dom-hydration-state.js +30 -0
  26. package/dist/dom-hydration-state.js.map +1 -0
  27. package/dist/dom-hydration.d.ts +10 -0
  28. package/dist/dom-hydration.d.ts.map +1 -0
  29. package/dist/dom-hydration.js +537 -0
  30. package/dist/dom-hydration.js.map +1 -0
  31. package/dist/dom.d.ts +35 -5
  32. package/dist/dom.d.ts.map +1 -1
  33. package/dist/dom.js +809 -285
  34. package/dist/dom.js.map +1 -1
  35. package/dist/error-boundary.d.ts.map +1 -1
  36. package/dist/error-boundary.js +68 -20
  37. package/dist/error-boundary.js.map +1 -1
  38. package/dist/hydrate.d.ts +4 -0
  39. package/dist/hydrate.d.ts.map +1 -0
  40. package/dist/hydrate.js +131 -0
  41. package/dist/hydrate.js.map +1 -0
  42. package/dist/hydration-proxy.d.ts +14 -0
  43. package/dist/hydration-proxy.d.ts.map +1 -0
  44. package/dist/hydration-proxy.js +40 -0
  45. package/dist/hydration-proxy.js.map +1 -0
  46. package/dist/hydration-state.d.ts +47 -0
  47. package/dist/hydration-state.d.ts.map +1 -0
  48. package/dist/hydration-state.js +83 -0
  49. package/dist/hydration-state.js.map +1 -0
  50. package/dist/hydration.d.ts +7 -32
  51. package/dist/hydration.d.ts.map +1 -1
  52. package/dist/hydration.js +299 -123
  53. package/dist/hydration.js.map +1 -1
  54. package/dist/index-list.d.ts.map +1 -1
  55. package/dist/index-list.js +249 -63
  56. package/dist/index-list.js.map +1 -1
  57. package/dist/index.d.ts +3 -1
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js +3 -1
  60. package/dist/index.js.map +1 -1
  61. package/dist/jsx-runtime.d.ts +1 -1
  62. package/dist/jsx-runtime.d.ts.map +1 -1
  63. package/dist/jsx-runtime.js +4 -3
  64. package/dist/jsx-runtime.js.map +1 -1
  65. package/dist/metadata.d.ts +1 -0
  66. package/dist/metadata.d.ts.map +1 -1
  67. package/dist/metadata.js +29 -11
  68. package/dist/metadata.js.map +1 -1
  69. package/dist/mount-core.d.ts +14 -0
  70. package/dist/mount-core.d.ts.map +1 -0
  71. package/dist/mount-core.js +64 -0
  72. package/dist/mount-core.js.map +1 -0
  73. package/dist/mount.d.ts +1 -2
  74. package/dist/mount.d.ts.map +1 -1
  75. package/dist/mount.js +17 -132
  76. package/dist/mount.js.map +1 -1
  77. package/dist/server-backend.d.ts +23 -0
  78. package/dist/server-backend.d.ts.map +1 -0
  79. package/dist/server-backend.js +25 -0
  80. package/dist/server-backend.js.map +1 -0
  81. package/dist/server-hole.d.ts +3 -0
  82. package/dist/server-hole.d.ts.map +1 -0
  83. package/dist/server-hole.js +12 -0
  84. package/dist/server-hole.js.map +1 -0
  85. package/dist/server-proxy.d.ts +6 -0
  86. package/dist/server-proxy.d.ts.map +1 -0
  87. package/dist/server-proxy.js +14 -0
  88. package/dist/server-proxy.js.map +1 -0
  89. package/dist/server-state.d.ts +10 -0
  90. package/dist/server-state.d.ts.map +1 -0
  91. package/dist/server-state.js +14 -0
  92. package/dist/server-state.js.map +1 -0
  93. package/dist/static-effect.d.ts +4 -0
  94. package/dist/static-effect.d.ts.map +1 -0
  95. package/dist/static-effect.js +11 -0
  96. package/dist/static-effect.js.map +1 -0
  97. package/dist/suspense.d.ts.map +1 -1
  98. package/dist/suspense.js +4 -5
  99. package/dist/suspense.js.map +1 -1
  100. package/dist/switch.d.ts.map +1 -1
  101. package/dist/switch.js +37 -33
  102. package/dist/switch.js.map +1 -1
  103. package/package.json +4 -4
@@ -1,7 +1,13 @@
1
- import { createRenderEffect, createRoot, createSignal, getOwner, onCleanup, runWithOwner, untrack, } from "@ivlev-dev/silent-reactivity";
2
- import { attachDisposer, collectNodes, createMountPoint, disposeNode, settleNodes, } from "./dom.js";
3
- import { extractMetadata } from "./metadata.js";
4
- import { ITEM, LIST_END, SHOW, collectBetween, currentWindow, isHydrating, isItemMarker, isMarker, mismatch, withWindow, } from "./hydration.js";
1
+ import { createRenderEffect, createSignal, getOwner, onCleanup, runWithOwner, untrack, } from "@ivlev-dev/silent-reactivity";
2
+ import { createEffectUnlessStatic } from "./static-effect.js";
3
+ import { attachDisposer, attachItemDisposer, clearItemDisposer, collectNodes, createMountPoint, disposeNode, isServerMode, } from "./dom.js";
4
+ import { hydratedEffect, settleNodes } from "./dom-hydration-state.js";
5
+ import { getServerBackend, serializeServerValue } from "./server-proxy.js";
6
+ import { extractMetadata, extractMetadataNode, isMetadataFree } from "./metadata.js";
7
+ import { ITEM, LIST_END, SHOW, collectBetween, currentWindow, isHydrating, isMarker, mismatch, withWindow, } from "./hydration-proxy.js";
8
+ const SERVER = typeof __SILENT_SERVER__ === "undefined" || __SILENT_SERVER__;
9
+ const noopCleanup = () => { };
10
+ const NO_NODES = [];
5
11
  function truthy(value) {
6
12
  return value !== null && value !== undefined && value !== false && value !== 0 && value !== "";
7
13
  }
@@ -12,90 +18,273 @@ function resolve(value) {
12
18
  }
13
19
  return current;
14
20
  }
15
- function precedes(block, reference) {
21
+ function precedesBlock(nodes, anchor, reference) {
16
22
  let cursor = reference.previousSibling;
17
- for (let index = block.length - 1; index >= 0; index -= 1) {
18
- const node = block[index];
19
- if (node === undefined || cursor === null || node !== cursor) {
23
+ for (let index = nodes.length - 1; index >= 0; index -= 1) {
24
+ if (cursor === null || nodes[index] !== cursor) {
20
25
  return false;
21
26
  }
22
27
  cursor = cursor.previousSibling;
23
28
  }
24
- return true;
29
+ return cursor === anchor;
25
30
  }
26
- function longestIncreasingSubsequence(values) {
27
- const tails = [];
28
- const previous = new Array(values.length).fill(-1);
29
- for (let index = 0; index < values.length; index += 1) {
30
- const value = values[index];
31
+ function markStableSlots(positions, slots, count, stable, tails, previous) {
32
+ let length = 0;
33
+ for (let index = 0; index < count; index += 1) {
34
+ const value = positions[index];
31
35
  let low = 0;
32
- let high = tails.length;
36
+ let high = length;
33
37
  while (low < high) {
34
38
  const middle = (low + high) >> 1;
35
- if (values[tails[middle]] < value) {
39
+ if (positions[tails[middle]] < value) {
36
40
  low = middle + 1;
37
41
  }
38
42
  else {
39
43
  high = middle;
40
44
  }
41
45
  }
42
- if (low > 0) {
43
- previous[index] = tails[low - 1];
44
- }
46
+ previous[index] = low > 0 ? tails[low - 1] + 1 : 0;
45
47
  tails[low] = index;
48
+ if (low + 1 > length) {
49
+ length = low + 1;
50
+ }
51
+ }
52
+ let cursor = length === 0 ? 0 : tails[length - 1] + 1;
53
+ while (cursor !== 0) {
54
+ const index = cursor - 1;
55
+ stable[slots[index]] = 1;
56
+ cursor = previous[index];
57
+ }
58
+ }
59
+ const ITEM_MARKER = `<!--${ITEM}-->`;
60
+ const LIST_END_MARKER = `<!--${LIST_END}-->`;
61
+ function renderForServer(props) {
62
+ const fallback = props.fallback;
63
+ const hasFallback = fallback !== undefined;
64
+ const items = typeof props.each === "function" ? props.each() : props.each;
65
+ const backend = getServerBackend();
66
+ if (backend?.createRaw !== undefined) {
67
+ if (items.length === 0) {
68
+ const out = hasFallback
69
+ ? serializeServerValue(resolve(fallback)) + LIST_END_MARKER
70
+ : LIST_END_MARKER;
71
+ return backend.createRaw(out);
72
+ }
73
+ let out = "";
74
+ let currentIndex = 0;
75
+ const indexOf = () => currentIndex;
76
+ for (let position = 0; position < items.length; position += 1) {
77
+ const item = items[position];
78
+ if (item === undefined) {
79
+ break;
80
+ }
81
+ currentIndex = position;
82
+ out += ITEM_MARKER;
83
+ out += serializeServerValue(props.children(item, indexOf));
84
+ }
85
+ out += LIST_END_MARKER;
86
+ return backend.createRaw(out);
87
+ }
88
+ if (backend?.createFragment !== undefined) {
89
+ const total = items.length;
90
+ const values = new Array(total);
91
+ let currentIndex = 0;
92
+ const indexOf = () => currentIndex;
93
+ let count = 0;
94
+ for (let position = 0; position < total; position += 1) {
95
+ const item = items[position];
96
+ if (item === undefined) {
97
+ break;
98
+ }
99
+ currentIndex = position;
100
+ values[position] = props.children(item, indexOf);
101
+ count = position + 1;
102
+ }
103
+ values.length = count;
104
+ let parts;
105
+ if (total === 0) {
106
+ if (hasFallback) {
107
+ values.push(resolve(fallback));
108
+ parts = ["", LIST_END_MARKER];
109
+ }
110
+ else {
111
+ parts = [LIST_END_MARKER];
112
+ }
113
+ }
114
+ else if (count === 0) {
115
+ parts = [ITEM_MARKER];
116
+ }
117
+ else {
118
+ parts = new Array(count + 1);
119
+ parts[0] = ITEM_MARKER;
120
+ if (count > 1) {
121
+ parts.fill(ITEM_MARKER, 1, count);
122
+ }
123
+ parts[count] = LIST_END_MARKER;
124
+ }
125
+ const fragment = backend.createFragment(parts, values);
126
+ if (fragment !== null) {
127
+ return fragment.node;
128
+ }
129
+ }
130
+ const endAnchor = document.createComment(LIST_END);
131
+ const sink = document.createDocumentFragment();
132
+ sink.appendChild(endAnchor);
133
+ let fallbackIndex = 0;
134
+ const indexOf = () => fallbackIndex;
135
+ for (let position = 0; position < items.length; position += 1) {
136
+ const item = items[position];
137
+ if (item === undefined) {
138
+ break;
139
+ }
140
+ const anchor = document.createComment(ITEM);
141
+ sink.insertBefore(anchor, endAnchor);
142
+ fallbackIndex = position;
143
+ const value = props.children(item, indexOf);
144
+ if (value instanceof Node && !(value instanceof DocumentFragment) && isMetadataFree(value)) {
145
+ sink.insertBefore(value, endAnchor);
146
+ continue;
147
+ }
148
+ const nodes = extractMetadata(collectNodes(value));
149
+ for (const node of nodes) {
150
+ sink.insertBefore(node, endAnchor);
151
+ }
46
152
  }
47
- const result = new Set();
48
- let cursor = tails.length === 0 ? -1 : tails[tails.length - 1];
49
- while (cursor !== -1) {
50
- result.add(cursor);
51
- cursor = previous[cursor];
153
+ if (items.length === 0 && hasFallback) {
154
+ const nodes = extractMetadata(collectNodes(resolve(fallback)));
155
+ for (const node of nodes) {
156
+ sink.insertBefore(node, endAnchor);
157
+ }
52
158
  }
53
- return result;
159
+ return sink;
54
160
  }
55
161
  export function For(props) {
162
+ if (SERVER && isServerMode()) {
163
+ return renderForServer(props);
164
+ }
56
165
  const hydrating = isHydrating();
57
166
  const outer = hydrating ? currentWindow() : null;
58
- const container = document.createDocumentFragment();
167
+ let container = null;
59
168
  const fallback = props.fallback;
60
- const parentOwner = getOwner();
169
+ const hasFallback = fallback !== undefined;
61
170
  let states = [];
171
+ let spare = [];
172
+ const createdBuffer = [];
173
+ let generation = 0;
62
174
  let endAnchor;
175
+ let positions = null;
176
+ let slots = null;
177
+ let stable = null;
178
+ let lisTails = null;
179
+ let lisPrevious = null;
180
+ const ensureBuffers = (reused, length) => {
181
+ if (positions === null || slots === null || positions.length < reused) {
182
+ const capacity = reused * 2;
183
+ positions = new Int32Array(capacity);
184
+ slots = new Int32Array(capacity);
185
+ }
186
+ if (stable === null || stable.length < length) {
187
+ stable = new Uint8Array(length * 2);
188
+ }
189
+ else {
190
+ stable.fill(0);
191
+ }
192
+ if (lisTails === null || lisPrevious === null || lisTails.length < reused) {
193
+ const capacity = reused * 2;
194
+ lisTails = new Int32Array(capacity);
195
+ lisPrevious = new Int32Array(capacity);
196
+ }
197
+ return [positions, slots, stable, lisTails, lisPrevious];
198
+ };
63
199
  const detachNode = (node) => {
64
200
  if (node.parentNode !== null) {
65
201
  node.parentNode.removeChild(node);
66
202
  }
67
203
  };
204
+ const materializeNodes = (state) => {
205
+ const nodes = state.nodes;
206
+ if (nodes !== NO_NODES || state.nodesEnd === null) {
207
+ return nodes;
208
+ }
209
+ const collected = [];
210
+ let cursor = state.anchor === null ? null : state.anchor.nextSibling;
211
+ while (cursor !== null && cursor !== state.nodesEnd) {
212
+ collected.push(cursor);
213
+ cursor = cursor.nextSibling;
214
+ }
215
+ state.nodes = collected;
216
+ return collected;
217
+ };
218
+ const applyItemDisposer = (state, node) => {
219
+ attachItemDisposer(node, () => removeState(state));
220
+ };
68
221
  const renderItem = (state, value, mountParent = null) => {
69
- for (const node of state.nodes) {
70
- disposeNode(node);
71
- detachNode(node);
222
+ const nodes = materializeNodes(state);
223
+ let tail = null;
224
+ let parent = mountParent;
225
+ if (nodes.length > 0) {
226
+ tail = nodes[nodes.length - 1].nextSibling;
227
+ parent ??= nodes[0].parentNode;
228
+ clearItemDisposer(nodes[0]);
229
+ for (let index = 0; index < nodes.length; index += 1) {
230
+ const node = nodes[index];
231
+ disposeNode(node);
232
+ detachNode(node);
233
+ }
234
+ }
235
+ else if (state.anchor !== null) {
236
+ tail = state.anchor.nextSibling;
237
+ parent ??= state.anchor.parentNode;
72
238
  }
73
239
  state.nodes = [];
74
- const parent = mountParent ?? state.anchor.parentNode;
240
+ state.nodesEnd = null;
75
241
  if (parent === null) {
76
242
  return;
77
243
  }
78
- const nodes = value instanceof Node && !(value instanceof DocumentFragment)
79
- ? [value]
80
- : collectNodes(value);
81
- const reference = state.anchor.nextSibling;
82
- const placed = extractMetadata(nodes);
83
- for (const node of placed) {
84
- parent.insertBefore(node, reference);
244
+ let placed;
245
+ if (value instanceof Node && !(value instanceof DocumentFragment)) {
246
+ const kept = extractMetadataNode(value);
247
+ placed = kept === null ? [] : [kept];
248
+ }
249
+ else {
250
+ placed = extractMetadata(collectNodes(value));
251
+ }
252
+ if (placed.length === 0) {
253
+ if (state.anchor === null) {
254
+ const anchor = document.createComment(ITEM);
255
+ if (tail === null) {
256
+ parent.appendChild(anchor);
257
+ }
258
+ else {
259
+ parent.insertBefore(anchor, tail);
260
+ }
261
+ state.anchor = anchor;
262
+ applyItemDisposer(state, anchor);
263
+ }
264
+ return;
265
+ }
266
+ for (let index = 0; index < placed.length; index += 1) {
267
+ parent.insertBefore(placed[index], tail);
85
268
  }
86
269
  state.nodes = placed;
270
+ applyItemDisposer(state, placed[0]);
87
271
  };
88
272
  const removeState = (state) => {
89
273
  if (state.disposed) {
90
274
  return;
91
275
  }
92
276
  state.disposed = true;
93
- for (const node of state.nodes) {
277
+ const nodes = materializeNodes(state);
278
+ for (let index = 0; index < nodes.length; index += 1) {
279
+ const node = nodes[index];
94
280
  disposeNode(node);
95
281
  detachNode(node);
96
282
  }
97
283
  state.nodes = [];
98
- detachNode(state.anchor);
284
+ state.nodesEnd = null;
285
+ if (state.anchor !== null) {
286
+ detachNode(state.anchor);
287
+ }
99
288
  state.cleanup();
100
289
  };
101
290
  const removeAll = () => {
@@ -103,9 +292,30 @@ export function For(props) {
103
292
  removeState(state);
104
293
  }
105
294
  states = [];
295
+ spare = [];
296
+ createdBuffer.length = 0;
106
297
  fallbackMount.clear();
107
298
  };
108
- const makeState = (item, anchor, nodes, itemEnd, position, mountParent = null) => {
299
+ const ensureAnchors = (list) => {
300
+ for (const state of list) {
301
+ if (state.anchor !== null) {
302
+ continue;
303
+ }
304
+ const first = state.nodes[0];
305
+ if (first === undefined) {
306
+ continue;
307
+ }
308
+ const parent = first.parentNode;
309
+ if (parent === null) {
310
+ continue;
311
+ }
312
+ const anchor = document.createComment(ITEM);
313
+ parent.insertBefore(anchor, first);
314
+ state.anchor = anchor;
315
+ applyItemDisposer(state, anchor);
316
+ }
317
+ };
318
+ const makeState = (item, anchor, itemEnd, position, mountParent = null) => {
109
319
  let handle = null;
110
320
  let current = position;
111
321
  let initialMount = mountParent;
@@ -128,44 +338,45 @@ export function For(props) {
128
338
  index,
129
339
  setIndex,
130
340
  anchor,
131
- nodes,
341
+ nodes: itemEnd === null ? [] : NO_NODES,
342
+ nodesEnd: itemEnd,
132
343
  cleanup: () => { },
133
344
  disposed: false,
345
+ previousIndex: -1,
346
+ generation: -1,
134
347
  };
135
348
  let seeded = itemEnd !== null;
136
- state.cleanup = runWithOwner(parentOwner, () => createRoot((dispose) => {
137
- createRenderEffect(() => {
138
- if (seeded) {
139
- seeded = false;
140
- const window = {
141
- start: anchor,
142
- end: itemEnd,
143
- cursor: anchor.nextSibling,
144
- };
145
- const value = withWindow(window, () => props.children(item, index));
146
- settleNodes(itemEnd, value);
147
- if (window.cursor !== itemEnd) {
148
- mismatch("содержимое элемента списка не совпало");
149
- }
150
- return;
349
+ const run = () => {
350
+ if (seeded) {
351
+ seeded = false;
352
+ const start = anchor;
353
+ const window = {
354
+ start,
355
+ end: itemEnd,
356
+ cursor: start.nextSibling,
357
+ };
358
+ const value = withWindow(window, props.children, item, index);
359
+ settleNodes(itemEnd, value);
360
+ if (window.cursor !== itemEnd) {
361
+ mismatch("содержимое элемента списка не совпало");
151
362
  }
152
- renderItem(state, props.children(item, index), initialMount);
153
- initialMount = null;
154
- });
155
- return dispose;
156
- }));
157
- attachDisposer(anchor, () => removeState(state));
158
- return state;
159
- };
160
- const createState = (item, position, sink) => {
161
- const anchor = document.createComment(ITEM);
162
- const fragment = document.createDocumentFragment();
163
- fragment.appendChild(anchor);
164
- const state = makeState(item, anchor, [], null, position, fragment);
165
- sink.appendChild(fragment);
363
+ return;
364
+ }
365
+ renderItem(state, props.children(item, index), initialMount);
366
+ initialMount = null;
367
+ };
368
+ state.cleanup = hydrating
369
+ ? hydratedEffect(run) ?? noopCleanup
370
+ : createEffectUnlessStatic(run) ?? noopCleanup;
371
+ if (anchor !== null) {
372
+ attachDisposer(anchor, () => removeState(state));
373
+ }
166
374
  return state;
167
375
  };
376
+ const createState = (item, position, sink) => makeState(item, null, null, position, sink);
168
377
  let fallbackSeed = [];
378
+ let skipFirst = hydrating;
379
+ const parentOwner = getOwner();
169
380
  if (hydrating) {
170
381
  if (outer === null) {
171
382
  return mismatch("For вне окна гидрации");
@@ -177,7 +388,7 @@ export function For(props) {
177
388
  endAnchor = candidate;
178
389
  const items = untrack(() => typeof props.each === "function" ? props.each() : props.each);
179
390
  if (items.length === 0) {
180
- if (fallback !== undefined) {
391
+ if (hasFallback) {
181
392
  const window = {
182
393
  start: outer.start,
183
394
  end: endAnchor,
@@ -192,32 +403,36 @@ export function For(props) {
192
403
  }
193
404
  let cursor = items.length === 0 ? endAnchor : outer.start.nextSibling;
194
405
  let position = 0;
195
- while (cursor !== null && cursor !== endAnchor) {
196
- if (!isItemMarker(cursor)) {
197
- return mismatch("ожидался маркер элемента списка");
198
- }
199
- const anchor = cursor;
200
- const nodes = [];
201
- let node = cursor.nextSibling;
202
- while (node !== null && node !== endAnchor && !isItemMarker(node)) {
203
- nodes.push(node);
204
- node = node.nextSibling;
205
- }
206
- const item = items[position];
207
- if (item === undefined) {
208
- return mismatch("лишний элемент списка");
406
+ runWithOwner(parentOwner, () => {
407
+ while (cursor !== null && cursor !== endAnchor) {
408
+ if (cursor.nodeType !== Node.COMMENT_NODE || cursor.data !== ITEM) {
409
+ return mismatch("ожидался маркер элемента списка");
410
+ }
411
+ const anchor = cursor;
412
+ let node = cursor.nextSibling;
413
+ while (node !== null && node !== endAnchor) {
414
+ if (node.nodeType === Node.COMMENT_NODE && node.data === ITEM) {
415
+ break;
416
+ }
417
+ node = node.nextSibling;
418
+ }
419
+ const item = items[position];
420
+ if (item === undefined) {
421
+ return mismatch("лишний элемент списка");
422
+ }
423
+ states.push(makeState(item, anchor, node ?? endAnchor, position));
424
+ position += 1;
425
+ cursor = node;
209
426
  }
210
- states.push(makeState(item, anchor, nodes, node ?? endAnchor, position));
211
- position += 1;
212
- cursor = node;
213
- }
427
+ });
214
428
  if (position !== items.length) {
215
429
  return mismatch("число элементов списка не совпало");
216
430
  }
217
431
  }
218
432
  else {
219
433
  endAnchor = document.createComment(LIST_END);
220
- container.appendChild(endAnchor);
434
+ const sink = container ?? (container = document.createDocumentFragment());
435
+ sink.appendChild(endAnchor);
221
436
  }
222
437
  const fallbackParent = hydrating ? endAnchor.parentNode : null;
223
438
  if (hydrating && fallbackParent === null) {
@@ -225,72 +440,121 @@ export function For(props) {
225
440
  }
226
441
  const fallbackMount = hydrating
227
442
  ? createMountPoint(fallbackParent, endAnchor, fallbackSeed)
228
- : createMountPoint(container, endAnchor);
229
- const skipFirst = { value: hydrating };
443
+ : createMountPoint(container ?? (container = document.createDocumentFragment()), endAnchor);
230
444
  createRenderEffect(() => {
231
445
  const parent = endAnchor.parentNode;
232
446
  if (parent === null) {
233
447
  removeAll();
234
448
  return;
235
449
  }
236
- const { each } = props;
450
+ const each = props.each;
237
451
  const items = typeof each === "function" ? each() : each;
238
- if (skipFirst.value) {
239
- skipFirst.value = false;
452
+ if (skipFirst) {
453
+ skipFirst = false;
240
454
  return;
241
455
  }
242
456
  const previous = states;
457
+ const previousLength = previous.length;
458
+ const nextLength = items.length;
459
+ const shared = previousLength < nextLength ? previousLength : nextLength;
460
+ let prefix = 0;
461
+ while (prefix < shared && previous[prefix].item === items[prefix]) {
462
+ prefix += 1;
463
+ }
464
+ if (prefix === shared) {
465
+ if (nextLength >= previousLength) {
466
+ if (nextLength > previousLength) {
467
+ const batch = document.createDocumentFragment();
468
+ runWithOwner(parentOwner, () => {
469
+ for (let position = previousLength; position < nextLength; position += 1) {
470
+ previous.push(createState(items[position], position, batch));
471
+ }
472
+ });
473
+ parent.insertBefore(batch, endAnchor);
474
+ }
475
+ fallbackMount.render(nextLength === 0 && hasFallback ? resolve(fallback) : null);
476
+ return;
477
+ }
478
+ for (let index = nextLength; index < previousLength; index += 1) {
479
+ removeState(previous[index]);
480
+ }
481
+ previous.length = nextLength;
482
+ fallbackMount.render(nextLength === 0 && hasFallback ? resolve(fallback) : null);
483
+ return;
484
+ }
485
+ generation += 1;
486
+ const currentGeneration = generation;
243
487
  const pool = new Map();
244
- for (const state of previous) {
488
+ for (let index = 0; index < previousLength; index += 1) {
489
+ const state = previous[index];
490
+ state.previousIndex = index;
245
491
  const existing = pool.get(state.item);
246
492
  if (existing === undefined) {
247
493
  pool.set(state.item, state);
248
- continue;
249
494
  }
250
- if (Array.isArray(existing)) {
495
+ else if (Array.isArray(existing)) {
251
496
  existing.push(state);
252
- continue;
253
497
  }
254
- pool.set(state.item, [existing, state]);
255
- }
256
- const next = [];
257
- const created = [];
258
- const batch = document.createDocumentFragment();
259
- let batchUsed = false;
260
- const used = new Set();
261
- for (const item of items) {
262
- const entry = pool.get(item);
263
- const state = entry === undefined
264
- ? undefined
265
- : Array.isArray(entry)
266
- ? entry.shift()
267
- : entry;
268
- if (state === undefined) {
269
- const fresh = createState(item, next.length, batch);
270
- batchUsed = true;
271
- created.push(fresh);
272
- next.push(fresh);
273
- continue;
498
+ else {
499
+ pool.set(state.item, [existing, state]);
274
500
  }
275
- used.add(state);
276
- next.push(state);
277
501
  }
278
- for (const state of previous) {
279
- if (!used.has(state)) {
502
+ const next = spare;
503
+ next.length = 0;
504
+ const created = createdBuffer;
505
+ created.length = 0;
506
+ let batch = null;
507
+ runWithOwner(parentOwner, () => {
508
+ for (let position = 0; position < nextLength; position += 1) {
509
+ const item = items[position];
510
+ const entry = pool.get(item);
511
+ let state;
512
+ if (entry === undefined) {
513
+ state = undefined;
514
+ }
515
+ else if (Array.isArray(entry)) {
516
+ state = entry.length === 0 ? undefined : entry.shift();
517
+ if (entry.length === 0) {
518
+ pool.delete(item);
519
+ }
520
+ }
521
+ else {
522
+ state = entry;
523
+ pool.delete(item);
524
+ }
525
+ if (state === undefined) {
526
+ if (batch === null) {
527
+ batch = document.createDocumentFragment();
528
+ }
529
+ const fresh = createState(item, position, batch);
530
+ created.push(fresh);
531
+ next.push(fresh);
532
+ continue;
533
+ }
534
+ state.generation = currentGeneration;
535
+ next.push(state);
536
+ }
537
+ });
538
+ for (let index = 0; index < previousLength; index += 1) {
539
+ const state = previous[index];
540
+ if (state.generation !== currentGeneration) {
280
541
  removeState(state);
281
542
  }
282
543
  }
283
- if (batchUsed) {
544
+ if (batch !== null) {
284
545
  parent.insertBefore(batch, endAnchor);
285
546
  }
286
- states = next;
287
- for (let position = 0; position < next.length; position += 1) {
288
- next[position].setIndex(position);
547
+ for (let position = 0; position < nextLength; position += 1) {
548
+ const state = next[position];
549
+ if (state.previousIndex !== position) {
550
+ state.setIndex(position);
551
+ }
289
552
  }
290
553
  let inOrder = true;
291
554
  let cursor = 0;
292
- for (const state of previous) {
293
- if (!used.has(state)) {
555
+ for (let index = 0; index < previousLength; index += 1) {
556
+ const state = previous[index];
557
+ if (state.generation !== currentGeneration) {
294
558
  continue;
295
559
  }
296
560
  if (next[cursor] !== state) {
@@ -300,57 +564,79 @@ export function For(props) {
300
564
  cursor += 1;
301
565
  }
302
566
  if (inOrder) {
303
- for (const state of created) {
304
- if (next[cursor] !== state) {
567
+ for (let index = 0; index < created.length; index += 1) {
568
+ if (next[cursor] !== created[index]) {
305
569
  inOrder = false;
306
570
  break;
307
571
  }
308
572
  cursor += 1;
309
573
  }
310
574
  }
311
- if (inOrder && cursor !== next.length) {
575
+ if (inOrder && cursor !== nextLength) {
312
576
  inOrder = false;
313
577
  }
314
578
  if (!inOrder) {
315
- const previousOrder = new Map();
316
- for (let position = 0; position < previous.length; position += 1) {
317
- previousOrder.set(previous[position], position);
318
- }
319
- const positions = [];
320
- const slots = [];
321
- for (let position = 0; position < next.length; position += 1) {
322
- const previousPosition = previousOrder.get(next[position]);
323
- if (previousPosition !== undefined) {
324
- positions.push(previousPosition);
325
- slots.push(position);
579
+ ensureAnchors(next);
580
+ let reused = 0;
581
+ for (let position = 0; position < nextLength; position += 1) {
582
+ if (next[position].previousIndex >= 0) {
583
+ reused += 1;
326
584
  }
327
585
  }
328
- const stableSlots = new Set();
329
- for (const index of longestIncreasingSubsequence(positions)) {
330
- const slot = slots[index];
331
- if (slot !== undefined) {
332
- stableSlots.add(slot);
586
+ const [orderPositions, orderSlots, orderStable, orderTails, orderPrevious] = ensureBuffers(reused, nextLength);
587
+ let sequence = 0;
588
+ for (let position = 0; position < nextLength; position += 1) {
589
+ const state = next[position];
590
+ if (state.previousIndex >= 0) {
591
+ orderPositions[sequence] = state.previousIndex;
592
+ orderSlots[sequence] = position;
593
+ sequence += 1;
333
594
  }
334
595
  }
596
+ markStableSlots(orderPositions, orderSlots, reused, orderStable, orderTails, orderPrevious);
335
597
  let reference = endAnchor;
336
- for (let position = next.length - 1; position >= 0; position -= 1) {
598
+ for (let position = nextLength - 1; position >= 0; position -= 1) {
337
599
  const state = next[position];
338
- const block = [state.anchor, ...state.nodes];
339
- const parent = reference.parentNode;
340
- if (parent !== null && !stableSlots.has(position) && !precedes(block, reference)) {
341
- for (const node of block) {
342
- parent.insertBefore(node, reference);
600
+ const anchor = state.anchor;
601
+ if (anchor === null) {
602
+ continue;
603
+ }
604
+ const holder = reference.parentNode;
605
+ if (holder !== null &&
606
+ orderStable[position] === 0 &&
607
+ !precedesBlock(materializeNodes(state), anchor, reference)) {
608
+ holder.insertBefore(anchor, reference);
609
+ const nodes = materializeNodes(state);
610
+ for (let index = 0; index < nodes.length; index += 1) {
611
+ holder.insertBefore(nodes[index], reference);
343
612
  }
344
613
  }
345
- reference = state.anchor;
614
+ reference = anchor;
346
615
  }
347
616
  }
348
- fallbackMount.render(items.length === 0 && fallback !== undefined ? resolve(fallback) : null);
617
+ states = next;
618
+ spare = previous;
619
+ previous.length = 0;
620
+ created.length = 0;
621
+ fallbackMount.render(nextLength === 0 && hasFallback ? resolve(fallback) : null);
349
622
  });
350
623
  onCleanup(removeAll);
351
- return hydrating ? document.createDocumentFragment() : container;
624
+ return hydrating
625
+ ? document.createDocumentFragment()
626
+ : (container ??= document.createDocumentFragment());
352
627
  }
353
628
  export function Show(props) {
629
+ if (SERVER && isServerMode()) {
630
+ const active = truthy(resolve(props.when));
631
+ const value = resolve(active ? props.children : props.fallback);
632
+ const container = document.createDocumentFragment();
633
+ const nodes = extractMetadata(collectNodes(value));
634
+ for (const node of nodes) {
635
+ container.appendChild(node);
636
+ }
637
+ container.appendChild(document.createComment(SHOW));
638
+ return container;
639
+ }
354
640
  if (isHydrating()) {
355
641
  const outer = currentWindow();
356
642
  if (outer === null) {
@@ -376,7 +662,7 @@ export function Show(props) {
376
662
  end: anchor,
377
663
  cursor: outer.start.nextSibling,
378
664
  };
379
- const value = withWindow(window, () => resolve(active ? props.children : props.fallback));
665
+ const value = withWindow(window, resolve, active ? props.children : props.fallback);
380
666
  settleNodes(anchor, value);
381
667
  if (window.cursor !== anchor) {
382
668
  mismatch("содержимое Show не совпало");
@@ -385,7 +671,7 @@ export function Show(props) {
385
671
  }
386
672
  mount.render(active ? resolve(props.children) : resolve(props.fallback));
387
673
  });
388
- onCleanup(() => mount.clear());
674
+ onCleanup(mount.clear);
389
675
  return document.createDocumentFragment();
390
676
  }
391
677
  const container = document.createDocumentFragment();
@@ -397,7 +683,7 @@ export function Show(props) {
397
683
  const next = truthy(when) ? resolve(props.children) : resolve(props.fallback);
398
684
  mount.render(next);
399
685
  });
400
- onCleanup(() => mount.clear());
686
+ onCleanup(mount.clear);
401
687
  return container;
402
688
  }
403
689
  //# sourceMappingURL=control-flow.js.map