|
1 | 1 | import { assertNever } from '@contember/utilities'
|
2 |
| -import type { ElementType, ReactElement, ReactNode } from 'react' |
| 2 | +import { ElementType, ReactElement, ReactNode, useReducer } from 'react' |
| 3 | +import * as React from 'react' |
3 | 4 | import type { BranchNodeList } from './BranchNodeList'
|
4 | 5 | import { ChildrenAnalyzerError } from './ChildrenAnalyzerError'
|
5 | 6 | import type { ChildrenAnalyzerOptions } from './ChildrenAnalyzerOptions'
|
@@ -66,17 +67,30 @@ export class ChildrenAnalyzer<
|
66 | 67 | children: ReactNode,
|
67 | 68 | initialStaticContext: StaticContext,
|
68 | 69 | ): Array<AllLeavesRepresentation | AllBranchNodesRepresentation> {
|
| 70 | + if ('__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' in React) { |
| 71 | + const { ReactCurrentDispatcher } = (React as any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED |
| 72 | + const originalCurrentDispatcher = ReactCurrentDispatcher.current |
| 73 | + ReactCurrentDispatcher.current = { |
| 74 | + ...originalCurrentDispatcher, |
| 75 | + ...staticRenderReactDispatcher, |
| 76 | + } |
| 77 | + const result = this.doProcessChildren(children, initialStaticContext) |
| 78 | + ReactCurrentDispatcher.current = originalCurrentDispatcher |
| 79 | + return result |
| 80 | + } |
| 81 | + return this.doProcessChildren(children, initialStaticContext) |
| 82 | + } |
| 83 | + |
| 84 | + public doProcessChildren( |
| 85 | + children: ReactNode, |
| 86 | + initialStaticContext: StaticContext, |
| 87 | + ): Array<AllLeavesRepresentation | AllBranchNodesRepresentation> { |
| 88 | + |
69 | 89 | const processed = this.processNode(children, initialStaticContext, [])
|
70 | 90 |
|
71 |
| - const rawResult: Array<AllLeavesRepresentation | AllBranchNodesRepresentation | undefined> = Array.isArray( |
72 |
| - processed, |
73 |
| - ) |
74 |
| - ? processed |
75 |
| - : [processed] |
| 91 | + const rawResult = Array.isArray(processed) ? processed : [processed] |
76 | 92 |
|
77 |
| - return rawResult.filter( |
78 |
| - (item): item is AllLeavesRepresentation | AllBranchNodesRepresentation => item !== undefined, |
79 |
| - ) |
| 93 | + return rawResult.filter((item): item is AllLeavesRepresentation | AllBranchNodesRepresentation => item !== undefined) |
80 | 94 | }
|
81 | 95 |
|
82 | 96 | private processNode(
|
@@ -258,3 +272,46 @@ export class ChildrenAnalyzer<
|
258 | 272 | return processedChildren
|
259 | 273 | }
|
260 | 274 | }
|
| 275 | + |
| 276 | +let id = 0 |
| 277 | +const staticRenderReactDispatcher = { |
| 278 | + useCallback: (cb: any) => { |
| 279 | + return cb |
| 280 | + }, |
| 281 | + useDebugValue: () => { |
| 282 | + // do nothing |
| 283 | + }, |
| 284 | + useEffect: () => { |
| 285 | + // do nothing |
| 286 | + }, |
| 287 | + useImperativeHandle: () => { |
| 288 | + // do nothing |
| 289 | + }, |
| 290 | + useLayoutEffect: () => { |
| 291 | + // do nothing |
| 292 | + }, |
| 293 | + useMemo: (value: () => any) => { |
| 294 | + return value() |
| 295 | + }, |
| 296 | + useReducer: (reducer: any, initializerArg: any, initializer: any) => { |
| 297 | + return initializer ? [initializer(initializerArg), () => { |
| 298 | + }] : [initializerArg, () => { |
| 299 | + }] |
| 300 | + }, |
| 301 | + useRef: (initialValue: any) => { |
| 302 | + return { current: initialValue } |
| 303 | + }, |
| 304 | + useState: (initialState: any) => { |
| 305 | + return [typeof initialState === 'function' ? initialState() : initialState, () => { |
| 306 | + }] |
| 307 | + }, |
| 308 | + useDeferredValue: (value: any) => { |
| 309 | + return value |
| 310 | + }, |
| 311 | + useId() { |
| 312 | + return `id-${id++}` |
| 313 | + }, |
| 314 | + useInsertionEffect() { |
| 315 | + // do nothing |
| 316 | + }, |
| 317 | +} |
0 commit comments