@@ -23,6 +23,11 @@ const {
2323
2424let prevDispatcher = ReactCurrentDispatcher . current
2525
26+ /** visitChildren walks all elements (depth-first) and while it walks the
27+ element tree some components will suspend and put a `Frame` onto
28+ the queue. Hence we recursively look at suspended components in
29+ this queue, wait for their promises to resolve, and continue
30+ calling visitChildren on their children. */
2631const flushFrames = ( queue : Frame [ ] , visitor : Visitor ) : Promise < void > => {
2732 if ( queue . length === 0 ) {
2833 return Promise . resolve ( )
@@ -35,6 +40,9 @@ const flushFrames = (queue: Frame[], visitor: Visitor): Promise<void> => {
3540 ReactCurrentDispatcher . current = Dispatcher
3641
3742 let children = [ ]
43+
44+ // Update the component after we've suspended to rerender it,
45+ // at which point we'll actually get its children
3846 if ( frame . kind === 'frame.class' ) {
3947 children = getChildrenArray ( updateClassComponent ( queue , frame ) )
4048 } else if ( frame . kind === 'frame.hooks' ) {
@@ -43,6 +51,8 @@ const flushFrames = (queue: Frame[], visitor: Visitor): Promise<void> => {
4351 children = getChildrenArray ( updateLazyComponent ( queue , frame ) )
4452 }
4553
54+ // Now continue walking the previously suspended component's
55+ // children (which might also suspend)
4656 visitChildren ( children , queue , visitor )
4757 ReactCurrentDispatcher . current = prevDispatcher
4858
@@ -56,12 +66,19 @@ const renderPrepass = (element: Node, visitor?: Visitor): Promise<void> => {
5666 const queue : Frame [ ] = [ ]
5767 let fn = visitor !== undefined ? visitor : defaultVisitor
5868
69+ // Context state is kept globally and is modified in-place.
70+ // Before we start walking the element tree we need to reset
71+ // its current state
5972 setCurrentContextMap ( { } )
6073 setCurrentContextStore ( new Map ( ) )
6174
6275 try {
76+ // The "Dispatcher" is what handles hook calls and
77+ // a React internal that needs to be set to our
78+ // dispatcher and reset after we're done
6379 prevDispatcher = ReactCurrentDispatcher . current
6480 ReactCurrentDispatcher . current = Dispatcher
81+
6582 visitChildren ( getChildrenArray ( element ) , queue , fn )
6683 } catch ( error ) {
6784 return Promise . reject ( error )
0 commit comments