@reckona/mreact-compat 0.0.86 → 0.0.88

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.
@@ -0,0 +1,68 @@
1
+ import { Component, PureComponent, } from "./class-component.js";
2
+ import { Fragment, Activity, Profiler, Suspense, SuspenseList, StrictMode, Children, cloneElement, createElement, createErrorBoundary, createPortal, createRef, forwardRef, isValidElement, lazy, memo, } from "./element.js";
3
+ import { createContext, renderContextConsumerToString, renderContextProviderToString, useContext, } from "./context.js";
4
+ import { applyStreamingHydrationFragments, createRoot, createStreamingHydrationRoot, enableEventHydrationManifestReplay, enableHydrationEventReplay, flushSync, hydrateRoot, queueHydrationEvent, readEventHydrationManifest, render, unmountComponentAtNode, } from "./render.js";
5
+ import { useCallback, useDebugValue, useDeferredValue, useEffectEvent, useEffect, useId, useImperativeHandle, useInsertionEffect, useLayoutEffect, useMemo, useOptimistic, useReducer, useRef, useState, useSyncExternalStore, use, useActionState, act, cache, cacheSignal, captureOwnerStack, renderToString, startTransition, unstable_useCacheRefresh, useTransition, version, } from "./hooks.js";
6
+ const ReactCompat = {
7
+ Component,
8
+ PureComponent,
9
+ Fragment,
10
+ Activity,
11
+ Profiler,
12
+ Suspense,
13
+ SuspenseList,
14
+ StrictMode,
15
+ Children,
16
+ cloneElement,
17
+ createElement,
18
+ createErrorBoundary,
19
+ createPortal,
20
+ createRef,
21
+ forwardRef,
22
+ isValidElement,
23
+ lazy,
24
+ memo,
25
+ createContext,
26
+ renderContextConsumerToString,
27
+ renderContextProviderToString,
28
+ useContext,
29
+ applyStreamingHydrationFragments,
30
+ createRoot,
31
+ createStreamingHydrationRoot,
32
+ enableEventHydrationManifestReplay,
33
+ enableHydrationEventReplay,
34
+ flushSync,
35
+ hydrateRoot,
36
+ queueHydrationEvent,
37
+ readEventHydrationManifest,
38
+ render,
39
+ unmountComponentAtNode,
40
+ useCallback,
41
+ useDebugValue,
42
+ useDeferredValue,
43
+ useEffectEvent,
44
+ useEffect,
45
+ useId,
46
+ useImperativeHandle,
47
+ useInsertionEffect,
48
+ useLayoutEffect,
49
+ useMemo,
50
+ useOptimistic,
51
+ useReducer,
52
+ useRef,
53
+ useState,
54
+ useSyncExternalStore,
55
+ use,
56
+ useActionState,
57
+ act,
58
+ cache,
59
+ cacheSignal,
60
+ captureOwnerStack,
61
+ renderToString,
62
+ startTransition,
63
+ unstable_useCacheRefresh,
64
+ useTransition,
65
+ version,
66
+ };
67
+ export default ReactCompat;
68
+ //# sourceMappingURL=react-default.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react-default.js","sourceRoot":"","sources":["../src/react-default.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,aAAa,GACd,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,UAAU,EACV,QAAQ,EACR,YAAY,EACZ,aAAa,EACb,mBAAmB,EACnB,YAAY,EACZ,SAAS,EACT,UAAU,EACV,cAAc,EACd,IAAI,EACJ,IAAI,GACL,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,aAAa,EACb,6BAA6B,EAC7B,6BAA6B,EAC7B,UAAU,GACX,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,gCAAgC,EAChC,UAAU,EACV,4BAA4B,EAC5B,kCAAkC,EAClC,0BAA0B,EAC1B,SAAS,EACT,WAAW,EACX,mBAAmB,EACnB,0BAA0B,EAC1B,MAAM,EACN,sBAAsB,GACvB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,cAAc,EACd,SAAS,EACT,KAAK,EACL,mBAAmB,EACnB,kBAAkB,EAClB,eAAe,EACf,OAAO,EACP,aAAa,EACb,UAAU,EACV,MAAM,EACN,QAAQ,EACR,oBAAoB,EACpB,GAAG,EACH,cAAc,EACd,GAAG,EACH,KAAK,EACL,WAAW,EACX,iBAAiB,EACjB,cAAc,EACd,eAAe,EACf,wBAAwB,EACxB,aAAa,EACb,OAAO,GACR,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,GAAG;IAClB,SAAS;IACT,aAAa;IACb,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,YAAY;IACZ,UAAU;IACV,QAAQ;IACR,YAAY;IACZ,aAAa;IACb,mBAAmB;IACnB,YAAY;IACZ,SAAS;IACT,UAAU;IACV,cAAc;IACd,IAAI;IACJ,IAAI;IACJ,aAAa;IACb,6BAA6B;IAC7B,6BAA6B;IAC7B,UAAU;IACV,gCAAgC;IAChC,UAAU;IACV,4BAA4B;IAC5B,kCAAkC;IAClC,0BAA0B;IAC1B,SAAS;IACT,WAAW;IACX,mBAAmB;IACnB,0BAA0B;IAC1B,MAAM;IACN,sBAAsB;IACtB,WAAW;IACX,aAAa;IACb,gBAAgB;IAChB,cAAc;IACd,SAAS;IACT,KAAK;IACL,mBAAmB;IACnB,kBAAkB;IAClB,eAAe;IACf,OAAO;IACP,aAAa;IACb,UAAU;IACV,MAAM;IACN,QAAQ;IACR,oBAAoB;IACpB,GAAG;IACH,cAAc;IACd,GAAG;IACH,KAAK;IACL,WAAW;IACX,iBAAiB;IACjB,cAAc;IACd,eAAe;IACf,wBAAwB;IACxB,aAAa;IACb,OAAO;CACC,CAAC;AAEX,eAAe,WAAW,CAAC","sourcesContent":["import {\n Component,\n PureComponent,\n} from \"./class-component.js\";\nimport {\n Fragment,\n Activity,\n Profiler,\n Suspense,\n SuspenseList,\n StrictMode,\n Children,\n cloneElement,\n createElement,\n createErrorBoundary,\n createPortal,\n createRef,\n forwardRef,\n isValidElement,\n lazy,\n memo,\n} from \"./element.js\";\nimport {\n createContext,\n renderContextConsumerToString,\n renderContextProviderToString,\n useContext,\n} from \"./context.js\";\nimport {\n applyStreamingHydrationFragments,\n createRoot,\n createStreamingHydrationRoot,\n enableEventHydrationManifestReplay,\n enableHydrationEventReplay,\n flushSync,\n hydrateRoot,\n queueHydrationEvent,\n readEventHydrationManifest,\n render,\n unmountComponentAtNode,\n} from \"./render.js\";\nimport {\n useCallback,\n useDebugValue,\n useDeferredValue,\n useEffectEvent,\n useEffect,\n useId,\n useImperativeHandle,\n useInsertionEffect,\n useLayoutEffect,\n useMemo,\n useOptimistic,\n useReducer,\n useRef,\n useState,\n useSyncExternalStore,\n use,\n useActionState,\n act,\n cache,\n cacheSignal,\n captureOwnerStack,\n renderToString,\n startTransition,\n unstable_useCacheRefresh,\n useTransition,\n version,\n} from \"./hooks.js\";\n\nconst ReactCompat = {\n Component,\n PureComponent,\n Fragment,\n Activity,\n Profiler,\n Suspense,\n SuspenseList,\n StrictMode,\n Children,\n cloneElement,\n createElement,\n createErrorBoundary,\n createPortal,\n createRef,\n forwardRef,\n isValidElement,\n lazy,\n memo,\n createContext,\n renderContextConsumerToString,\n renderContextProviderToString,\n useContext,\n applyStreamingHydrationFragments,\n createRoot,\n createStreamingHydrationRoot,\n enableEventHydrationManifestReplay,\n enableHydrationEventReplay,\n flushSync,\n hydrateRoot,\n queueHydrationEvent,\n readEventHydrationManifest,\n render,\n unmountComponentAtNode,\n useCallback,\n useDebugValue,\n useDeferredValue,\n useEffectEvent,\n useEffect,\n useId,\n useImperativeHandle,\n useInsertionEffect,\n useLayoutEffect,\n useMemo,\n useOptimistic,\n useReducer,\n useRef,\n useState,\n useSyncExternalStore,\n use,\n useActionState,\n act,\n cache,\n cacheSignal,\n captureOwnerStack,\n renderToString,\n startTransition,\n unstable_useCacheRefresh,\n useTransition,\n version,\n} as const;\n\nexport default ReactCompat;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reckona/mreact-compat",
3
- "version": "0.0.86",
3
+ "version": "0.0.88",
4
4
  "description": "React-compatible runtime implementation for mreact.",
