@farm.js/react 0.1.0-beta.41 → 0.1.0-beta.42

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.
@@ -8440,6 +8440,42 @@ var mixedRangesRuntimeFeature = {
8440
8440
  MixedRanges: createMixedRangesBlockComponent(owner)
8441
8441
  })
8442
8442
  };
8443
+ var compilerEffectStates = /* @__PURE__ */ new WeakMap();
8444
+ function runCompilerEffects(component, effects) {
8445
+ let states = compilerEffectStates.get(component);
8446
+ if (!effects) {
8447
+ const mounted = states?.splice(0) ?? [];
8448
+ for (const layout of [true, false]) {
8449
+ for (const state of mounted) if (state?.layout === layout) state.cleanup?.();
8450
+ }
8451
+ return;
8452
+ }
8453
+ if (!states) compilerEffectStates.set(component, states = []);
8454
+ const changed = [];
8455
+ for (let index = 0; index < effects.length; index += 1) {
8456
+ const dependencies = effects[index][1];
8457
+ const previous = states[index];
8458
+ if (!previous || !dependencies || !previous.dependencies || dependencies.length !== previous.dependencies.length || dependencies.some((value, position) => !Object.is(value, previous.dependencies[position]))) {
8459
+ changed.push(index);
8460
+ }
8461
+ }
8462
+ for (const layout of [true, false]) {
8463
+ const phase = changed.filter((index) => Boolean(effects[index][2]) === layout);
8464
+ for (const index of phase) states[index]?.cleanup?.();
8465
+ for (const index of phase) {
8466
+ const cleanup = effects[index][0]();
8467
+ states[index] = {
8468
+ dependencies: effects[index][1],
8469
+ cleanup: typeof cleanup === "function" ? cleanup : void 0,
8470
+ layout
8471
+ };
8472
+ }
8473
+ }
8474
+ }
8475
+ var effectsRuntimeFeature = {
8476
+ name: "effects",
8477
+ create: () => ({ effects: runCompilerEffects })
8478
+ };
8443
8479
  var componentRuntimeFeature = {
8444
8480
  name: "component",
8445
8481
  create: (owner) => ({
@@ -8565,7 +8601,7 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8565
8601
  getDefinitionVersion: () => definitionReference.current
8566
8602
  };
8567
8603
  const blockRuntime = {
8568
- target: (id) => this.bindingTarget(id)
8604
+ target: (id, ref) => this.bindingTarget(id, ref)
8569
8605
  };
8570
8606
  for (const feature of runtimeFeatures) {
8571
8607
  Object.assign(blockRuntime, feature.create(featureOwner));
@@ -8665,7 +8701,7 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8665
8701
  this.bindingValues.set(binding, this.bindingValue(binding, value));
8666
8702
  }
8667
8703
  }
8668
- bindingTarget(id) {
8704
+ bindingTarget(id, ref) {
8669
8705
  const existing = this.bindingTargetRefs.get(id);
8670
8706
  if (existing) return existing;
8671
8707
  const capture = (element) => {
@@ -8674,6 +8710,7 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8674
8710
  } else {
8675
8711
  this.bindingTargets.delete(id);
8676
8712
  }
8713
+ if (ref) ref.current = element;
8677
8714
  };
8678
8715
  this.bindingTargetRefs.set(id, capture);
8679
8716
  return capture;
@@ -8690,11 +8727,20 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8690
8727
  direction: active.selectionDirection || "none"
8691
8728
  };
8692
8729
  }
