@reckona/mreact-compat 0.0.163 → 0.0.165

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/dist/hooks.js CHANGED
@@ -25,6 +25,7 @@ let currentEventPriority = "default";
25
25
  let eventRerenderScheduled = false;
26
26
  let automaticRerenderScheduled = false;
27
27
  let effectFlushRerenderDepth = 0;
28
+ let hostCommitRerenderDepth = 0;
28
29
  let strictMemoOwnerId = 0;
29
30
  const strictMemoObjectOwnerIds = new WeakMap();
30
31
  const queuedTransitionRerenders = new Map();
@@ -144,8 +145,6 @@ export function createRootRuntime(rerender, options = {}) {
144
145
  hookRenderState.currentInstance = undefined;
145
146
  if (committed) {
146
147
  flushProfilerCommits(this, profilerCommits);
147
- flushHostCommitRerenders();
148
- this.externalStoreUpdate = false;
149
148
  }
150
149
  },
151
150
  flushEffects() {
@@ -168,7 +167,9 @@ export function createRootRuntime(rerender, options = {}) {
168
167
  this.effectFlushPhase = undefined;
169
168
  this.profilerFlushDepth -= 1;
170
169
  if (this.profilerFlushDepth === 0) {
170
+ flushHostCommitRerenders();
171
171
  flushEffectFlushRerenders();
172
+ this.externalStoreUpdate = false;
172
173
  }
173
174
  }
174
175
  },
@@ -537,6 +538,14 @@ function clearReactiveTextBinding(node) {
537
538
  previous.subscribers.delete(node);
538
539
  reactiveTextBindingsByNode.delete(node);
539
540
  }
541
+ function clearReactiveTextBindingSubscribers(binding) {
542
+ for (const node of binding.subscribers) {
543
+ if (reactiveTextBindingsByNode.get(node) === binding) {
544
+ reactiveTextBindingsByNode.delete(node);
545
+ }
546
+ }
547
+ binding.subscribers.clear();
548
+ }
540
549
  function getStateTextBinding(slot) {
541
550
  slot.textBinding ??= {
542
551
  value: slot.value,
@@ -871,13 +880,25 @@ export function useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)
871
880
  }
872
881
  const nextSnapshot = getSnapshot();
873
882
  if (!Object.is(slot.value, nextSnapshot)) {
883
+ if (hookRenderState.hostCommitDepth > 0 && !Object.hasOwn(slot, "hostCommitValue")) {
884
+ slot.hostCommitValue = slot.value;
885
+ }
874
886
  slot.value = nextSnapshot;
875
- instance.dirty = true;
876
887
  runtime.externalStoreUpdate = true;
888
+ if (hookRenderState.hostCommitDepth > 0) {
889
+ updateHostCommitDirtyState(instance);
890
+ hookRenderState.queuedHostCommitRerenders.add(runtime);
891
+ return;
892
+ }
893
+ instance.dirty = true;
877
894
  if (runtime.profilerFlushDepth > 0) {
878
895
  hookRenderState.queuedEffectFlushRerenders.add(runtime);
879
896
  return;
880
897
  }
898
+ if (eventBatchDepth > 0) {
899
+ queueEventRerender(runtime);
900
+ return;
901
+ }
881
902
  runtime.rerender("sync");
882
903
  }
883
904
  };
@@ -1059,7 +1080,7 @@ export function startTransition(scope) {
1059
1080
  runTransitionScope(scope, context);
1060
1081
  }
1061
1082
  /** Runs a callback while updates are batched at the requested event priority. */
