@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.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { RectifyNode, FC, Fiber } from '@rectify-dev/shared';
2
- export { AnchorHTMLAttributes, AriaAttributes, AudioHTMLAttributes, ButtonHTMLAttributes, CSSProperties, FC, FormHTMLAttributes, HTMLAttributes, IframeHTMLAttributes, ImgHTMLAttributes, InputHTMLAttributes, LabelHTMLAttributes, MetaHTMLAttributes, OptGroupHTMLAttributes, OptionHTMLAttributes, RectifyEventHandlers, RectifyIntrinsicElements, ScriptHTMLAttributes, SelectHTMLAttributes, SyntheticAnimationEvent, SyntheticChangeEvent, SyntheticClipboardEvent, SyntheticDragEvent, SyntheticEvent as SyntheticEventType, SyntheticFocusEvent, SyntheticInputEvent, SyntheticKeyboardEvent, SyntheticMouseEvent, SyntheticPointerEvent, SyntheticSubmitEvent, SyntheticTouchEvent, SyntheticTransitionEvent, SyntheticWheelEvent, TextareaHTMLAttributes, VideoHTMLAttributes } from '@rectify-dev/shared';
2
+ export { AnchorHTMLAttributes, AriaAttributes, AudioHTMLAttributes, ButtonHTMLAttributes, CSSProperties, FC, FormHTMLAttributes, HTMLAttributes, IframeHTMLAttributes, ImgHTMLAttributes, InputHTMLAttributes, LabelHTMLAttributes, MetaHTMLAttributes, OptGroupHTMLAttributes, OptionHTMLAttributes, RectifyEventHandlers, RectifyIntrinsicElements, RectifyNode, ScriptHTMLAttributes, SelectHTMLAttributes, SyntheticAnimationEvent, SyntheticChangeEvent, SyntheticClipboardEvent, SyntheticDragEvent, SyntheticEvent as SyntheticEventType, SyntheticFocusEvent, SyntheticInputEvent, SyntheticKeyboardEvent, SyntheticMouseEvent, SyntheticPointerEvent, SyntheticSubmitEvent, SyntheticTouchEvent, SyntheticTransitionEvent, SyntheticWheelEvent, TextareaHTMLAttributes, VideoHTMLAttributes } from '@rectify-dev/shared';
3
3
  export { Fragment, jsx } from './runtime.cjs';
4
4
 
