@ivlev-dev/silent-core 0.6.0 → 0.7.1

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