5
5
  "keywords": [
6
6
  "compatibility",
@@ -65,6 +65,6 @@
65
65
  "access": "public"
66
66
  },
67
67
  "dependencies": {
68
- "@reckona/mreact-shared": "0.0.86"
68
+ "@reckona/mreact-shared": "0.0.88"
69
69
  }
70
70
  }
@@ -39,6 +39,10 @@ export interface ClassComponentInstance {
39
39
 
40
40
  export interface ClassComponentType {
41
41
  new (props: Record<string, unknown>): ClassComponentInstance;
42
+ getDerivedStateFromProps?: (
43
+ nextProps: Record<string, unknown>,
44
+ previousState: Record<string, unknown>,
45
+ ) => Record<string, unknown> | null;
42
46
  getDerivedStateFromError?: (error: Error) => Record<string, unknown> | null;
43
47
  }
44
48
 
@@ -170,10 +174,11 @@ export function renderClassComponentWithRuntime(
170
174
  const previousProps = instance.props;
171
175
  const snapshot = classLifecycleSnapshots.get(instance);
172
176
  const previousState = snapshot?.previousState ?? instance.state ?? {};
173
- const nextState = instance.state ?? {};
174
177
 
175
178
  instanceRef.current = instance;
176
179
  installClassUpdateMethods(instance, runtime);
180
+ applyDerivedStateFromProps(type, instance, props, previousState);
181
+ const nextState = instance.state ?? {};
177
182
  const shouldSkipUpdate =
178
183
  didCommitRef.current &&
179
184
  snapshot?.force !== true &&
@@ -219,6 +224,24 @@ export function renderClassComponentWithRuntime(
219
224
  });
220
225
  }