1062
- export function runWithEventPriority(priority, callback) {
1083
+ export function runWithEventPriority(priority, callback, deferFlush) {
1063
1084
  const previousPriority = currentEventPriority;
1064
1085
  currentEventPriority = priority;
1065
1086
  eventBatchDepth += 1;
@@ -1070,7 +1091,15 @@ export function runWithEventPriority(priority, callback) {
1070
1091
  eventBatchDepth -= 1;
1071
1092
  currentEventPriority = previousPriority;
1072
1093
  if (eventBatchDepth === 0) {
1073
- flushEventRerendersForPriority(priority);
1094
+ const flush = () => {
1095
+ flushEventRerendersForPriority(priority);
1096
+ };
1097
+ if (deferFlush === undefined) {
1098
+ flush();
1099
+ }
1100
+ else {
1101
+ deferFlush(flush);
1102
+ }
1074
1103
  }
1075
1104
  }
1076
1105
  }
@@ -1401,18 +1430,28 @@ function scheduleInstanceUpdate(runtime, instance, options = {}) {
1401
1430
  scheduleRuntimeRerender(runtime, options);
1402
1431
  }
1403
1432
  function flushHostCommitRerenders() {
1404
- if (hookRenderState.hostCommitDepth > 0 ||
1433
+ if (hostCommitRerenderDepth > 0 ||
1434
+ hookRenderState.hostCommitDepth > 0 ||
1405
1435
  hookRenderState.queuedHostCommitRerenders.size === 0) {
1406
1436
  return;
1407
1437
  }
1408
- const runtimes = [...hookRenderState.queuedHostCommitRerenders];
1409
- hookRenderState.queuedHostCommitRerenders.clear();
1410
- for (const runtime of runtimes) {
1411
- const hasDirtyInstance = Array.from(runtime.instances.values()).some((instance) => instance.dirty);
1412
- clearHostCommitStateBaselines(runtime);
1413
- if (hasDirtyInstance) {
1414
- runtime.rerender("sync");
1438
+ hostCommitRerenderDepth += 1;
1439
+ try {
1440
+ for (let attempt = 0; attempt < 3 && hookRenderState.queuedHostCommitRerenders.size > 0; attempt += 1) {
1441
+ const runtimes = [...hookRenderState.queuedHostCommitRerenders];
1442
+ hookRenderState.queuedHostCommitRerenders.clear();
1443
+ for (const runtime of runtimes) {
1444
+ const hasDirtyInstance = Array.from(runtime.instances.values()).some((instance) => instance.dirty);
1445
+ clearHostCommitStateBaselines(runtime);
1446
+ if (hasDirtyInstance) {
1447
+ runtime.rerender("sync");
1448
+ }
1449
+ }
1415
1450
  }
1451
+ hookRenderState.queuedHostCommitRerenders.clear();
1452
+ }
1453
+ finally {
1454
+ hostCommitRerenderDepth -= 1;
1416
1455
  }
1417
1456
  }
1418
1457
  function flushEffectFlushRerenders() {
@@ -1439,14 +1478,14 @@ function flushEffectFlushRerenders() {
1439
1478
  }
1440
1479
  }
1441
1480
  function updateHostCommitDirtyState(instance) {
1442
- instance.dirty = instance.hooks.some((slot) => slot.kind === "state" &&
1481
+ instance.dirty = instance.hooks.some((slot) => (slot.kind === "state" || slot.kind === "store") &&
1443
1482
  Object.hasOwn(slot, "hostCommitValue") &&
1444
1483
  !Object.is(slot.hostCommitValue, slot.value));
1445
1484
  }
1446
1485
  function clearHostCommitStateBaselines(runtime) {
1447
1486
  for (const instance of runtime.instances.values()) {
1448
1487
  for (const slot of instance.hooks) {
1449
- if (slot.kind === "state") {
1488
+ if (slot.kind === "state" || slot.kind === "store") {
1450
1489
  delete slot.hostCommitValue;
1451
1490
  }
1452
1491
  }
@@ -1558,6 +1597,9 @@ function cleanupInstance(instance) {
1558
1597
  slot.cleanup?.();
1559
1598
  delete slot.cleanup;
1560
1599
  }
1600
+ else if (slot?.kind === "state" && slot.textBinding !== undefined) {
1601
+ clearReactiveTextBindingSubscribers(slot.textBinding);
1602
+ }
1561
1603
  }
1562
1604
  }
1563
1605
  function requireRuntime() {