Skip to content

Commit de9ab95

Browse files
committed
Add additional comments on Context, Frames, and render
1 parent 34deaed commit de9ab95

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const {
2323

2424
let 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. */
2631
const 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)

src/internals/context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
ContextStore
88
} from '../types'
99

10-
/* The context is kept as a Map from a Context value to the current
10+
/** The context is kept as a Map from a Context value to the current
1111
value on the React element tree.
1212
The legacy context is kept as a simple object.
1313
When the tree is being walked modifications are made by assigning

src/render/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// @flow
22

3+
/** Every type of component here can suspend itself.
4+
This means it pushes a `Frame` to the queue.
5+
Components are first mounted in visitor.js,
6+
and if they have suspended after their promise
7+
resolves `update` is called instead for them,
8+
which preserves their previous mounted state
9+
and rerenders the component. */
10+
311
export {
412
mount as mountLazyComponent,
513
update as updateLazyComponent

src/visitor.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,18 @@ export const visitElement = (
148148
}
149149
}
150150

151+
/** The context (legacy and createContext are separate) are kept
152+
as global state. As we're walking the tree depth-first, we
153+
simply overwrite it with new values. This is recursive and
154+
as we walk back upwards (after visitChildren) we restore
155+
the value that we have previously overwritten. */
151156
const visitChild = (
152157
child: AbstractElement,
153158
queue: Frame[],
154159
visitor: Visitor
155160
) => {
156161
const children = visitElement(child, queue, visitor)
157-
// Flush the context changes
162+
// Flush changes (if any) that have been made to the context
158163
const prevMap = flushPrevContextMap()
159164
const prevStore = flushPrevContextStore()
160165

0 commit comments

Comments
 (0)