@farm.js/react 0.1.0-beta.20 → 0.1.0-beta.22

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.
@@ -5,8 +5,14 @@ import { flushSync } from 'react-dom';
5
5
 
6
6
  // src/iterable.ts
7
7
  var oneShotSnapshots = /* @__PURE__ */ new WeakMap();
8
+ var arrayIterator = Array.prototype[Symbol.iterator];
8
9
  function materializeIterable(source) {
9
10
  if (source == null) return [];
11
+ if (Array.isArray(source) && Object.getPrototypeOf(source) === Array.prototype && source[Symbol.iterator] === arrayIterator) {
12
+ const items2 = [];
13
+ for (let index = 0; index < source.length; index += 1) items2.push(source[index]);
14
+ return items2;
15
+ }
10
16
  const iterator = source[Symbol.iterator]();
11
17
  const oneShot = iterator === source && (typeof source === "object" || typeof source === "function");
12
18
  if (oneShot) {
@@ -1580,7 +1586,9 @@ function getCompilerHmrRegistry() {
1580
1586
  return registry;
1581
1587
  }
1582
1588
  function renderTextValue(value) {
1583
- if (Array.isArray(value)) return value.map(renderTextValue).join("");
1589
+ if (Array.isArray(value)) {
1590
+ return value.length === 1 ? renderTextValue(value[0]) : value.map(renderTextValue).join("");
1591
+ }
1584
1592
  if (value === null || value === void 0 || typeof value === "boolean") return "";
1585
1593
  return String(value);
1586
1594
  }
@@ -1707,8 +1715,11 @@ function updateSelectValue(element, value) {
1707
1715
  }
1708
1716
  if (fallback) fallback.selected = true;
1709
1717
  }
1718
+ function compilerAttributeName(name) {
1719
+ return name === "className" ? "class" : name === "htmlFor" ? "for" : name;
1720
+ }
1710
1721
  function updateAttribute(element, name, value) {
1711
- const attributeName = name === "className" ? "class" : name === "htmlFor" ? "for" : name;
1722
+ const attributeName = compilerAttributeName(name);
1712
1723
  const stringifiesBoolean = attributeName.startsWith("data-") || attributeName.startsWith("aria-");
1713
1724
  if (name === "value" && (isInputElement(element) || isTextAreaElement(element))) {
1714
1725
  const nextValue = value === null || value === void 0 ? "" : String(value);
@@ -1961,7 +1972,10 @@ function hasUnsafeCompilerFallbackKeys(descriptor) {
1961
1972
  }
1962
1973
  function findCompilerHostTarget(root, path) {
1963
1974
  let current = root;
1964
- for (const index of path) current = current?.children[index] || null;
1975
+ for (const index of path) {
1976
+ current = current && current.firstElementChild;
1977
+ for (let step = 0; step < index && current; step += 1) current = current.nextElementSibling;
1978
+ }
1965
1979
  return current;
1966
1980
  }
1967
1981
  var UNSET_STATIC_RANGE_BINDING = /* @__PURE__ */ Symbol("unset static range binding");
@@ -2496,9 +2510,64 @@ function normalizedKeyedRowBindingValue(binding, value) {
2496
2510
  return binding.kind === "text" ? renderTextValue(value) : value;
2497
2511
  }
2498
2512
  function readKeyedRowBindingValues(props, item, index) {
2499
- return props.bindings.map(
2500
- (binding) => normalizedKeyedRowBindingValue(binding, binding.read(item, index))
2501
- );
2513
+ const values = [];
2514
+ for (const binding of props.bindings) {
2515
+ values.push(normalizedKeyedRowBindingValue(binding, binding.read(item, index)));
2516
+ }
2517
+ return values;
2518
+ }
2519
+ var KEYED_ROW_TEMPLATES = /* @__PURE__ */ new WeakMap();
2520
+ function createKeyedRowInstance(props, document, key, item, index) {
2521
+ const template = props.cloneRows && KEYED_ROW_TEMPLATES.get(props);
2522
+ const element = template ? template.cloneNode(true) : createCompilerHostElement(document, props.create(item, index));
2523
+ const instance = {
2524
+ key,
2525
+ element,
2526
+ values: readKeyedRowBindingValues(props, item, index),
2527
+ item,
2528
+ index,
2529
+ conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
2530
+ };
2531
+ if (props.cloneRows) {
2532
+ const targets = [];
2533
+ let found = true;
2534
+ for (const binding of props.bindings) {
2535
+ const target = findCompilerHostTarget(element, binding.path);
2536
+ found && (found = target !== null);
2537
+ targets.push(target);
2538
+ }
2539
+ if (found) instance.targets = targets;
2540
+ if (template) {
2541
+ for (let bindingIndex = 0; bindingIndex < targets.length; bindingIndex += 1) {
2542
+ commitKeyedRowBinding(
2543
+ props.bindings[bindingIndex],
2544
+ instance,
2545
+ bindingIndex,
2546
+ instance.values[bindingIndex],
2547
+ targets[bindingIndex]
2548
+ );
2549
+ }
2550
+ } else {
2551
+ const skeleton = keyedRowTemplate(props, element, targets);
2552
+ if (skeleton) KEYED_ROW_TEMPLATES.set(props, skeleton);
2553
+ }
2554
+ }
2555
+ return instance;
2556
+ }
2557
+ function keyedRowTemplate(props, element, targets) {
2558
+ for (let bindingIndex = 0; bindingIndex < props.bindings.length; bindingIndex += 1) {
2559
+ const binding = props.bindings[bindingIndex];
2560
+ const target = targets[bindingIndex];
2561
+ if (!target || binding.kind === "style") return void 0;
2562
+ if (binding.kind === "attribute" && !target.hasAttribute(compilerAttributeName(binding.name))) {
2563
+ return void 0;
2564
+ }
2565
+ }
2566
+ const template = element.cloneNode(true);
2567
+ for (const binding of props.bindings) {
2568
+ if (binding.kind === "text") findCompilerHostTarget(template, binding.path).textContent = "";
2569
+ }
2570
+ return template;
2502
2571
  }
2503
2572
  function applyKeyedRowBindings(props, instance, item, index) {
2504
2573
  if (props.bindings.length < 2) {
@@ -2510,10 +2579,7 @@ function applyKeyedRowBindings(props, instance, item, index) {
2510
2579
  for (let bindingIndex = 0; bindingIndex < props.bindings.length; bindingIndex += 1) {
2511
2580
  if (Object.is(instance.values[bindingIndex], nextValues[bindingIndex])) continue;
2512
2581
  targets || (targets = []);
2513
- targets[bindingIndex] = findCompilerHostTarget(
2514
- instance.element,
2515
- props.bindings[bindingIndex].path
2516
- );
2582
+ targets[bindingIndex] = keyedRowBindingTarget(props, instance, bindingIndex);
2517
2583
  }
2518
2584
  if (!targets) return;
2519
2585
  for (let bindingIndex = 0; bindingIndex < props.bindings.length; bindingIndex += 1) {
@@ -2532,14 +2598,22 @@ function applyKeyedRowBinding(props, instance, item, index, bindingIndex) {
2532
2598
  const binding = props.bindings[bindingIndex];
2533
2599
  const value = normalizedKeyedRowBindingValue(binding, binding.read(item, index));
2534
2600
  if (Object.is(instance.values[bindingIndex], value)) return;
2535
- const target = findCompilerHostTarget(instance.element, binding.path);
2601
+ const target = keyedRowBindingTarget(props, instance, bindingIndex);
2536
2602
  commitKeyedRowBinding(binding, instance, bindingIndex, value, target);
2537
2603
  }
2604
+ function keyedRowBindingTarget(props, instance, bindingIndex) {
2605
+ return instance.targets?.[bindingIndex] || findCompilerHostTarget(instance.element, props.bindings[bindingIndex].path);
2606
+ }
2538
2607
  function commitKeyedRowBinding(binding, instance, bindingIndex, value, target) {
2539
2608
  instance.values[bindingIndex] = value;
2540
2609
  if (!target) return;
2541
2610
  if (binding.kind === "text") {
2542
- target.textContent = value;
2611
+ const text = target.firstChild;
2612
+ if (value && text && text === target.lastChild && text.nodeType === 3) {
2613
+ text.data = value;
2614
+ } else {
2615
+ target.textContent = value;
2616
+ }
2543
2617
  } else if (binding.kind === "style" && binding.name) {
2544
2618
  updateStyle(target, binding.name, value);
2545
2619
  } else if (binding.kind === "attribute" && binding.name) {
@@ -2551,28 +2625,29 @@ function prepareKeyedRowBindingUpdate(props, instance, item, index, bindingIndex
2551
2625
  const rawValue = binding.read(item, index);
2552
2626
  const value = normalizedKeyedRowBindingValue(binding, rawValue);
2553
2627
  if (Object.is(instance.values[bindingIndex], value)) return null;
2554
- const target = findCompilerHostTarget(instance.element, binding.path);
2628
+ const target = keyedRowBindingTarget(props, instance, bindingIndex);
2555
2629
  if (!target || binding.kind !== "text" && !binding.name) return void 0;
2556
2630
  return [bindingIndex, target, value];
2557
2631
  }
2558
2632
  function prepareKeyedRowBindingUpdates(props, instance, item, index) {
2559
- const updates = [];
2633
+ let updates;
2560
2634
  for (let bindingIndex = 0; bindingIndex < props.bindings.length; bindingIndex += 1) {
2561
2635
  const update = prepareKeyedRowBindingUpdate(props, instance, item, index, bindingIndex);
2562
2636
  if (update === void 0) return void 0;
2563
- if (update) updates.push(update);
2637
+ if (update) (updates || (updates = [])).push(update);
2564
2638
  }
2565
- return updates;
2639
+ return updates || NO_KEYED_ROW_BINDING_UPDATES;
2566
2640
  }
2641
+ var NO_KEYED_ROW_BINDING_UPDATES = [];
2567
2642
  function applyPreparedKeyedRowBindingUpdates(props, instance, updates) {
2568
2643
  for (const [bindingIndex, target, value] of updates) {
2569
2644
  commitKeyedRowBinding(props.bindings[bindingIndex], instance, bindingIndex, value, target);
2570
2645
  }
2571
2646
  }
2572
- function prepareKeyedRowBindingSetUpdates(props, instances, rows) {
2647
+ function prepareKeyedRowBindingSetUpdates(props, instances, rows, ordered) {
2573
2648
  const preparedUpdates = [];
2574
2649
  for (let index = 0; index < rows.items.length; index += 1) {
2575
- const instance = instances.get(rows.keys[index]);
2650
+ const instance = ordered ? ordered[index] : instances.get(rows.keys[index]);
2576
2651
  if (!instance) return void 0;
2577
2652
  const updates = prepareKeyedRowBindingUpdates(props, instance, rows.items[index], index);
2578
2653
  if (!updates) return void 0;
@@ -2992,6 +3067,24 @@ var CompilerNestedConditionalRanges = class {
2992
3067
  this.staticValues.length = 0;
2993
3068
  }
2994
3069
  };
3070
+ function reconcileStableNestedKeyedRows(range, previous, rows) {
3071
+ if (rows.keys.length !== previous.size) return void 0;
3072
+ let index = 0;
3073
+ for (const key of previous.keys()) {
3074
+ if (rows.keys[index++] !== key) return void 0;
3075
+ }
3076
+ index = 0;
3077
+ for (const existing of previous.values()) {
3078
+ const item = rows.items[index];
3079
+ const descriptor = range.create(item, index);
3080
+ if (!existing.scope?.update(descriptor)) return false;
3081
+ applyKeyedRowBindings(range, existing, item, index);
3082
+ existing.item = item;
3083
+ existing.index = index;
3084
+ index += 1;
3085
+ }
3086
+ return true;
3087
+ }
2995
3088
  var CompilerNestedKeyedRanges = class {
2996
3089
  constructor(owner, root, host, block, onFallback) {
2997
3090
  this.owner = owner;
@@ -3165,6 +3258,8 @@ var CompilerNestedKeyedRanges = class {
3165
3258
  reconcileRange(rangeIndex, rows) {
3166
3259
  const range = this.block.ranges[rangeIndex];
3167
3260
  const previous = this.instances[rangeIndex];
3261
+ const stable = reconcileStableNestedKeyedRows(range, previous, rows);
3262
+ if (stable !== void 0) return stable;
3168
3263
  const oldIndices = /* @__PURE__ */ new Map();
3169
3264
  [...previous.keys()].forEach((key, index) => oldIndices.set(key, index));
3170
3265
  const nextKeys = new Set(rows.keys);
@@ -3205,7 +3300,7 @@ var CompilerNestedKeyedRanges = class {
3205
3300
  let anchor = this.anchorAfter(rangeIndex);
3206
3301
  for (let index = nextInstances.length - 1; index >= 0; index -= 1) {
3207
3302
  const instance = nextInstances[index];
3208
- if (sequence[index] < 0 || !stablePositions.has(index) && instance.element.nextSibling !== anchor) {
3303
+ if (sequence[index] < 0 || !stablePositions[index] && instance.element.nextSibling !== anchor) {
3209
3304
  this.root.insertBefore(instance.element, anchor);
3210
3305
  }
3211
3306
  anchor = instance.element;
@@ -3523,6 +3618,8 @@ var CompilerNestedMixedRanges = class {
3523
3618
  const instance = this.instances[rangeIndex];
3524
3619
  if (instance.kind !== "keyed") return false;
3525
3620
  const previous = instance.value;
3621
+ const stable = reconcileStableNestedKeyedRows(range, previous, rows);
3622
+ if (stable !== void 0) return stable;
3526
3623
  const oldIndices = /* @__PURE__ */ new Map();
3527
3624
  [...previous.keys()].forEach((key, index) => oldIndices.set(key, index));
3528
3625
  const nextKeys = new Set(rows.keys);
@@ -3570,7 +3667,7 @@ var CompilerNestedMixedRanges = class {
3570
3667
  let anchor = this.anchorAfter(rangeIndex);
3571
3668
  for (let index = nextInstances.length - 1; index >= 0; index -= 1) {
3572
3669
  const row = nextInstances[index];
3573
- if (sequence[index] < 0 || !stablePositions.has(index) && row.element.nextSibling !== anchor) {
3670
+ if (sequence[index] < 0 || !stablePositions[index] && row.element.nextSibling !== anchor) {
3574
3671
  this.root.insertBefore(row.element, anchor);
3575
3672
  }
3576
3673
  anchor = row.element;
@@ -4232,7 +4329,7 @@ function createConditionalRangesBlockComponent(owner) {
4232
4329
  }
4233
4330
  function longestIncreasingSubsequencePositions(sequence) {
4234
4331
  const tails = [];
4235
- const previous = Array.from({ length: sequence.length }, () => -1);
4332
+ const previous = new Int32Array(sequence.length).fill(-1);
4236
4333
  for (let index = 0; index < sequence.length; index += 1) {
4237
4334
  if (sequence[index] < 0) continue;
4238
4335
  let low = 0;
@@ -4245,10 +4342,10 @@ function longestIncreasingSubsequencePositions(sequence) {
4245
4342
  if (low > 0) previous[index] = tails[low - 1];
4246
4343
  tails[low] = index;
4247
4344
  }
4248
- const positions = /* @__PURE__ */ new Set();
4345
+ const positions = new Uint8Array(sequence.length);
4249
4346
  let cursor = tails.at(-1) ?? -1;
4250
4347
  while (cursor >= 0) {
4251
- positions.add(cursor);
4348
+ positions[cursor] = 1;
4252
4349
  cursor = previous[cursor];
4253
4350
  }
4254
4351
  return positions;
@@ -4261,15 +4358,13 @@ function reorderCompilerKeyedRows(root, instances, sequence, anchor) {
4261
4358
  if (sequence[index] < 0) {
4262
4359
  let start = index;
4263
4360
  while (start > 0 && sequence[start - 1] < 0) start -= 1;
4264
- const fragment = root.ownerDocument.createDocumentFragment();
4265
4361
  for (let itemIndex = start; itemIndex <= index; itemIndex += 1) {
4266
- fragment.append(instances[itemIndex].element);
4362
+ root.insertBefore(instances[itemIndex].element, anchor);
4267
4363
  }
4268
- root.insertBefore(fragment, anchor);
4269
4364
  anchor = instances[start].element;
4270
4365
  index = start;
4271
4366
  } else {
4272
- if (!stablePositions.has(index) && instance.element.nextSibling !== anchor) {
4367
+ if (!stablePositions[index] && instance.element.nextSibling !== anchor) {
4273
4368
  root.insertBefore(instance.element, anchor);
4274
4369
  }
4275
4370
  anchor = instance.element;
@@ -4537,15 +4632,13 @@ function reconcileCompilerKeyedArrayAppend(props, dirtyState, collectionToken, i
4537
4632
  const item = finalValue[index];
4538
4633
  const key = keyedRowIdentity(props.rowKey(item, index));
4539
4634
  if (nextInstances.has(key)) return void 0;
4540
- const descriptor = props.create(item, index);
4541
- const instance = {
4635
+ const instance = createKeyedRowInstance(
4636
+ props,
4637
+ root.ownerDocument,
4542
4638
  key,
4543
- element: createCompilerHostElement(root.ownerDocument, descriptor),
4544
- values: readKeyedRowBindingValues(props, item, index),
4545
4639
  item,
4546
- index,
4547
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
4548
- };
4640
+ index
4641
+ );
4549
4642
  appended.push(instance);
4550
4643
  nextInstances.set(key, instance);
4551
4644
  }
@@ -4602,15 +4695,7 @@ function reconcileCompilerKeyedArrayStructuralAppend(props, dirtyState, collecti
4602
4695
  const key = keyedRowIdentity(props.rowKey(item, index));
4603
4696
  if (knownKeys.has(key)) return void 0;
4604
4697
  knownKeys.add(key);
4605
- const descriptor = props.create(item, index);
4606
- appended.push({
4607
- key,
4608
- element: createCompilerHostElement(root.ownerDocument, descriptor),
4609
- values: readKeyedRowBindingValues(props, item, index),
4610
- item,
4611
- index,
4612
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
4613
- });
4698
+ appended.push(createKeyedRowInstance(props, root.ownerDocument, key, item, index));
4614
4699
  }
4615
4700
  } catch {
4616
4701
  return void 0;
@@ -4711,15 +4796,7 @@ function reconcileCompilerKeyedArrayStructuralAppendMap(props, dirtyState, colle
4711
4796
  const key = keyedRowIdentity(props.rowKey(item, index));
4712
4797
  if (instances.has(key) || appendedKeys.has(key)) return void 0;
4713
4798
  appendedKeys.add(key);
4714
- const descriptor = props.create(item, index);
4715
- appended.push({
4716
- key,
4717
- element: createCompilerHostElement(root.ownerDocument, descriptor),
4718
- values: readKeyedRowBindingValues(props, item, index),
4719
- item,
4720
- index,
4721
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
4722
- });
4799
+ appended.push(createKeyedRowInstance(props, root.ownerDocument, key, item, index));
4723
4800
  }
4724
4801
  } catch {
4725
4802
  return void 0;
@@ -4817,15 +4894,7 @@ function reconcileCompilerKeyedArrayRollingWindow(props, dirtyState, collectionT
4817
4894
  const key = keyedRowIdentity(props.rowKey(item, index));
4818
4895
  if (knownKeys.has(key)) return void 0;
4819
4896
  knownKeys.add(key);
4820
- const descriptor = props.create(item, index);
4821
- incoming.push({
4822
- key,
4823
- element: createCompilerHostElement(root.ownerDocument, descriptor),
4824
- values: readKeyedRowBindingValues(props, item, index),
4825
- item,
4826
- index,
4827
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
4828
- });
4897
+ incoming.push(createKeyedRowInstance(props, root.ownerDocument, key, item, index));
4829
4898
  }
4830
4899
  } catch {
4831
4900
  return void 0;
@@ -4896,15 +4965,7 @@ function reconcileCompilerKeyedArrayMappedRollingWindow(props, dirtyState, colle
4896
4965
  const key = keyedRowIdentity(props.rowKey(item, index));
4897
4966
  if (instances.has(key) || incomingKeys.has(key)) return void 0;
4898
4967
  incomingKeys.add(key);
4899
- const descriptor = props.create(item, index);
4900
- incoming.push({
4901
- key,
4902
- element: createCompilerHostElement(root.ownerDocument, descriptor),
4903
- values: readKeyedRowBindingValues(props, item, index),
4904
- item,
4905
- index,
4906
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
4907
- });
4968
+ incoming.push(createKeyedRowInstance(props, root.ownerDocument, key, item, index));
4908
4969
  }
4909
4970
  } catch {
4910
4971
  return void 0;
@@ -4983,15 +5044,7 @@ function reconcileCompilerKeyedArrayPosition(props, dirtyState, collectionToken,
4983
5044
  if (instances.has(key)) return void 0;
4984
5045
  let inserted;
4985
5046
  try {
4986
- const descriptor = props.create(item, update.position);
4987
- inserted = {
4988
- key,
4989
- element: createCompilerHostElement(root.ownerDocument, descriptor),
4990
- values: readKeyedRowBindingValues(props, item, update.position),
4991
- item,
4992
- index: update.position,
4993
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
4994
- };
5047
+ inserted = createKeyedRowInstance(props, root.ownerDocument, key, item, update.position);
4995
5048
  } catch {
4996
5049
  return void 0;
4997
5050
  }
@@ -5015,15 +5068,7 @@ function reconcileCompilerKeyedArrayPosition(props, dirtyState, collectionToken,
5015
5068
  if (instances.has(key)) return void 0;
5016
5069
  let replacement;
5017
5070
  try {
5018
- const descriptor = props.create(item, update.position);
5019
- replacement = {
5020
- key,
5021
- element: createCompilerHostElement(root.ownerDocument, descriptor),
5022
- values: readKeyedRowBindingValues(props, item, update.position),
5023
- item,
5024
- index: update.position,
5025
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
5026
- };
5071
+ replacement = createKeyedRowInstance(props, root.ownerDocument, key, item, update.position);
5027
5072
  } catch {
5028
5073
  return void 0;
5029
5074
  }
@@ -5066,15 +5111,7 @@ function reconcileCompilerKeyedArrayBatchInsert(props, dirtyState, collectionTok
5066
5111
  const key = keyedRowIdentity(props.rowKey(item, index));
5067
5112
  if (instances.has(key) || incomingKeys.has(key)) return void 0;
5068
5113
  incomingKeys.add(key);
5069
- const descriptor = props.create(item, index);
5070
- incoming.push({
5071
- key,
5072
- element: createCompilerHostElement(root.ownerDocument, descriptor),
5073
- values: readKeyedRowBindingValues(props, item, index),
5074
- item,
5075
- index,
5076
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
5077
- });
5114
+ incoming.push(createKeyedRowInstance(props, root.ownerDocument, key, item, index));
5078
5115
  }
5079
5116
  } catch {
5080
5117
  return void 0;
@@ -5160,15 +5197,7 @@ function prepareCompilerKeyedArrayWindow(props, instances, previousInstances, ro
5160
5197
  continue;
5161
5198
  }
5162
5199
  if (instances.has(key)) return void 0;
5163
- const descriptor = props.create(item, index);
5164
- nextWindow.push({
5165
- key,
5166
- element: createCompilerHostElement(root.ownerDocument, descriptor),
5167
- values: readKeyedRowBindingValues(props, item, index),
5168
- item,
5169
- index,
5170
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
5171
- });
5200
+ nextWindow.push(createKeyedRowInstance(props, root.ownerDocument, key, item, index));
5172
5201
  sequence.push(-1);
5173
5202
  preparedBindings.push(void 0);
5174
5203
  }
@@ -5298,20 +5327,12 @@ function reconcileCompilerKeyedArrayWindowReplace(props, dirtyState, collectionT
5298
5327
  if (instances.has(key) || incomingKeys2.has(key)) return void 0;
5299
5328
  incomingKeys2.add(key);
5300
5329
  replacesRows = true;
5301
- const descriptor = props.create(item, index);
5302
5330
  prepared2.push([
5303
5331
  index,
5304
5332
  instance,
5305
5333
  item,
5306
5334
  void 0,
5307
- {
5308
- key,
5309
- element: createCompilerHostElement(root.ownerDocument, descriptor),
5310
- values: readKeyedRowBindingValues(props, item, index),
5311
- item,
5312
- index,
5313
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
5314
- }
5335
+ createKeyedRowInstance(props, root.ownerDocument, key, item, index)
5315
5336
  ]);
5316
5337
  continue;
5317
5338
  }
@@ -5649,15 +5670,7 @@ function reconcileCompilerKeyedArrayPrepend(props, dirtyState, collectionToken,
5649
5670
  const key = keyedRowIdentity(props.rowKey(item, index));
5650
5671
  if (instances.has(key) || incomingKeys.has(key)) return void 0;
5651
5672
  incomingKeys.add(key);
5652
- const descriptor = props.create(item, index);
5653
- prepended.push({
5654
- key,
5655
- element: createCompilerHostElement(root.ownerDocument, descriptor),
5656
- values: readKeyedRowBindingValues(props, item, index),
5657
- item,
5658
- index,
5659
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
5660
- });
5673
+ prepended.push(createKeyedRowInstance(props, root.ownerDocument, key, item, index));
5661
5674
  }
5662
5675
  } catch {
5663
5676
  return void 0;
@@ -5715,15 +5728,7 @@ function reconcileCompilerKeyedArrayStructuralPrepend(props, dirtyState, collect
5715
5728
  const key = keyedRowIdentity(props.rowKey(item, index));
5716
5729
  if (knownKeys.has(key)) return void 0;
5717
5730
  knownKeys.add(key);
5718
- const descriptor = props.create(item, index);
5719
- prepended.push({
5720
- key,
5721
- element: createCompilerHostElement(root.ownerDocument, descriptor),
5722
- values: readKeyedRowBindingValues(props, item, index),
5723
- item,
5724
- index,
5725
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
5726
- });
5731
+ prepended.push(createKeyedRowInstance(props, root.ownerDocument, key, item, index));
5727
5732
  }
5728
5733
  } catch {
5729
5734
  return void 0;
@@ -5834,15 +5839,7 @@ function reconcileCompilerKeyedArrayStructuralPrependMap(props, dirtyState, coll
5834
5839
  const key = keyedRowIdentity(props.rowKey(item, index));
5835
5840
  if (knownKeys.has(key)) return void 0;
5836
5841
  knownKeys.add(key);
5837
- const descriptor = props.create(item, index);
5838
- prepended.push({
5839
- key,
5840
- element: createCompilerHostElement(root.ownerDocument, descriptor),
5841
- values: readKeyedRowBindingValues(props, item, index),
5842
- item,
5843
- index,
5844
- conditionalValues: EMPTY_KEYED_ROW_CONDITIONAL_VALUES
5845
- });
5842
+ prepended.push(createKeyedRowInstance(props, root.ownerDocument, key, item, index));
5846
5843
  }
5847
5844
  } catch {
5848
5845
  return void 0;
@@ -5894,7 +5891,7 @@ function reconcileCompilerKeyedArrayFilter(props, dirtyState, collectionToken, i
5894
5891
  return void 0;
5895
5892
  }
5896
5893
  const dependencies = props.dependencies || props.structureDependencies;
5897
- const relevantDirty = (dependencies || []).filter((index) => dirtyState.has(index));
5894
+ const relevantDirty = (dependencies || []).filter((index2) => dirtyState.has(index2));
5898
5895
  if (relevantDirty.length !== 1 || relevantDirty[0] !== collectionDependency) {
5899
5896
  return void 0;
5900
5897
  }
@@ -5906,31 +5903,34 @@ function reconcileCompilerKeyedArrayFilter(props, dirtyState, collectionToken, i
5906
5903
  );
5907
5904
  if (!survivorResult || !Array.isArray(finalValue)) return void 0;
5908
5905
  const { indices: survivorIndices, keysStable } = survivorResult;
5909
- const previousInstances = [...instances.values()];
5910
- const survivors = [];
5906
+ const removed = [];
5907
+ let cursor = 0;
5908
+ let sourceIndex = 0;
5911
5909
  try {
5912
- for (let index = 0; index < survivorIndices.length; index += 1) {
5913
- const sourceIndex = survivorIndices[index];
5914
- const instance = previousInstances[sourceIndex];
5915
- const item = finalValue[index];
5916
- if (!instance || instance.index !== sourceIndex || !Object.is(instance.item, item) || !keysStable && keyedRowIdentity(props.rowKey(item, index)) !== instance.key) {
5917
- return void 0;
5910
+ for (const instance of instances.values()) {
5911
+ if (survivorIndices[cursor] !== sourceIndex) {
5912
+ removed.push(instance);
5913
+ } else {
5914
+ const item = finalValue[cursor];
5915
+ if (instance.index !== sourceIndex || !Object.is(instance.item, item) || !keysStable && keyedRowIdentity(props.rowKey(item, cursor)) !== instance.key) {
5916
+ return void 0;
5917
+ }
5918
+ cursor += 1;
5918
5919
  }
5919
- survivors.push(instance);
5920
+ sourceIndex += 1;
5920
5921
  }
5921
5922
  } catch {
5922
5923
  return void 0;
5923
5924
  }
5924
- const survivorSet = new Set(survivorIndices);
5925
- for (let index = 0; index < previousInstances.length; index += 1) {
5926
- if (survivorSet.has(index)) continue;
5927
- previousInstances[index].scope?.cleanup();
5928
- previousInstances[index].element.remove();
5929
- }
5930
- for (let index = 0; index < survivors.length; index += 1) {
5931
- survivors[index].index = index;
5925
+ if (cursor !== survivorIndices.length) return void 0;
5926
+ for (const instance of removed) {
5927
+ instance.scope?.cleanup();
5928
+ instance.element.remove();
5929
+ instances.delete(instance.key);
5932
5930
  }
5933
- return keyedRowInstancesByKey(survivors);
5931
+ let index = 0;
5932
+ for (const instance of instances.values()) instance.index = index++;
5933
+ return instances;
5934
5934
  }
5935
5935
  function createKeyedRowConditionalRuntime() {
5936
5936
  class FarmKeyedRowConditional extends React.Component {
@@ -5975,9 +5975,42 @@ function createKeyedRowConditionalRuntime() {
5975
5975
  }
5976
5976
  FarmKeyedRowConditional.displayName = "FarmCompiledKeyedRowConditional";
5977
5977
  return {
5978
- Component: FarmKeyedRowConditional,
5978
+ element: (props) => React.createElement(FarmKeyedRowConditional, {
5979
+ key: `${props.rowKey}:${props.id}`,
5980
+ ...props
5981
+ }),
5979
5982
  readValues: readKeyedRowConditionalValues,
5980
- changed: keyedRowConditionalChanged
5983
+ changed: keyedRowConditionalChanged,
5984
+ owner: (listeners) => ({
5985
+ subscribe(key, id, refresh) {
5986
+ let row = listeners.get(key);
5987
+ if (!row) {
5988
+ row = /* @__PURE__ */ new Map();
5989
+ listeners.set(key, row);
5990
+ }
5991
+ row.set(id, refresh);
5992
+ return () => {
5993
+ const current = listeners.get(key);
5994
+ if (current?.get(id) !== refresh) return;
5995
+ current.delete(id);
5996
+ if (current.size === 0) listeners.delete(key);
5997
+ };
5998
+ }
5999
+ }),
6000
+ notify(listeners, changes, afterCommit) {
6001
+ let pending = changes.length;
6002
+ const committed = () => {
6003
+ pending -= 1;
6004
+ if (pending === 0) afterCommit?.();
6005
+ };
6006
+ flushSync(() => {
6007
+ for (const change of changes) {
6008
+ const refresh = listeners.get(change.key)?.get(change.id);
6009
+ if (refresh) refresh(change.item, change.index, committed);
6010
+ else committed();
6011
+ }
6012
+ });
6013
+ }
5981
6014
  };
5982
6015
  }
5983
6016
  function createKeyedRowHostRuntime() {
@@ -6003,14 +6036,15 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6003
6036
  this.currentProps = this.props;
6004
6037
  this.definitionVersion = owner.getDefinitionVersion?.();
6005
6038
  this.instances = /* @__PURE__ */ new Map();
6039
+ this.reactElements = [];
6006
6040
  this.keyedTargets = options.keyedTargets?.(this.props);
6007
6041
  this.renderVersion = 0;
6008
6042
  this.eventHandlers = /* @__PURE__ */ new Map();
6009
6043
  this.conditionalListeners = /* @__PURE__ */ new Map();
6010
6044
  this.instancesByElement = /* @__PURE__ */ new WeakMap();
6011
- this.conditionalOwner = {
6012
- subscribe: (key, id, refresh) => this.subscribeToRowConditional(key, id, refresh)
6013
- };
6045
+ // False after a rebuild without delegated events left surviving rows unindexed.
6046
+ this.elementIndexComplete = true;
6047
+ this.conditionalOwner = options.conditionals?.owner(this.conditionalListeners);
6014
6048
  this.captureRoot = (root) => {
6015
6049
  this.root = root;
6016
6050
  };
@@ -6062,14 +6096,10 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6062
6096
  }
6063
6097
  };
6064
6098
  this.rowConditional = (item, index, conditionalId, render) => {
6065
- if (this.state.fallback) return render(item, index);
6066
- const key = keyedRowIdentity(this.currentProps.rowKey(item, index));
6067
- const Conditional = options.conditionals?.Component;
6068
- if (!Conditional) return render(item, index);
6069
- return React.createElement(Conditional, {
6070
- key: `${key}:${conditionalId}`,
6099
+ if (this.state.fallback || !options.conditionals) return render(item, index);
6100
+ return options.conditionals.element({
6071
6101
  owner: this.conditionalOwner,
6072
- rowKey: key,
6102
+ rowKey: keyedRowIdentity(this.currentProps.rowKey(item, index)),
6073
6103
  id: conditionalId,
6074
6104
  renderVersion: this.renderVersion,
6075
6105
  item,
@@ -6119,7 +6149,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6119
6149
  );
6120
6150
  if (positionedInstances) {
6121
6151
  if (positionedInstances !== this.instances) {
6122
- this.instances = new Map(positionedInstances);
6152
+ this.instances = positionedInstances;
6123
6153
  this.rebuildElementIndex(this.instances);
6124
6154
  this.pruneEventHandlers();
6125
6155
  this.pruneConditionalListeners();
@@ -6161,8 +6191,10 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6161
6191
  this.instances = rollingWindowInstances instanceof Map ? rollingWindowInstances : new Map(rollingWindowInstances);
6162
6192
  if (delta) {
6163
6193
  COMPILER_KEYED_ROLLING_WINDOW_DELTAS.delete(rollingWindowInstances);
6164
- for (const instance of delta.added) {
6165
- this.instancesByElement.set(instance.element, instance);
6194
+ if (this.usesDelegatedEvents()) {
6195
+ for (const instance of delta.added) {
6196
+ this.instancesByElement.set(instance.element, instance);
6197
+ }
6166
6198
  }
6167
6199
  for (const key of delta.removedKeys) {
6168
6200
  this.eventHandlers.delete(key);
@@ -6186,7 +6218,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6186
6218
  );
6187
6219
  if (filteredInstances) {
6188
6220
  this.instances = filteredInstances;
6189
- this.rebuildElementIndex(this.instances);
6221
+ if (!this.elementIndexComplete) this.rebuildElementIndex(this.instances);
6190
6222
  this.pruneEventHandlers();
6191
6223
  this.pruneConditionalListeners();
6192
6224
  this.commitCurrentCollection(dirtyState);
@@ -6202,7 +6234,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6202
6234
  this.hasReactOwnedRows()
6203
6235
  );
6204
6236
  if (prependedInstances) {
6205
- this.instances = new Map(prependedInstances);
6237
+ this.instances = prependedInstances;
6206
6238
  this.rebuildElementIndex(this.instances);
6207
6239
  this.commitCurrentCollection(dirtyState);
6208
6240
  afterCommit?.();
@@ -6215,9 +6247,29 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6215
6247
  }
6216
6248
  readRows(props) {
6217
6249
  const items = materializeIterable(props.items());
6218
- const keys = items.map((item, index) => keyedRowIdentity(props.rowKey(item, index)));
6219
- if (new Set(keys).size !== keys.length) return null;
6220
- return { items, keys };
6250
+ const keys = [];
6251
+ const committedSize = this.instances.size;
6252
+ let prefix = 0;
6253
+ const committed = this.instances.keys();
6254
+ for (let index = 0; index < items.length; index += 1) {
6255
+ const key = keyedRowIdentity(props.rowKey(items[index], index));
6256
+ keys.push(key);
6257
+ if (prefix === index && index < committedSize && committed.next().value === key) {
6258
+ prefix += 1;
6259
+ }
6260
+ }
6261
+ if (prefix === committedSize && keys.length === committedSize) return { items, keys };
6262
+ if (prefix === committedSize && committedSize > 0) {
6263
+ const tail = /* @__PURE__ */ new Set();
6264
+ for (let index = committedSize; index < keys.length; index += 1) {
6265
+ const key = keys[index];
6266
+ if (this.instances.has(key) || tail.has(key)) return null;
6267
+ tail.add(key);
6268
+ }
6269
+ return { items, keys, appended: true };
6270
+ }
6271
+ const keySet = new Set(keys);
6272
+ return keySet.size === keys.length ? { items, keys, keySet } : null;
6221
6273
  }
6222
6274
  usesDelegatedEvents(props = this.currentProps) {
6223
6275
  return Boolean(
@@ -6226,6 +6278,8 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6226
6278
  }
6227
6279
  rebuildElementIndex(instances) {
6228
6280
  this.instancesByElement = /* @__PURE__ */ new WeakMap();
6281
+ this.elementIndexComplete = this.usesDelegatedEvents();
6282
+ if (!this.elementIndexComplete) return;
6229
6283
  for (const instance of instances.values()) {
6230
6284
  this.instancesByElement.set(instance.element, instance);
6231
6285
  }
@@ -6267,20 +6321,6 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6267
6321
  })
6268
6322
  );
6269
6323
  }
6270
- subscribeToRowConditional(key, id, refresh) {
6271
- let listeners = this.conditionalListeners.get(key);
6272
- if (!listeners) {
6273
- listeners = /* @__PURE__ */ new Map();
6274
- this.conditionalListeners.set(key, listeners);
6275
- }
6276
- listeners.set(id, refresh);
6277
- return () => {
6278
- const current = this.conditionalListeners.get(key);
6279
- if (current?.get(id) !== refresh) return;
6280
- current.delete(id);
6281
- if (current.size === 0) this.conditionalListeners.delete(key);
6282
- };
6283
- }
6284
6324
  hasReactOwnedRows(props = this.currentProps) {
6285
6325
  return Boolean(
6286
6326
  props.conditionals?.length || props.events?.length && !this.usesDelegatedEvents(props)
@@ -6360,6 +6400,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6360
6400
  }
6361
6401
  this.cleanupHostScopes();
6362
6402
  this.instances = instances;
6403
+ this.reactElements = elements;
6363
6404
  this.rebuildElementIndex(instances);
6364
6405
  this.pruneEventHandlers();
6365
6406
  this.pruneConditionalListeners();
@@ -6398,22 +6439,8 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6398
6439
  });
6399
6440
  }
6400
6441
  notifyConditionalChanges(changes, afterCommit) {
6401
- if (changes.length === 0) {
6402
- afterCommit?.();
6403
- return;
6404
- }
6405
- let pending = changes.length;
6406
- const committed = () => {
6407
- pending -= 1;
6408
- if (pending === 0) afterCommit?.();
6409
- };
6410
- flushSync(() => {
6411
- for (const change of changes) {
6412
- const refresh = this.conditionalListeners.get(change.key)?.get(change.id);
6413
- if (refresh) refresh(change.item, change.index, committed);
6414
- else committed();
6415
- }
6416
- });
6442
+ if (changes.length === 0) afterCommit?.();
6443
+ else options.conditionals.notify(this.conditionalListeners, changes, afterCommit);
6417
6444
  }
6418
6445
  refreshDirtyBindings(dirtyState, afterCommit) {
6419
6446
  if (!this.mounted || !this.root || this.state.fallback || this.hasReactOwnedRows() || this.currentProps.hostBlocks || !this.currentProps.structureDependencies || this.currentProps.structureDependencies.some((dependency) => dirtyState.has(dependency)) || this.currentProps.bindings.some((binding) => binding.dependencies === void 0)) {
@@ -6494,7 +6521,8 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6494
6521
  index += 1;
6495
6522
  }
6496
6523
  const shouldPrepareBindings = rows.items.length > 1 && this.currentProps.bindings.length > 0 && !this.currentProps.hostBlocks;
6497
- const preparedBindings = shouldPrepareBindings ? prepareKeyedRowBindingSetUpdates(this.currentProps, this.instances, rows) : void 0;
6524
+ const ordered = [...this.instances.values()];
6525
+ const preparedBindings = shouldPrepareBindings ? prepareKeyedRowBindingSetUpdates(this.currentProps, this.instances, rows, ordered) : void 0;
6498
6526
  if (shouldPrepareBindings && !preparedBindings) {
6499
6527
  this.activateFallback(afterCommit);
6500
6528
  return true;
@@ -6502,8 +6530,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6502
6530
  const conditionalChanges = [];
6503
6531
  for (index = 0; index < rows.items.length; index += 1) {
6504
6532
  const key = rows.keys[index];
6505
- const existing = this.instances.get(key);
6506
- if (!existing) return false;
6533
+ const existing = ordered[index];
6507
6534
  if (this.currentProps.hostBlocks) {
6508
6535
  const descriptor = this.currentProps.create(rows.items[index], index);
6509
6536
  if (!existing.scope?.update(descriptor)) {
@@ -6540,6 +6567,39 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6540
6567
  this.notifyConditionalChanges(conditionalChanges, afterCommit);
6541
6568
  return true;
6542
6569
  }
6570
+ // Keys are the committed keys plus new ones: patch every existing row like
6571
+ // the complete path does, then create and append only the new rows.
6572
+ reconcileAppendedRows(rows, afterCommit, dirtyState) {
6573
+ const root = this.root;
6574
+ let index = 0;
6575
+ for (const existing of this.instances.values()) {
6576
+ applyKeyedRowBindings(this.currentProps, existing, rows.items[index], index);
6577
+ existing.item = rows.items[index];
6578
+ existing.index = index;
6579
+ index += 1;
6580
+ }
6581
+ const appended = [];
6582
+ for (; index < rows.items.length; index += 1) {
6583
+ appended.push(
6584
+ createKeyedRowInstance(
6585
+ this.currentProps,
6586
+ root.ownerDocument,
6587
+ rows.keys[index],
6588
+ rows.items[index],
6589
+ index
6590
+ )
6591
+ );
6592
+ }
6593
+ const indexElements = this.elementIndexComplete && this.usesDelegatedEvents();
6594
+ for (const instance of appended) {
6595
+ root.appendChild(instance.element);
6596
+ this.instances.set(instance.key, instance);
6597
+ if (indexElements) this.instancesByElement.set(instance.element, instance);
6598
+ }
6599
+ if (!indexElements) this.rebuildElementIndex(this.instances);
6600
+ this.commitCurrentCollection(dirtyState);
6601
+ afterCommit?.();
6602
+ }
6543
6603
  reconcileRemovals(rows, afterCommit, dirtyState) {
6544
6604
  if (this.hasReactOwnedRows() || rows.keys.length >= this.instances.size) return false;
6545
6605
  const removed = [];
@@ -6642,6 +6702,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6642
6702
  this.root.replaceChildren();
6643
6703
  this.instances = /* @__PURE__ */ new Map();
6644
6704
  this.instancesByElement = /* @__PURE__ */ new WeakMap();
6705
+ this.elementIndexComplete = true;
6645
6706
  this.eventHandlers.clear();
6646
6707
  this.conditionalListeners.clear();
6647
6708
  this.commitCurrentCollection(dirtyState);
@@ -6650,7 +6711,11 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6650
6711
  }
6651
6712
  if (this.reconcileStableRows(rows, afterCommit, dirtyState)) return;
6652
6713
  if (this.reconcileRemovals(rows, afterCommit, dirtyState)) return;
6653
- const nextKeys = new Set(rows.keys);
6714
+ if (rows.appended && !this.currentProps.hostBlocks) {
6715
+ this.reconcileAppendedRows(rows, afterCommit, dirtyState);
6716
+ return;
6717
+ }
6718
+ const nextKeys = rows.keySet ?? new Set(rows.keys);
6654
6719
  const hasSurvivingRows = rows.keys.some((key) => this.instances.has(key));
6655
6720
  const preparedBindings = nextKeys.size < this.instances.size && this.currentProps.bindings.length > 0 && !this.currentProps.hostBlocks && rows.keys.every((key) => this.instances.has(key)) ? prepareKeyedRowBindingSetUpdates(this.currentProps, this.instances, rows) : null;
6656
6721
  if (preparedBindings === void 0) {
@@ -6659,7 +6724,8 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6659
6724
  }
6660
6725
  const oldIndices = /* @__PURE__ */ new Map();
6661
6726
  if (hasSurvivingRows) {
6662
- [...this.instances].forEach(([key], index) => oldIndices.set(key, index));
6727
+ let oldIndex = 0;
6728
+ for (const key of this.instances.keys()) oldIndices.set(key, oldIndex++);
6663
6729
  }
6664
6730
  for (const [key, instance] of this.instances) {
6665
6731
  if (!nextKeys.has(key)) {
@@ -6667,7 +6733,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6667
6733
  if (hasSurvivingRows) instance.element.remove();
6668
6734
  }
6669
6735
  }
6670
- const sequence = hasSurvivingRows ? rows.keys.map((key) => oldIndices.get(key) ?? -1) : rows.keys.map(() => -1);
6736
+ const sequence = hasSurvivingRows ? rows.keys.map((key) => oldIndices.get(key) ?? -1) : [];
6671
6737
  const nextInstances = [];
6672
6738
  const conditionalChanges = [];
6673
6739
  for (let index = 0; index < rows.items.length; index += 1) {
@@ -6711,10 +6777,26 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6711
6777
  nextInstances.push(existing);
6712
6778
  continue;
6713
6779
  }
6780
+ if (!this.currentProps.hostBlocks) {
6781
+ const instance = createKeyedRowInstance(
6782
+ this.currentProps,
6783
+ this.root.ownerDocument,
6784
+ key,
6785
+ rows.items[index],
6786
+ index
6787
+ );
6788
+ instance.conditionalValues = this.readConditionalValues(
6789
+ this.currentProps,
6790
+ rows.items[index],
6791
+ index
6792
+ );
6793
+ nextInstances.push(instance);
6794
+ continue;
6795
+ }
6714
6796
  const descriptor = this.currentProps.create(rows.items[index], index);
6715
6797
  const element = createCompilerHostElement(this.root.ownerDocument, descriptor);
6716
6798
  const scope = this.mountRowHostScope(element, descriptor);
6717
- if (this.currentProps.hostBlocks && !scope) {
6799
+ if (!scope) {
6718
6800
  for (const instance of nextInstances) {
6719
6801
  if (!this.instances.has(instance.key)) instance.scope?.cleanup();
6720
6802
  }
@@ -6736,14 +6818,21 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6736
6818
  });
6737
6819
  }
6738
6820
  if (!hasSurvivingRows) {
6739
- const fragment = this.root.ownerDocument.createDocumentFragment();
6740
- for (const instance of nextInstances) fragment.append(instance.element);
6741
- this.root.replaceChildren(fragment);
6821
+ this.root.replaceChildren();
6822
+ for (const instance of nextInstances) this.root.appendChild(instance.element);
6742
6823
  } else {
6743
6824
  reorderCompilerKeyedRows(this.root, nextInstances, sequence, null);
6744
6825
  }
6745
6826
  this.instances = keyedRowInstancesByKey(nextInstances);
6746
- this.rebuildElementIndex(this.instances);
6827
+ if (hasSurvivingRows && this.elementIndexComplete && this.usesDelegatedEvents()) {
6828
+ for (let index = 0; index < nextInstances.length; index += 1) {
6829
+ if (sequence[index] < 0) {
6830
+ this.instancesByElement.set(nextInstances[index].element, nextInstances[index]);
6831
+ }
6832
+ }
6833
+ } else {
6834
+ this.rebuildElementIndex(this.instances);
6835
+ }
6747
6836
  this.pruneEventHandlers();
6748
6837
  this.pruneConditionalListeners();
6749
6838
  this.commitCurrentCollection(dirtyState);
@@ -6781,6 +6870,10 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6781
6870
  }
6782
6871
  if (nextState.fallback || this.state.fallback) return true;
6783
6872
  if (definitionChanged || wasReactOwned || willBeReactOwned) {
6873
+ const elements = this.root?.children;
6874
+ if (elements && (elements.length !== this.reactElements.length || this.reactElements.some((element, index) => element !== elements[index]))) {
6875
+ this.fallbackVersion += 1;
6876
+ }
6784
6877
  this.eventHandlers.clear();
6785
6878
  return true;
6786
6879
  }
@@ -6809,8 +6902,10 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6809
6902
  this.id = void 0;
6810
6903
  this.cleanupHostScopes();
6811
6904
  this.instances.clear();
6905
+ this.reactElements = [];
6812
6906
  this.keyedTargets?.clear();
6813
6907
  this.instancesByElement = /* @__PURE__ */ new WeakMap();
6908
+ this.elementIndexComplete = true;
6814
6909
  this.eventHandlers.clear();
6815
6910
  this.conditionalListeners.clear();
6816
6911
  }
@@ -6824,7 +6919,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6824
6919
  }
6825
6920
  return React.cloneElement(container, {
6826
6921
  ...this.delegatedContainerProps(container),
6827
- key: this.state.fallback ? `react-${this.fallbackVersion}` : "compiled",
6922
+ key: `${this.state.fallback ? "react" : "compiled"}-${this.fallbackVersion}`,
6828
6923
  ref: this.captureRoot
6829
6924
  });
6830
6925
  }
@@ -6962,25 +7057,16 @@ function createKeyedRangesBlockComponent(owner) {
6962
7057
  nextInstances.push(existing);
6963
7058
  continue;
6964
7059
  }
6965
- const element = createCompilerHostElement(
6966
- this.root.ownerDocument,
6967
- range.create(rows.items[index], index)
7060
+ nextInstances.push(
7061
+ createKeyedRowInstance(range, this.root.ownerDocument, key, rows.items[index], index)
6968
7062
  );
6969
- nextInstances.push({
6970
- key,
6971
- element,
6972
- values: readKeyedRowBindingValues(range, rows.items[index], index),
6973
- item: rows.items[index],
6974
- index,
6975
- conditionalValues: /* @__PURE__ */ new Map()
6976
- });
6977
7063
  }
6978
7064
  let anchor = this.anchorAfter(rangeIndex);
6979
7065
  for (let index = nextInstances.length - 1; index >= 0; index -= 1) {
6980
7066
  const instance = nextInstances[index];
6981
7067
  if (sequence[index] < 0) {
6982
7068
  this.root.insertBefore(instance.element, anchor);
6983
- } else if (!stablePositions.has(index) && instance.element.nextSibling !== anchor) {
7069
+ } else if (!stablePositions[index] && instance.element.nextSibling !== anchor) {
6984
7070
  this.root.insertBefore(instance.element, anchor);
6985
7071
  }
6986
7072
  anchor = instance.element;