8693
- restoreInputSelection(snapshot) {
8694
- if (!snapshot || !snapshot.element.isConnected || snapshot.element.ownerDocument.activeElement !== snapshot.element) {
8695
- return;
8730
+ /**
8731
+ * Runs once compiled DOM updates have committed: restores the focused field's selection, then
8732
+ * runs the effects.
8733
+ */
8734
+ afterCommit(snapshot) {
8735
+ if (snapshot && snapshot.element.isConnected && snapshot.element.ownerDocument.activeElement === snapshot.element) {
8736
+ snapshot.element.setSelectionRange(snapshot.start, snapshot.end, snapshot.direction);
8737
+ }
8738
+ if (this.mounted) {
8739
+ this.blockRuntime.effects?.(
8740
+ this,
8741
+ definitionReference.current.effects?.(this.props, this.cells)
8742
+ );
8696
8743
  }
8697
- snapshot.element.setSelectionRange(snapshot.start, snapshot.end, snapshot.direction);
8698
8744
  }
8699
8745
  setBlockRoot(id, root, expectedRoot) {
8700
8746
  const previous = this.blockRoots.get(id);
@@ -8725,6 +8771,7 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8725
8771
  this.flushBindingUpdates();
8726
8772
  });
8727
8773
  }
8774
+ /** Returns true when it committed an update, after which it has run the effects. */
8728
8775
  flushBindingUpdates() {
8729
8776
  const inputSelection = this.inputSelection;
8730
8777
  this.inputSelection = null;
@@ -8771,15 +8818,16 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8771
8818
  for (const refresh of blockRefreshes) {
8772
8819
  refresh(() => {
8773
8820
  pendingBlockCommits -= 1;
8774
- if (pendingBlockCommits === 0) this.restoreInputSelection(inputSelection);
8821
+ if (pendingBlockCommits === 0) this.afterCommit(inputSelection);
8775
8822
  }, dirty);
8776
8823
  }
8777
- if (blockRefreshes.length === 0) this.restoreInputSelection(inputSelection);
8824
+ if (blockRefreshes.length === 0) this.afterCommit(inputSelection);
8778
8825
  } catch (error) {
8779
8826
  this.bindingError = error;
8780
8827
  this.hasBindingError = true;
8781
8828
  this.forceUpdate();
8782
8829
  }
8830
+ return true;
8783
8831
  }
8784
8832
  applyBinding(binding, force = false) {
8785
8833
  if (!this.root || binding.kind === "block") return;
@@ -8846,9 +8894,10 @@ function createCompiledComponentRuntime(definitionReference, runtimeFeatures, re
8846
8894
  this.applyBinding(binding, true);
8847
8895
  }
8848
8896
  }
8849
- if (this.dirtyState.size) this.flushBindingUpdates();
8897
+ if (!(this.dirtyState.size && this.flushBindingUpdates())) this.afterCommit(null);
8850
8898
  }
8851
8899
  componentWillUnmount() {
8900
+ this.blockRuntime.effects?.(this);
8852
8901
  this.mounted = false;
8853
8902
  this.inputSelection = null;
8854
8903
  this.blockRefreshListeners.clear();
@@ -8915,7 +8964,8 @@ var COMPLETE_RUNTIME_FEATURES = [
8915
8964
  keyedRowsCompleteCompatibilityRuntimeFeature,
8916
8965
  keyedRangesRuntimeFeature,
8917
8966
  mixedRangesRuntimeFeature,
8918
- componentRuntimeFeature
8967
+ componentRuntimeFeature,
8968
+ effectsRuntimeFeature
8919
8969
  ];
8920
8970
  function createCompiledComponent(definition) {
8921
8971
  return createCompiledComponentWithFeatures(definition, COMPLETE_RUNTIME_FEATURES);
@@ -9343,6 +9393,7 @@ exports.createCompilerKeyedArrayStructuralSort = createCompilerKeyedArrayStructu
9343
9393
  exports.createCompilerKeyedArrayWindowReplace = createCompilerKeyedArrayWindowReplace;
9344
9394
  exports.createCompilerKeyedCollectionUpdate = createCompilerKeyedCollectionUpdate;
9345
9395
  exports.createCompilerKeyedMapUpdate = createCompilerKeyedMapUpdate;
9396
+ exports.effectsRuntimeFeature = effectsRuntimeFeature;
9346
9397
  exports.finalizeCompilerKeyedArrayMappedStructuralUpdate = finalizeCompilerKeyedArrayMappedStructuralUpdate;
9347
9398
  exports.hostConditionalRuntimeFeature = hostConditionalRuntimeFeature;
9348
9399
  exports.keyedListRuntimeFeature = keyedListRuntimeFeature;