221
226
 
227
+ export function applyDerivedStateFromProps(
228
+ type: ClassComponentType,
229
+ instance: ClassComponentInstance,
230
+ nextProps: Record<string, unknown>,
231
+ previousState: Record<string, unknown>,
232
+ ): void {
233
+ const derivedState = type.getDerivedStateFromProps?.(nextProps, previousState);
234
+
235
+ if (derivedState === undefined || derivedState === null) {
236
+ return;
237
+ }
238
+
239
+ instance.state = {
240
+ ...instance.state,
241
+ ...derivedState,
242
+ };
243
+ }
244
+
222
245
  export function recoverClassComponentError(
223
246
  type: ClassComponentType,
224
247
  instance: ClassComponentInstance,
package/src/element.ts CHANGED
@@ -263,6 +263,14 @@ export const Children = {
263
263
  fn(child as Exclude<ReactCompatNode, null | undefined | boolean>, index),
264
264
  );
265
265
  },
266
+ forEach(
267
+ children: ReactCompatNode,
268
+ fn: (child: Exclude<ReactCompatNode, null | undefined | boolean>, index: number) => void,
269
+ ): void {
270
+ toChildArray(children).forEach((child, index) => {
271
+ fn(child, index);
272
+ });
273
+ },
266
274
  count(children: ReactCompatNode): number {
267
275
  if (children === null || children === undefined) {
268
276
  return 0;
@@ -27,6 +27,7 @@ import { DidCapture } from "./fiber-flags.js";
27
27
  import { mergeLanes, NoLanes } from "./fiber-lanes.js";
28
28
  import { isThenable } from "./thenable.js";
29
29
  import {
30
+ applyDerivedStateFromProps,
30
31
  isClassComponentType,
31
32
  type ClassComponentInstance,
32
33
  type ClassComponentType,
@@ -518,6 +519,8 @@ function beginClassComponent(
518
519
  ? unit.alternate.stateNode
519
520
  : undefined;
520
521
  const instance = currentInstance ?? new type(nextProps);
522
+ const previousState = instance.state ?? {};
523
+ applyDerivedStateFromProps(type, instance, nextProps, previousState);
521
524
  const nextState = instance.state ?? {};
522
525
 
523
526
  unit.stateNode = instance;
package/src/hooks.ts CHANGED
@@ -601,15 +601,14 @@ export function useImperativeHandle<T>(
601
601
  create: () => T,
602
602
  deps?: readonly unknown[],
603
603
  ): void {
604
- const handle = runWithoutDevToolsHookTracking(() => useMemo(create, deps));
605
-
606
604
  runWithoutDevToolsHookTracking(() =>
607
605
  useInsertionEffect(() => {
606
+ const handle = create();
608
607
  assignRef(ref, handle);
609
608
  return () => {
610
609
  assignRef(ref, null);
611
610
  };
612
- }, [ref, handle])
611
+ }, deps === undefined ? undefined : [ref, ...deps])
613
612
  );
614
613
  recordDevToolsHook("useImperativeHandle", deps === undefined
615
614
  ? { kind: "imperative-handle" }
@@ -983,7 +982,9 @@ export function hasStableExternalStores(
983
982
  }
984
983
 
985
984
  export function renderToString<TProps>(
986
- component: (props: TProps) => ReactCompatNode,
985
+ component:
986
+ | ((props: TProps) => ReactCompatNode)
987
+ | (new (props: TProps) => { render(): ReactCompatNode }),
987
988
  props?: TProps,
988
989
  options: RootRuntimeOptions = {},
989
990
  ): string {
@@ -994,7 +995,14 @@ export function renderToString<TProps>(
994
995
 
995
996
  return runWithCacheScope(createCacheScope(), () => {
996
997
  try {
997
- const rendered = renderWithRootRuntime(runtime, "0", () => component(props as TProps));
998
+ const rendered = renderWithRootRuntime(runtime, "0", () => {
999
+ if (isClassComponentType(component)) {
1000
+ const instance = new component(props as Record<string, unknown>);
1001
+ return instance.render();
1002
+ }
1003
+
1004
+ return (component as (props: TProps) => ReactCompatNode)(props as TProps);
1005
+ });
998
1006
  return typeof rendered === "string"
999
1007
  ? rendered
1000
1008
  : renderNodeToString(rendered, runtime, "0");
package/src/index.ts CHANGED
@@ -93,3 +93,4 @@ export {
93
93
  version,
94
94
  } from "./hooks.js";
95
95
  export type { StartTransition, TransitionScope } from "./hooks.js";
96
+ export { default } from "./react-default.js";
@@ -0,0 +1,133 @@
1
+ import {
2
+ Component,
3
+ PureComponent,
4
+ } from "./class-component.js";
5
+ import {
6
+ Fragment,
7
+ Activity,
8
+ Profiler,
9
+ Suspense,
10
+ SuspenseList,
11
+ StrictMode,
12
+ Children,
13
+ cloneElement,
14
+ createElement,
15
+ createErrorBoundary,
16
+ createPortal,
17
+ createRef,
18
+ forwardRef,
19
+ isValidElement,
20
+ lazy,
21
+ memo,
22
+ } from "./element.js";
23
+ import {
24
+ createContext,
25
+ renderContextConsumerToString,
26
+ renderContextProviderToString,
27
+ useContext,
28
+ } from "./context.js";
29
+ import {
30
+ applyStreamingHydrationFragments,
31
+ createRoot,
32
+ createStreamingHydrationRoot,
33
+ enableEventHydrationManifestReplay,
34
+ enableHydrationEventReplay,
35
+ flushSync,
36
+ hydrateRoot,
37
+ queueHydrationEvent,
38
+ readEventHydrationManifest,
39
+ render,
40
+ unmountComponentAtNode,
41
+ } from "./render.js";
42
+ import {
43
+ useCallback,
44
+ useDebugValue,
45
+ useDeferredValue,
46
+ useEffectEvent,
47
+ useEffect,
48
+ useId,
49
+ useImperativeHandle,
50
+ useInsertionEffect,
51
+ useLayoutEffect,
52
+ useMemo,
53
+ useOptimistic,
54
+ useReducer,
55
+ useRef,
56
+ useState,
57
+ useSyncExternalStore,
58
+ use,
59
+ useActionState,
60
+ act,
61
+ cache,
62
+ cacheSignal,
63
+ captureOwnerStack,
64
+ renderToString,
65
+ startTransition,
66
+ unstable_useCacheRefresh,
67
+ useTransition,
68
+ version,
69
+ } from "./hooks.js";
70
+
71
+ const ReactCompat = {
72
+ Component,
73
+ PureComponent,
74
+ Fragment,
75
+ Activity,
76
+ Profiler,
77
+ Suspense,
78
+ SuspenseList,
79
+ StrictMode,
80
+ Children,
81
+ cloneElement,
82
+ createElement,
83
+ createErrorBoundary,
84
+ createPortal,
85
+ createRef,
86
+ forwardRef,
87
+ isValidElement,
88
+ lazy,
89
+ memo,
90
+ createContext,
91
+ renderContextConsumerToString,
92
+ renderContextProviderToString,
93
+ useContext,
94
+ applyStreamingHydrationFragments,
95
+ createRoot,
96
+ createStreamingHydrationRoot,
97
+ enableEventHydrationManifestReplay,
98
+ enableHydrationEventReplay,
99
+ flushSync,
100
+ hydrateRoot,
101
+ queueHydrationEvent,
102
+ readEventHydrationManifest,
103
+ render,
104
+ unmountComponentAtNode,
105
+ useCallback,
106
+ useDebugValue,
107
+ useDeferredValue,
108
+ useEffectEvent,
109
+ useEffect,
110
+ useId,
111
+ useImperativeHandle,
112
+ useInsertionEffect,
113
+ useLayoutEffect,
114
+ useMemo,
115
+ useOptimistic,
116
+ useReducer,
117
+ useRef,
118
+ useState,
119
+ useSyncExternalStore,
120
+ use,
121
+ useActionState,
122
+ act,
123
+ cache,
124
+ cacheSignal,
125
+ captureOwnerStack,
126
+ renderToString,
127
+ startTransition,
128
+ unstable_useCacheRefresh,
129
+ useTransition,
130
+ version,
131
+ } as const;
132
+
133
+ export default ReactCompat;