@farm.js/react 0.1.0-beta.23 → 0.1.0-beta.24

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.
package/README.md CHANGED
@@ -289,6 +289,12 @@ itself, and failed runtime checks use complete keyed reconciliation. A key that
289
289
  collection prevents hint emission entirely. The compiler report exposes the emitted-site count as
290
290
  `keyedArrayAppendHints`.
291
291
 
292
+ When all keyed-row plans in a component are plain, target-free rows and the only emitted update
293
+ hints are appends, the compiler selects a smaller append-only runtime. This omits unused map-update
294
+ and keyed-target fast paths, not support for other state changes: opaque updates and rejected hints
295
+ still use complete keyed reconciliation. Mixed hints, targeted bindings, conditional rows, and host
296
+ blocks keep their existing runtime. No additional option is required.
297
+
292
298
  Adjacent removal and append setters can share the same committed-row proof:
293
299
 
294
300
  ```tsx
@@ -2028,16 +2028,25 @@ function applyStaticRangeBindings(bindings, segments, values) {
2028
2028
  return true;
2029
2029
  }
2030
2030
  var COMPILER_KEYED_ROLLING_WINDOW_DELTAS = /* @__PURE__ */ new WeakMap();
2031
+ var COMPILER_KEYED_APPEND_DELTAS = /* @__PURE__ */ new WeakMap();
2031
2032
  function keyedRowInstancesByKey(instances) {
2032
2033
  return new Map(instances.map((instance) => [instance.key, instance]));
2033
2034
  }
2034
2035
  var UNSET_KEYED_ROW_BINDING = /* @__PURE__ */ Symbol("unset interactive keyed-row binding");
2036
+ var KEYED_ROWS_MAX_MOVED = 64;
2035
2037
  function keyedRowIdentity(key) {
2036
- return String(key);
2038
+ if (typeof key === "number") return key;
2039
+ const text = String(key);
2040
+ const first = text.charCodeAt(0);
2041
+ if (first >= 48 && first <= 57 || first === 45 || first === 73 || first === 78) {
2042
+ const number = Number(text);
2043
+ if (String(number) === text) return number;
2044
+ }
2045
+ return text;
2037
2046
  }
2038
2047
  function keyedIdentityTargetSnapshot(value) {
2039
2048
  const kind = typeof value;
2040
- return value === null || value === void 0 || kind === "string" || kind === "number" || kind === "bigint" ? { eligible: true, key: String(value) } : { eligible: false };
2049
+ return value === null || value === void 0 || kind === "string" || kind === "number" || kind === "bigint" ? { eligible: true, key: keyedRowIdentity(value) } : { eligible: false };
2041
2050
  }
2042
2051
  var NATIVE_SET_PROTOTYPE = Set.prototype;
2043
2052
  var NATIVE_SET_ADD = Set.prototype.add;
@@ -2083,7 +2092,7 @@ function keyedMembershipDeltaSnapshot(value, previous) {
2083
2092
  const before = keyedMembershipSnapshotHas(previous, key);
2084
2093
  if (before === void 0) return void 0;
2085
2094
  changes.set(key, present);
2086
- if (before !== present) NATIVE_SET_ADD.call(targetedKeys, String(key));
2095
+ if (before !== present) NATIVE_SET_ADD.call(targetedKeys, keyedRowIdentity(key));
2087
2096
  }
2088
2097
  } catch {
2089
2098
  return void 0;
@@ -2137,10 +2146,10 @@ function keyedMembershipChangedKeys(previousSnapshot, nextSnapshot) {
2137
2146
  const keys = /* @__PURE__ */ new Set();
2138
2147
  if (!previous || !next) return keys;
2139
2148
  for (const value of previous) {
2140
- if (!NATIVE_SET_HAS.call(next, value)) NATIVE_SET_ADD.call(keys, String(value));
2149
+ if (!NATIVE_SET_HAS.call(next, value)) NATIVE_SET_ADD.call(keys, keyedRowIdentity(value));
2141
2150
  }
2142
2151
  for (const value of next) {
2143
- if (!NATIVE_SET_HAS.call(previous, value)) NATIVE_SET_ADD.call(keys, String(value));
2152
+ if (!NATIVE_SET_HAS.call(previous, value)) NATIVE_SET_ADD.call(keys, keyedRowIdentity(value));
2144
2153
  }
2145
2154
  return keys;
2146
2155
  }
@@ -2201,7 +2210,7 @@ function keyedMapLookupDeltaSnapshot(value, previous) {
2201
2210
  if (!before) return void 0;
2202
2211
  changes.set(key, { present, ...present ? { value: entry } : {} });
2203
2212
  if (before.present !== present || !Object.is(before.value, entry)) {
2204
- NATIVE_SET_ADD.call(targetedKeys, String(key));
2213
+ NATIVE_SET_ADD.call(targetedKeys, keyedRowIdentity(key));
2205
2214
  }
2206
2215
  }
2207
2216
  } catch {
@@ -2259,12 +2268,12 @@ function keyedMapLookupChangedKeys(previousSnapshot, nextSnapshot) {
2259
2268
  if (!previous || !next) return keys;
2260
2269
  for (const [key, value] of previous) {
2261
2270
  if (!Object.is(value, NATIVE_MAP_GET.call(next, key))) {
2262
- NATIVE_SET_ADD.call(keys, String(key));
2271
+ NATIVE_SET_ADD.call(keys, keyedRowIdentity(key));
2263
2272
  }
2264
2273
  }
2265
2274
  for (const [key, value] of next) {
2266
2275
  if (!Object.is(value, NATIVE_MAP_GET.call(previous, key))) {
2267
- NATIVE_SET_ADD.call(keys, String(key));
2276
+ NATIVE_SET_ADD.call(keys, keyedRowIdentity(key));
2268
2277
  }
2269
2278
  }
2270
2279
  return keys;
@@ -3920,7 +3929,7 @@ function createHostConditionalBlockComponent(owner) {
3920
3929
  schedulePropSync() {
3921
3930
  if (this.propSyncQueued) return;
3922
3931
  this.propSyncQueued = true;
3923
- queueMicrotask(() => {
3932
+ scheduleCompilerMicrotask(() => {
3924
3933
  this.propSyncQueued = false;
3925
3934
  if (this.mounted && !this.state.fallback) {
3926
3935
  this.listen();
@@ -4378,6 +4387,18 @@ function reorderCompilerKeyedRows(root, instances, sequence, anchor) {
4378
4387
  }
4379
4388
  restoreCompilerFocus(activeElement);
4380
4389
  }
4390
+ var RESOLVED_COMPILER_MICROTASK = Promise.resolve();
4391
+ function scheduleCompilerMicrotask(callback) {
4392
+ void RESOLVED_COMPILER_MICROTASK.then(() => {
4393
+ try {
4394
+ callback();
4395
+ } catch (error) {
4396
+ queueMicrotask(() => {
4397
+ throw error;
4398
+ });
4399
+ }
4400
+ });
4401
+ }
4381
4402
  function compilerFocusedElement(root) {
4382
4403
  const activeElement = root.ownerDocument.activeElement;
4383
4404
  return activeElement && root.contains(activeElement) ? activeElement : null;
@@ -4406,6 +4427,11 @@ var keyedUpdateRuntime = {
4406
4427
  commit: commitCompilerKeyedCollection,
4407
4428
  reconcile: reconcileCompilerKeyedMapUpdate
4408
4429
  };
4430
+ var keyedAppendOnlyUpdateRuntime = {
4431
+ append: reconcileCompilerKeyedArrayAppend,
4432
+ commit: commitCompilerKeyedCollection,
4433
+ reconcile: () => void 0
4434
+ };
4409
4435
  var keyedFilterUpdateRuntime = {
4410
4436
  ...keyedUpdateRuntime,
4411
4437
  filter: reconcileCompilerKeyedArrayFilter
@@ -4631,13 +4657,14 @@ function reconcileCompilerKeyedArrayAppend(props, dirtyState, collectionToken, i
4631
4657
  const finalValue = props.items();
4632
4658
  const startIndex = compilerKeyedArrayAppendStart(finalValue, collectionToken, instances.size);
4633
4659
  if (startIndex === void 0 || !Array.isArray(finalValue)) return void 0;
4634
- const nextInstances = new Map(instances);
4660
+ const appendedKeys = /* @__PURE__ */ new Set();
4635
4661
  const appended = [];
4636
4662
  try {
4637
4663
  for (let index = startIndex; index < finalValue.length; index += 1) {
4638
4664
  const item = finalValue[index];
4639
4665
  const key = keyedRowIdentity(props.rowKey(item, index));
4640
- if (nextInstances.has(key)) return void 0;
4666
+ if (instances.has(key) || appendedKeys.has(key)) return void 0;
4667
+ appendedKeys.add(key);
4641
4668
  const instance = createKeyedRowInstance(
4642
4669
  props,
4643
4670
  root.ownerDocument,
@@ -4646,7 +4673,6 @@ function reconcileCompilerKeyedArrayAppend(props, dirtyState, collectionToken, i
4646
4673
  index
4647
4674
  );
4648
4675
  appended.push(instance);
4649
- nextInstances.set(key, instance);
4650
4676
  }
4651
4677
  } catch {
4652
4678
  return void 0;
@@ -4656,7 +4682,10 @@ function reconcileCompilerKeyedArrayAppend(props, dirtyState, collectionToken, i
4656
4682
  for (const instance of appended) fragment.append(instance.element);
4657
4683
  root.append(fragment);
4658
4684
  }
4659
- return nextInstances;
4685
+ const mutableInstances = instances;
4686
+ for (const instance of appended) mutableInstances.set(instance.key, instance);
4687
+ COMPILER_KEYED_APPEND_DELTAS.set(mutableInstances, appended);
4688
+ return mutableInstances;
4660
4689
  }
4661
4690
  function reconcileCompilerKeyedArrayStructuralAppend(props, dirtyState, collectionToken, instances, root, reactOwnedRows) {
4662
4691
  if (reactOwnedRows || props.hostBlocks || (props.conditionals?.length || 0) > 0 || !props.filterIndexIndependent || props.collectionDependency === void 0) {
@@ -6139,8 +6168,16 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6139
6168
  this.hasReactOwnedRows()
6140
6169
  );
6141
6170
  if (appendedInstances) {
6171
+ const appended = COMPILER_KEYED_APPEND_DELTAS.get(appendedInstances);
6172
+ COMPILER_KEYED_APPEND_DELTAS.delete(appendedInstances);
6142
6173
  this.instances = appendedInstances instanceof Map ? appendedInstances : new Map(appendedInstances);
6143
- this.rebuildElementIndex(this.instances);
6174
+ if (appended && this.elementIndexComplete) {
6175
+ for (const instance of appended) {
6176
+ this.instancesByElement.set(instance.element, instance);
6177
+ }
6178
+ } else {
6179
+ this.rebuildElementIndex(this.instances);
6180
+ }
6144
6181
  this.commitCurrentCollection(dirtyState);
6145
6182
  afterCommit?.();
6146
6183
  return;
@@ -6256,15 +6293,41 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6256
6293
  const keys = [];
6257
6294
  const committedSize = this.instances.size;
6258
6295
  let prefix = 0;
6296
+ let tracking = true;
6297
+ let moved;
6298
+ let displaced;
6259
6299
  const committed = this.instances.keys();
6260
6300
  for (let index = 0; index < items.length; index += 1) {
6261
6301
  const key = keyedRowIdentity(props.rowKey(items[index], index));
6262
6302
  keys.push(key);
6263
- if (prefix === index && index < committedSize && committed.next().value === key) {
6264
- prefix += 1;
6303
+ if (!tracking || index >= committedSize) continue;
6304
+ const committedKey = committed.next().value;
6305
+ if (committedKey === key) {
6306
+ if (prefix === index) prefix += 1;
6307
+ continue;
6265
6308
  }
6309
+ if (items.length !== committedSize || moved && moved.length === KEYED_ROWS_MAX_MOVED) {
6310
+ tracking = false;
6311
+ moved = displaced = void 0;
6312
+ continue;
6313
+ }
6314
+ (moved ?? (moved = [])).push(index);
6315
+ (displaced ?? (displaced = [])).push(committedKey);
6266
6316
  }
6267
6317
  if (prefix === committedSize && keys.length === committedSize) return { items, keys };
6318
+ if (moved && displaced) {
6319
+ const pending = /* @__PURE__ */ new Map();
6320
+ for (let index = 0; index < moved.length; index += 1) pending.set(displaced[index], index);
6321
+ const sources = [];
6322
+ for (const position of moved) {
6323
+ const source = pending.get(keys[position]);
6324
+ if (source === void 0) break;
6325
+ pending.delete(keys[position]);
6326
+ sources.push(source);
6327
+ }
6328
+ if (sources.length === moved.length)
6329
+ return { items, keys, moved: { positions: moved, sources } };
6330
+ }
6268
6331
  if (prefix === committedSize && committedSize > 0) {
6269
6332
  const tail = /* @__PURE__ */ new Set();
6270
6333
  for (let index = committedSize; index < keys.length; index += 1) {
@@ -6526,22 +6589,35 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6526
6589
  if (rows.keys[index] !== key) return false;
6527
6590
  index += 1;
6528
6591
  }
6592
+ const conditionalChanges = this.patchOrderedRows(
6593
+ rows,
6594
+ [...this.instances.values()],
6595
+ afterCommit
6596
+ );
6597
+ if (!conditionalChanges) return true;
6598
+ this.commitCurrentCollection(dirtyState);
6599
+ this.notifyConditionalChanges(conditionalChanges, afterCommit);
6600
+ return true;
6601
+ }
6602
+ // Rereads and patches every row as React would rerender it. `ordered` holds
6603
+ // the existing instance for each row. Returns undefined after activating
6604
+ // the fallback.
6605
+ patchOrderedRows(rows, ordered, afterCommit) {
6529
6606
  const shouldPrepareBindings = rows.items.length > 1 && this.currentProps.bindings.length > 0 && !this.currentProps.hostBlocks;
6530
- const ordered = [...this.instances.values()];
6531
6607
  const preparedBindings = shouldPrepareBindings ? prepareKeyedRowBindingSetUpdates(this.currentProps, this.instances, rows, ordered) : void 0;
6532
6608
  if (shouldPrepareBindings && !preparedBindings) {
6533
6609
  this.activateFallback(afterCommit);
6534
- return true;
6610
+ return void 0;
6535
6611
  }
6536
6612
  const conditionalChanges = [];
6537
- for (index = 0; index < rows.items.length; index += 1) {
6613
+ for (let index = 0; index < rows.items.length; index += 1) {
6538
6614
  const key = rows.keys[index];
6539
6615
  const existing = ordered[index];
6540
6616
  if (this.currentProps.hostBlocks) {
6541
6617
  const descriptor = this.currentProps.create(rows.items[index], index);
6542
6618
  if (!existing.scope?.update(descriptor)) {
6543
6619
  this.activateFallback(afterCommit);
6544
- return true;
6620
+ return void 0;
6545
6621
  }
6546
6622
  }
6547
6623
  const bindingUpdates = preparedBindings?.[index];
@@ -6569,9 +6645,36 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6569
6645
  existing.item = rows.items[index];
6570
6646
  existing.index = index;
6571
6647
  }
6648
+ return conditionalChanges;
6649
+ }
6650
+ // The same rows in a new order that differs only at the ascending `moved`
6651
+ // positions: patch every row, then move only moved rows outside a stable
6652
+ // subsequence. A moved row may stay put only when every position between
6653
+ // its committed and final place also moved; otherwise an unmoved row lies
6654
+ // between them and the order would be wrong. Inserting the rest from the
6655
+ // last position down puts each before its final next sibling.
6656
+ reconcileMovedRows(rows, moved, sources, afterCommit, dirtyState) {
6657
+ const ordered = [...this.instances.values()];
6658
+ for (const position of moved) ordered[position] = this.instances.get(rows.keys[position]);
6659
+ const conditionalChanges = this.patchOrderedRows(rows, ordered, afterCommit);
6660
+ if (!conditionalChanges) return;
6661
+ const sequence = sources.map(
6662
+ (source, index) => Math.abs(moved[source] - moved[index]) === Math.abs(source - index) ? source : -1
6663
+ );
6664
+ const stays = longestIncreasingSubsequencePositions(sequence);
6665
+ const root = this.root;
6666
+ const activeElement = compilerFocusedElement(root);
6667
+ for (let index = moved.length - 1; index >= 0; index -= 1) {
6668
+ if (stays[index]) continue;
6669
+ const position = moved[index];
6670
+ const element = ordered[position].element;
6671
+ const anchor = position + 1 < ordered.length ? ordered[position + 1].element : null;
6672
+ if (element.nextSibling !== anchor) root.insertBefore(element, anchor);
6673
+ }
6674
+ restoreCompilerFocus(activeElement);
6675
+ this.instances = keyedRowInstancesByKey(ordered);
6572
6676
  this.commitCurrentCollection(dirtyState);
6573
6677
  this.notifyConditionalChanges(conditionalChanges, afterCommit);
6574
- return true;
6575
6678
  }
6576
6679
  // Keys are the committed keys plus new ones: patch every existing row like
6577
6680
  // the complete path does, then create and append only the new rows.
@@ -6716,6 +6819,16 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6716
6819
  return;
6717
6820
  }
6718
6821
  if (this.reconcileStableRows(rows, afterCommit, dirtyState)) return;
6822
+ if (rows.moved && !this.currentProps.hostBlocks) {
6823
+ this.reconcileMovedRows(
6824
+ rows,
6825
+ rows.moved.positions,
6826
+ rows.moved.sources,
6827
+ afterCommit,
6828
+ dirtyState
6829
+ );
6830
+ return;
6831
+ }
6719
6832
  if (this.reconcileRemovals(rows, afterCommit, dirtyState)) return;
6720
6833
  if (rows.appended && !this.currentProps.hostBlocks) {
6721
6834
  this.reconcileAppendedRows(rows, afterCommit, dirtyState);
@@ -6855,7 +6968,7 @@ function createKeyedRowsBlockComponentCore(owner, options = {}) {
6855
6968
  schedulePropSync() {
6856
6969
  if (this.propSyncQueued) return;
6857
6970
  this.propSyncQueued = true;
6858
- queueMicrotask(() => {
6971
+ scheduleCompilerMicrotask(() => {
6859
6972
  this.propSyncQueued = false;
6860
6973
  if (this.mounted && !this.state.fallback) {
6861
6974
  this.listen();
@@ -7443,6 +7556,14 @@ var keyedRowsHintedRuntimeFeature = {
7443
7556
  })
7444
7557
  })
7445
7558
  };
7559
+ var keyedRowsAppendOnlyRuntimeFeature = {
7560
+ name: "keyed-rows:append-only",
7561
+ create: (owner) => ({
7562
+ KeyedRows: createKeyedRowsBlockComponentCore(owner, {
7563
+ keyedUpdates: keyedAppendOnlyUpdateRuntime
7564
+ })
7565
+ })
7566
+ };
7446
7567
  var keyedRowsPositionHintedRuntimeFeature = {
7447
7568
  name: "keyed-rows:position-hinted",
7448
7569
  create: (owner) => ({
@@ -8374,7 +8495,7 @@ function createCompiledComponentWithFeatures(definition, runtimeFeatures) {
8374
8495
  this.captureInputSelection();
8375
8496
  if (!this.mounted || this.flushQueued) return;
8376
8497
  this.flushQueued = true;
8377
- queueMicrotask(() => {
8498
+ scheduleCompilerMicrotask(() => {
8378
8499
  this.flushQueued = false;
8379
8500
  this.flushBindingUpdates();
8380
8501
  });
@@ -8623,6 +8744,7 @@ exports.hostConditionalRuntimeFeature = hostConditionalRuntimeFeature;
8623
8744
  exports.keyedListRuntimeFeature = keyedListRuntimeFeature;
8624
8745
  exports.keyedRangesRuntimeFeature = keyedRangesRuntimeFeature;
8625
8746
  exports.keyedRowsAllHintedRuntimeFeature = keyedRowsAllHintedRuntimeFeature;
8747
+ exports.keyedRowsAppendOnlyRuntimeFeature = keyedRowsAppendOnlyRuntimeFeature;
8626
8748
  exports.keyedRowsBatchEveryHintedRuntimeFeature = keyedRowsBatchEveryHintedRuntimeFeature;
8627
8749
  exports.keyedRowsBatchPositionHintedRuntimeFeature = keyedRowsBatchPositionHintedRuntimeFeature;
8628
8750
  exports.keyedRowsCompleteAllHintedRuntimeFeature = keyedRowsCompleteAllHintedRuntimeFeature;