5
5
  type RectifyDomRoot = {
@@ -51,6 +51,11 @@ declare function useState<S>(): [S | undefined, StateDispatcher<S | undefined>];
51
51
  declare function useState<S>(initialState: S): [S, StateDispatcher<S>];
52
52
  declare function useState<S>(initialState: () => S): [S, StateDispatcher<S>];
53
53
 
54
+ type Reducer<S, A> = (state: S, action: A) => S;
55
+ type Dispatch<A> = (action: A) => void;
56
+ declare function useReducer<S, A>(reducer: Reducer<S, A>, initialState: S): [S, Dispatch<A>];
57
+ declare function useReducer<S, A, I>(reducer: Reducer<S, A>, initialArg: I, init: (arg: I) => S): [S, Dispatch<A>];
58
+
54
59
  declare function useEffect(create: () => void | (() => void), deps?: any[]): void;
55
60
 
56
61
  /**
@@ -157,4 +162,4 @@ declare function createContext<T>(defaultValue: T): RectifyContext<T>;
157
162
  */
158
163
  declare function useContext<T>(context: RectifyContext<T>): T;
159
164
 
160
- export { type MemoComponent, type RectifyContext, type RefObject, SyntheticEvent, createContext, createRoot, memo, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState };
165
+ export { type Dispatch, type MemoComponent, type RectifyContext, type Reducer, type RefObject, SyntheticEvent, createContext, createRoot, memo, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useReducer, useRef, useState };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { RectifyNode, FC, Fiber } from '@rectify-dev/shared';
2
- export { AnchorHTMLAttributes, AriaAttributes, AudioHTMLAttributes, ButtonHTMLAttributes, CSSProperties, FC, FormHTMLAttributes, HTMLAttributes, IframeHTMLAttributes, ImgHTMLAttributes, InputHTMLAttributes, LabelHTMLAttributes, MetaHTMLAttributes, OptGroupHTMLAttributes, OptionHTMLAttributes, RectifyEventHandlers, RectifyIntrinsicElements, ScriptHTMLAttributes, SelectHTMLAttributes, SyntheticAnimationEvent, SyntheticChangeEvent, SyntheticClipboardEvent, SyntheticDragEvent, SyntheticEvent as SyntheticEventType, SyntheticFocusEvent, SyntheticInputEvent, SyntheticKeyboardEvent, SyntheticMouseEvent, SyntheticPointerEvent, SyntheticSubmitEvent, SyntheticTouchEvent, SyntheticTransitionEvent, SyntheticWheelEvent, TextareaHTMLAttributes, VideoHTMLAttributes } from '@rectify-dev/shared';
2
+ export { AnchorHTMLAttributes, AriaAttributes, AudioHTMLAttributes, ButtonHTMLAttributes, CSSProperties, FC, FormHTMLAttributes, HTMLAttributes, IframeHTMLAttributes, ImgHTMLAttributes, InputHTMLAttributes, LabelHTMLAttributes, MetaHTMLAttributes, OptGroupHTMLAttributes, OptionHTMLAttributes, RectifyEventHandlers, RectifyIntrinsicElements, RectifyNode, ScriptHTMLAttributes, SelectHTMLAttributes, SyntheticAnimationEvent, SyntheticChangeEvent, SyntheticClipboardEvent, SyntheticDragEvent, SyntheticEvent as SyntheticEventType, SyntheticFocusEvent, SyntheticInputEvent, SyntheticKeyboardEvent, SyntheticMouseEvent, SyntheticPointerEvent, SyntheticSubmitEvent, SyntheticTouchEvent, SyntheticTransitionEvent, SyntheticWheelEvent, TextareaHTMLAttributes, VideoHTMLAttributes } from '@rectify-dev/shared';
3
3
  export { Fragment, jsx } from './runtime.js';
4
4
 
5
5
  type RectifyDomRoot = {
@@ -51,6 +51,11 @@ declare function useState<S>(): [S | undefined, StateDispatcher<S | undefined>];
51
51
  declare function useState<S>(initialState: S): [S, StateDispatcher<S>];
52
52
  declare function useState<S>(initialState: () => S): [S, StateDispatcher<S>];
53
53
 
54
+ type Reducer<S, A> = (state: S, action: A) => S;
55
+ type Dispatch<A> = (action: A) => void;
56
+ declare function useReducer<S, A>(reducer: Reducer<S, A>, initialState: S): [S, Dispatch<A>];
57
+ declare function useReducer<S, A, I>(reducer: Reducer<S, A>, initialArg: I, init: (arg: I) => S): [S, Dispatch<A>];
58
+
54
59
  declare function useEffect(create: () => void | (() => void), deps?: any[]): void;
55
60
 
56
61
  /**
@@ -157,4 +162,4 @@ declare function createContext<T>(defaultValue: T): RectifyContext<T>;
157
162
  */
158
163
  declare function useContext<T>(context: RectifyContext<T>): T;
159
164
 
160
- export { type MemoComponent, type RectifyContext, type RefObject, SyntheticEvent, createContext, createRoot, memo, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState };
165
+ export { type Dispatch, type MemoComponent, type RectifyContext, type Reducer, type RefObject, SyntheticEvent, createContext, createRoot, memo, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useReducer, useRef, useState };
package/dist/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { __name } from './chunk-2FA4QXYL.js';
2
2
  export { Fragment, jsx } from './chunk-2FA4QXYL.js';
3
3
  import { isFunction, shallowEqual, createElementFromRectifyNode, isValidRectifyElement, isArray, isPlainObject, omit, RECTIFY_FRAGMENT_TYPE, RECTIFY_TEXT_TYPE, RECTIFY_ELEMENT_TYPE } from '@rectify-dev/shared';
4
- export { FC } from '@rectify-dev/shared';
5
4
 
6
5
  // ../rectify-dom-binding/src/events/RectifyEventRegistry.ts
7
6
  var allNativeEvents = /* @__PURE__ */ new Set();
@@ -247,6 +246,7 @@ var PlacementFlag = 1 << 0;
247
246
  var UpdateFlag = 1 << 1;
248
247
  var DeletionFlag = 1 << 2;
249
248
  var MoveFlag = 1 << 3;
249
+ var RefFlag = 1 << 4;
250
250
 
251
251
  // ../rectify-reconciler/src/RectifyFiberLanes.ts
252
252
  var NoLanes = 0;
@@ -367,7 +367,8 @@ var createFiber = /* @__PURE__ */ __name((workTag, pendingProps, key = null) =>
367
367
  childLanes: NoLanes,
368
368
  subtreeFlags: NoFlags,
369
369
  flags: NoFlags,
370
- memoizedState: null
370
+ memoizedState: null,
371
+ refCleanup: null
371
372
  };
372
373
  }, "createFiber");
