@rectify-dev/core 2.0.2 → 2.1.0

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/index.cjs CHANGED
@@ -247,6 +247,7 @@ var PlacementFlag = 1 << 0;
247
247
  var UpdateFlag = 1 << 1;
248
248
  var DeletionFlag = 1 << 2;
249
249
  var MoveFlag = 1 << 3;
250
+ var RefFlag = 1 << 4;
250
251
 
251
252
  // ../rectify-reconciler/src/RectifyFiberLanes.ts
252
253
  var NoLanes = 0;
@@ -367,7 +368,8 @@ var createFiber = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((workTag, pendingProp
367
368
  childLanes: NoLanes,
368
369
  subtreeFlags: NoFlags,
369
370
  flags: NoFlags,
370
- memoizedState: null
371
+ memoizedState: null,
372
+ refCleanup: null
371
373
  };
372
374
  }, "createFiber");
373
375
  var createHostRootFiber = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((containerDom) => {
@@ -394,6 +396,7 @@ var createWorkInProgress = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((current, pe
394
396
  }
395
397
  wip.memoizedProps = current.memoizedProps;
396
398
  wip.memoizedState = current.memoizedState;
399
+ wip.refCleanup = current.refCleanup;
397
400
  wip.return = current.return;
398
401
  wip.child = current.child;
399
402
  wip.sibling = current.sibling;
@@ -482,23 +485,14 @@ function useState(initialState) {
482
485
  }
483
486
  const hookIndex = getHookIndex();
484
487
  nextHookIndex();
485
- let state = fiber.memoizedState;
486
- let prevHook = null;
487
- for (let i = 0; i < hookIndex; i++) {
488
- prevHook = state;
489
- state = state?.next ?? null;
490
- }
488
+ let { hook: state, prevHook } = getHookSlot(fiber, hookIndex);
491
489
  if (!state) {
492
490
  state = {
493
491
  memoizedState: getInitialState(initialState),
494
492
  queue: null,
495
493
  next: null
496
494
  };
497
- if (prevHook) {
498
- prevHook.next = state;
499
- } else {
500
- fiber.memoizedState = state;
501
- }
495
+ attachHook(fiber, state, prevHook);
502
496
  }
503
497
  let update = state.queue;
504
498
  while (update) {
@@ -523,6 +517,42 @@ function useState(initialState) {
523
517
  }
524
518
  chunkAJJIEZ7G_cjs.__name(useState, "useState");
525
519
  var RectifyHookUseState_default = useState;
520
+ function useReducer(reducer, initialArg, init) {
521
+ const fiber = getFiberRendering();
522
+ if (!fiber) {
523
+ throw new Error("useReducer must be used within a function component.");
524
+ }
525
+ const hookIndex = getHookIndex();
526
+ nextHookIndex();
527
+ let { hook, prevHook } = getHookSlot(fiber, hookIndex);
528
+ if (!hook) {
529
+ const initialState = shared.isFunction(init) ? init(initialArg) : initialArg;
530
+ hook = { memoizedState: initialState, queue: null, next: null };
531
+ attachHook(fiber, hook, prevHook);
532
+ }
533
+ let update = hook.queue;
534
+ while (update) {
535
+ hook.memoizedState = reducer(hook.memoizedState, update.action);
536
+ update = update.next;
537
+ }
538
+ hook.queue = null;
539
+ const dispatch = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((action) => {
540
+ const update2 = { action, next: null };
541
+ if (!hook.queue) {
542
+ hook.queue = update2;
543
+ } else {
544
+ let last = hook.queue;
545
+ while (last.next) {
546
+ last = last.next;
547
+ }
548
+ last.next = update2;
549
+ }
550
+ scheduleRerender(fiber);
551
+ }, "dispatch");
552
+ return [hook.memoizedState, dispatch];
553
+ }
554
+ chunkAJJIEZ7G_cjs.__name(useReducer, "useReducer");
555
+ var RectifyHookUseReducer_default = useReducer;
526
556
 
527
557
  // ../rectify-hook/src/RectifyHookDeps.ts
528
558
  var depsChanged = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((prev, next) => {
@@ -633,20 +663,11 @@ function useRef(initialValue) {
633
663
  }
634
664
  const hookIndex = getHookIndex();
635
665
  nextHookIndex();
636
- let hook = fiber.memoizedState;
637
- let prevHook = null;
638
- for (let i = 0; i < hookIndex; i++) {
639
- prevHook = hook;
640
- hook = hook?.next ?? null;
641
- }
666
+ const { hook, prevHook } = getHookSlot(fiber, hookIndex);
642
667
  if (!hook) {
643
668
  const ref = { current: initialValue };
644
669
  const newHook = { memoizedState: ref, queue: null, next: null };
645
- if (prevHook) {
646
- prevHook.next = newHook;
647
- } else {
648
- fiber.memoizedState = newHook;
649
- }
670
+ attachHook(fiber, newHook, prevHook);
650
671
  return ref;
651
672
  }
652
673
  return hook.memoizedState;
@@ -662,20 +683,10 @@ function useMemo(factory, deps) {
662
683
  }
663
684
  const hookIndex = getHookIndex();
664
685
  nextHookIndex();
665
- let hook = fiber.memoizedState;
666
- let prevHook = null;
667
- for (let i = 0; i < hookIndex; i++) {
668
- prevHook = hook;
669
- hook = hook?.next ?? null;
670
- }
686
+ const { hook, prevHook } = getHookSlot(fiber, hookIndex);
671
687
  if (!hook) {
672
688
  const state = { value: factory(), deps };
673
- const newHook = { memoizedState: state, queue: null, next: null };
674
- if (prevHook) {
675
- prevHook.next = newHook;
676
- } else {
677
- fiber.memoizedState = newHook;
678
- }
689
+ attachHook(fiber, { memoizedState: state, queue: null, next: null }, prevHook);
679
690
  return state.value;
680
691
  }
681
692
  const prev = hook.memoizedState;
@@ -1044,6 +1055,9 @@ var reuseOrCreate = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((oldFiber, element,
1044
1055
  if (hasPropsChanged(oldFiber.memoizedProps, element.props)) {
1045
1056
  addFlagToFiber(newFiber, UpdateFlag);
1046
1057
  }
1058
+ if ((oldFiber.memoizedProps?.ref ?? null) !== (element.props?.ref ?? null)) {
1059
+ addFlagToFiber(newFiber, RefFlag);
1060
+ }
1047
1061
  newFiber.return = wip;
1048
1062
  return newFiber;
1049
1063
  }, "reuseOrCreate");
@@ -1141,7 +1155,7 @@ var reconcileChildren = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((wip, children)
1141
1155
  }, "reconcileChildren");
1142
1156
 
1143
1157
  // ../rectify-reconciler/src/RectifyFiberCommitWork.ts
1144
- var MutationMask = PlacementFlag | UpdateFlag | MoveFlag;
1158
+ var MutationMask = PlacementFlag | UpdateFlag | MoveFlag | RefFlag;
1145
1159
  var commitWork = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((finishedWork) => {
1146
1160
  if (finishedWork.deletions?.length) {
1147
1161
  finishedWork.deletions.forEach(removeHostTree);
@@ -1166,13 +1180,25 @@ var syncMemoizedProps = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((wip) => {
1166
1180
  }, "syncMemoizedProps");
1167
1181
  var attachRef = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((wip) => {
1168
1182
  const ref = wip.pendingProps?.ref;
1169
- if (ref && typeof ref === "object" && "current" in ref) {
1183
+ if (!ref) return;
1184
+ if (typeof ref === "function") {
1185
+ const cleanup = ref(wip.stateNode);
1186
+ wip.refCleanup = typeof cleanup === "function" ? cleanup : null;
1187
+ } else if (typeof ref === "object" && "current" in ref) {
1170
1188
  ref.current = wip.stateNode;
1171
1189
  }
1172
1190
  }, "attachRef");
1173
1191
  var detachRef = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((fiber) => {
1192
+ if (fiber.refCleanup) {
1193
+ fiber.refCleanup();
1194
+ fiber.refCleanup = null;
1195
+ return;
1196
+ }
1174
1197
  const ref = fiber.memoizedProps?.ref;
1175
- if (ref && typeof ref === "object" && "current" in ref) {
1198
+ if (!ref) return;
1199
+ if (typeof ref === "function") {
1200
+ ref(null);
1201
+ } else if (typeof ref === "object" && "current" in ref) {
1176
1202
  ref.current = null;
1177
1203
  }
1178
1204
  }, "detachRef");
@@ -1216,9 +1242,13 @@ var commitMutationHostComponent = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((wip)
1216
1242
  if (hasFlagOnFiber(wip, UpdateFlag)) {
1217
1243
  applyPropsToDom(wip.stateNode, wip.memoizedProps, wip.pendingProps);
1218
1244
  precacheFiberNode(wip, wip.stateNode);
1219
- attachRef(wip);
1220
1245
  removeFlagFromFiber(wip, UpdateFlag);
1221
1246
  }
1247
+ if (hasFlagOnFiber(wip, RefFlag)) {
1248
+ detachRef(wip);
1249
+ attachRef(wip);
1250
+ removeFlagFromFiber(wip, RefFlag);
1251
+ }
1222
1252
  }, "commitMutationHostComponent");
1223
1253
  var commitMutationHostText = /* @__PURE__ */ chunkAJJIEZ7G_cjs.__name((wip) => {
1224
1254
  if (!wip.stateNode) {
@@ -1426,10 +1456,6 @@ Object.defineProperty(exports, "jsx", {
1426
1456
  enumerable: true,
1427
1457
  get: function () { return chunkAJJIEZ7G_cjs.jsx; }
1428
1458
  });
1429
- Object.defineProperty(exports, "FC", {
1430
- enumerable: true,
1431
- get: function () { return shared.FC; }
1432
- });
1433
1459
  exports.SyntheticEvent = SyntheticEvent_default;
1434
1460
  exports.createContext = createContext;
1435
1461
  exports.createRoot = createRoot;
@@ -1439,6 +1465,7 @@ exports.useContext = useContext;
1439
1465
  exports.useEffect = RectifyHookUseEffect_default;
1440
1466
  exports.useLayoutEffect = RectifyHookUseLayoutEffect_default;
1441
1467
  exports.useMemo = RectifyHookUseMemo_default;
1468
+ exports.useReducer = RectifyHookUseReducer_default;
1442
1469
  exports.useRef = RectifyHookUseRef_default;
1443
1470
  exports.useState = RectifyHookUseState_default;
1444
1471
  //# sourceMappingURL=index.cjs.map