Skip to content

Commit

Permalink
feat(react-multipsass-rendering): custom react dispatcher to support …
Browse files Browse the repository at this point in the history
…some hooks in a staticRender
  • Loading branch information
matej21 committed Mar 1, 2024
1 parent 0e23dbc commit a917a39
Showing 1 changed file with 66 additions and 9 deletions.
75 changes: 66 additions & 9 deletions packages/react-multipass-rendering/src/ChildrenAnalyzer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertNever } from '@contember/utilities'
import type { ElementType, ReactElement, ReactNode } from 'react'
import { ElementType, ReactElement, ReactNode, useReducer } from 'react'
import * as React from 'react'
import type { BranchNodeList } from './BranchNodeList'
import { ChildrenAnalyzerError } from './ChildrenAnalyzerError'
import type { ChildrenAnalyzerOptions } from './ChildrenAnalyzerOptions'
Expand Down Expand Up @@ -66,17 +67,30 @@ export class ChildrenAnalyzer<
children: ReactNode,
initialStaticContext: StaticContext,
): Array<AllLeavesRepresentation | AllBranchNodesRepresentation> {
if ('__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' in React) {
const { ReactCurrentDispatcher } = (React as any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
const originalCurrentDispatcher = ReactCurrentDispatcher.current
ReactCurrentDispatcher.current = {
...originalCurrentDispatcher,
...staticRenderReactDispatcher,
}
const result = this.doProcessChildren(children, initialStaticContext)
ReactCurrentDispatcher.current = originalCurrentDispatcher
return result
}
return this.doProcessChildren(children, initialStaticContext)
}

public doProcessChildren(
children: ReactNode,
initialStaticContext: StaticContext,
): Array<AllLeavesRepresentation | AllBranchNodesRepresentation> {

const processed = this.processNode(children, initialStaticContext, [])

const rawResult: Array<AllLeavesRepresentation | AllBranchNodesRepresentation | undefined> = Array.isArray(
processed,
)
? processed
: [processed]
const rawResult = Array.isArray(processed) ? processed : [processed]

return rawResult.filter(
(item): item is AllLeavesRepresentation | AllBranchNodesRepresentation => item !== undefined,
)
return rawResult.filter((item): item is AllLeavesRepresentation | AllBranchNodesRepresentation => item !== undefined)
}

private processNode(
Expand Down Expand Up @@ -258,3 +272,46 @@ export class ChildrenAnalyzer<
return processedChildren
}
}

let id = 0
const staticRenderReactDispatcher = {
useCallback: (cb: any) => {
return cb
},
useDebugValue: () => {
// do nothing
},
useEffect: () => {
// do nothing
},
useImperativeHandle: () => {
// do nothing
},
useLayoutEffect: () => {
// do nothing
},
useMemo: (value: () => any) => {
return value()
},
useReducer: (reducer: any, initializerArg: any, initializer: any) => {
return initializer ? [initializer(initializerArg), () => {
}] : [initializerArg, () => {
}]
},
useRef: (initialValue: any) => {
return { current: initialValue }
},
useState: (initialState: any) => {
return [typeof initialState === 'function' ? initialState() : initialState, () => {
}]
},
useDeferredValue: (value: any) => {
return value
},
useId() {
return `id-${id++}`
},
useInsertionEffect() {
// do nothing
},
}

0 comments on commit a917a39

Please sign in to comment.