373
374
  var createHostRootFiber = /* @__PURE__ */ __name((containerDom) => {
@@ -394,6 +395,7 @@ var createWorkInProgress = /* @__PURE__ */ __name((current, pendingProps) => {
394
395
  }
395
396
  wip.memoizedProps = current.memoizedProps;
396
397
  wip.memoizedState = current.memoizedState;
398
+ wip.refCleanup = current.refCleanup;
397
399
  wip.return = current.return;
398
400
  wip.child = current.child;
399
401
  wip.sibling = current.sibling;
@@ -482,23 +484,14 @@ function useState(initialState) {
482
484
  }
483
485
  const hookIndex = getHookIndex();
484
486
  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
- }
487
+ let { hook: state, prevHook } = getHookSlot(fiber, hookIndex);
491
488
  if (!state) {
492
489
  state = {
493
490
  memoizedState: getInitialState(initialState),
494
491
  queue: null,
495
492
  next: null
496
493
  };
497
- if (prevHook) {
498
- prevHook.next = state;
499
- } else {
500
- fiber.memoizedState = state;
501
- }
494
+ attachHook(fiber, state, prevHook);
502
495
  }
503
496
  let update = state.queue;
504
497
  while (update) {
@@ -523,6 +516,42 @@ function useState(initialState) {
523
516
  }
524
517
  __name(useState, "useState");
525
518
  var RectifyHookUseState_default = useState;
519
+ function useReducer(reducer, initialArg, init) {
520
+ const fiber = getFiberRendering();
521
+ if (!fiber) {
522
+ throw new Error("useReducer must be used within a function component.");
523
+ }
524
+ const hookIndex = getHookIndex();
525
+ nextHookIndex();
526
+ let { hook, prevHook } = getHookSlot(fiber, hookIndex);
527
+ if (!hook) {
528
+ const initialState = isFunction(init) ? init(initialArg) : initialArg;
529
+ hook = { memoizedState: initialState, queue: null, next: null };
530
+ attachHook(fiber, hook, prevHook);
531
+ }
532
+ let update = hook.queue;
533
+ while (update) {
534
+ hook.memoizedState = reducer(hook.memoizedState, update.action);
535
+ update = update.next;
536
+ }
537
+ hook.queue = null;
538
+ const dispatch = /* @__PURE__ */ __name((action) => {
539
+ const update2 = { action, next: null };
540
+ if (!hook.queue) {
541
+ hook.queue = update2;
542
+ } else {
543
+ let last = hook.queue;
544
+ while (last.next) {
545
+ last = last.next;
546
+ }
547
+ last.next = update2;
548
+ }
549
+ scheduleRerender(fiber);
550
+ }, "dispatch");
551
+ return [hook.memoizedState, dispatch];
552
+ }
553
+ __name(useReducer, "useReducer");
554
+ var RectifyHookUseReducer_default = useReducer;
526
555
 
527
556
  // ../rectify-hook/src/RectifyHookDeps.ts
528
557
  var depsChanged = /* @__PURE__ */ __name((prev, next) => {
@@ -633,20 +662,11 @@ function useRef(initialValue) {
633
662
  }
634
663
  const hookIndex = getHookIndex();
635
664
  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
- }
665
+ const { hook, prevHook } = getHookSlot(fiber, hookIndex);
642
666
  if (!hook) {
643
667
  const ref = { current: initialValue };
644
668
  const newHook = { memoizedState: ref, queue: null, next: null };
645
- if (prevHook) {
646
- prevHook.next = newHook;
647
- } else {
648
- fiber.memoizedState = newHook;
649
- }
669
+ attachHook(fiber, newHook, prevHook);
650
670
  return ref;
651
671
  }
652
672
  return hook.memoizedState;
@@ -662,20 +682,10 @@ function useMemo(factory, deps) {
662
682
  }
663
683
  const hookIndex = getHookIndex();
664
684
  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
- }
685
+ const { hook, prevHook } = getHookSlot(fiber, hookIndex);
671
686
  if (!hook) {
672
687
  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
- }
688
+ attachHook(fiber, { memoizedState: state, queue: null, next: null }, prevHook);
679
689
  return state.value;
680
690
  }
681
691
  const prev = hook.memoizedState;
@@ -1044,6 +1054,9 @@ var reuseOrCreate = /* @__PURE__ */ __name((oldFiber, element, wip) => {
1044
1054
  if (hasPropsChanged(oldFiber.memoizedProps, element.props)) {
1045
1055
  addFlagToFiber(newFiber, UpdateFlag);
1046
1056
  }
1057
+ if ((oldFiber.memoizedProps?.ref ?? null) !== (element.props?.ref ?? null)) {
1058
+ addFlagToFiber(newFiber, RefFlag);
1059
+ }
1047
1060
  newFiber.return = wip;
1048
1061
  return newFiber;
1049
1062
  }, "reuseOrCreate");
@@ -1141,7 +1154,7 @@ var reconcileChildren = /* @__PURE__ */ __name((wip, children) => {
1141
1154
  }, "reconcileChildren");
1142
1155
 
1143
1156
  // ../rectify-reconciler/src/RectifyFiberCommitWork.ts
1144
- var MutationMask = PlacementFlag | UpdateFlag | MoveFlag;
1157
+ var MutationMask = PlacementFlag | UpdateFlag | MoveFlag | RefFlag;
1145
1158
  var commitWork = /* @__PURE__ */ __name((finishedWork) => {
1146
1159
  if (finishedWork.deletions?.length) {
1147
1160
  finishedWork.deletions.forEach(removeHostTree);
@@ -1166,13 +1179,25 @@ var syncMemoizedProps = /* @__PURE__ */ __name((wip) => {
1166
1179
  }, "syncMemoizedProps");
1167
1180
  var attachRef = /* @__PURE__ */ __name((wip) => {
1168
1181
  const ref = wip.pendingProps?.ref;
1169
- if (ref && typeof ref === "object" && "current" in ref) {
1182
+ if (!ref) return;
1183
+ if (typeof ref === "function") {
1184
+ const cleanup = ref(wip.stateNode);
1185
+ wip.refCleanup = typeof cleanup === "function" ? cleanup : null;
1186
+ } else if (typeof ref === "object" && "current" in ref) {
1170
1187
  ref.current = wip.stateNode;
1171
1188
  }
1172
1189
  }, "attachRef");
1173
1190
  var detachRef = /* @__PURE__ */ __name((fiber) => {
1191
+ if (fiber.refCleanup) {
1192
+ fiber.refCleanup();
1193
+ fiber.refCleanup = null;
1194
+ return;
1195
+ }
1174
1196
  const ref = fiber.memoizedProps?.ref;
1175
- if (ref && typeof ref === "object" && "current" in ref) {
1197
+ if (!ref) return;
1198
+ if (typeof ref === "function") {
1199
+ ref(null);
1200
+ } else if (typeof ref === "object" && "current" in ref) {
1176
1201
  ref.current = null;
1177
1202
  }
1178
1203
  }, "detachRef");
@@ -1216,9 +1241,13 @@ var commitMutationHostComponent = /* @__PURE__ */ __name((wip) => {
1216
1241
  if (hasFlagOnFiber(wip, UpdateFlag)) {
1217
1242
  applyPropsToDom(wip.stateNode, wip.memoizedProps, wip.pendingProps);
1218
1243
  precacheFiberNode(wip, wip.stateNode);
1219
- attachRef(wip);
1220
1244
  removeFlagFromFiber(wip, UpdateFlag);
1221
1245
  }
1246
+ if (hasFlagOnFiber(wip, RefFlag)) {
1247
+ detachRef(wip);
1248
+ attachRef(wip);
1249
+ removeFlagFromFiber(wip, RefFlag);
1250
+ }
1222
1251
  }, "commitMutationHostComponent");
1223
1252
  var commitMutationHostText = /* @__PURE__ */ __name((wip) => {
1224
1253
  if (!wip.stateNode) {
@@ -1418,6 +1447,6 @@ function memo(Component, compare) {
1418
1447
  }
1419
1448
  __name(memo, "memo");
1420
1449
 
1421
- export { SyntheticEvent_default as SyntheticEvent, createContext, createRoot, memo, RectifyHookUseCallback_default as useCallback, useContext, RectifyHookUseEffect_default as useEffect, RectifyHookUseLayoutEffect_default as useLayoutEffect, RectifyHookUseMemo_default as useMemo, RectifyHookUseRef_default as useRef, RectifyHookUseState_default as useState };
1450
+ export { SyntheticEvent_default as SyntheticEvent, createContext, createRoot, memo, RectifyHookUseCallback_default as useCallback, useContext, RectifyHookUseEffect_default as useEffect, RectifyHookUseLayoutEffect_default as useLayoutEffect, RectifyHookUseMemo_default as useMemo, RectifyHookUseReducer_default as useReducer, RectifyHookUseRef_default as useRef, RectifyHookUseState_default as useState };
1422
1451
  //# sourceMappingURL=index.js.map
1423
1452
  //# sourceMappingURL=index.js.map