Skip to content

Commit 8ea55ce

Browse files
committed
checkpoint: renderer state refactor to accessors
1 parent ac88f1f commit 8ea55ce

File tree

9 files changed

+145
-150
lines changed

9 files changed

+145
-150
lines changed

src/component.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import { commitRoot } from './diff/commit';
1+
import {
2+
commitRoot,
3+
setCurrentContext,
4+
setCurrentParentDom
5+
} from './diff/renderer';
26
import options from './options';
37
import { createVNode, Fragment } from './create-element';
48
import { patch } from './diff/patch';
59
import { DIRTY_BIT, FORCE_UPDATE, MODE_UNMOUNTING } from './constants';
610
import { getParentContext, getParentDom } from './tree';
711

8-
/**
9-
* The render queue
10-
* @type {import('./internal').RendererState}
11-
*/
12-
export const rendererState = {
13-
_parentDom: null,
14-
_context: {},
15-
_commitQueue: []
16-
};
17-
1812
/**
1913
* Base Component class. Provides `setState()` and `forceUpdate()`, which
2014
* trigger rendering
@@ -108,9 +102,10 @@ function rerender(internal) {
108102
0
109103
);
110104

111-
rendererState._context = getParentContext(internal);
112-
rendererState._commitQueue = [];
113-
rendererState._parentDom = getParentDom(internal);
105+
// set up renderer state
106+
setCurrentContext(getParentContext(internal));
107+
setCurrentParentDom(getParentDom(internal));
108+
114109
patch(internal, vnode);
115110
commitRoot(internal);
116111
}

src/create-root.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import {
44
MODE_SVG,
55
UNDEFINED
66
} from './constants';
7-
import { commitRoot } from './diff/commit';
7+
import {
8+
commitRoot,
9+
setCurrentContext,
10+
setCurrentParentDom
11+
} from './diff/renderer';
812
import { createElement, Fragment } from './create-element';
913
import options from './options';
1014
import { mount } from './diff/mount';
1115
import { patch } from './diff/patch';
1216
import { createInternal } from './tree';
13-
import { rendererState } from './component';
1417

1518
/**
1619
*
@@ -30,10 +33,8 @@ export function createRoot(parentDom) {
3033
firstChild =
3134
/** @type {import('./internal').PreactElement} */ (parentDom.firstChild);
3235

33-
rendererState._context = {};
34-
// List of effects that need to be called after diffing:
35-
rendererState._commitQueue = [];
36-
rendererState._parentDom = parentDom;
36+
setCurrentContext({});
37+
setCurrentParentDom(parentDom);
3738

3839
if (rootInternal) {
3940
patch(rootInternal, vnode);

src/diff/children.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { mount } from './mount';
1212
import { patch } from './patch';
1313
import { unmount } from './unmount';
1414
import { createInternal, getDomSibling } from '../tree';
15-
import { rendererState } from '../component';
15+
import { getCurrentParentDom } from './renderer';
1616

1717
/**
1818
* Update an internal with new children.
@@ -72,11 +72,7 @@ export function patchChildren(internal, children) {
7272
childInternal = createInternal(childVNode, internal);
7373

7474
// We are mounting a new VNode
75-
mount(
76-
childInternal,
77-
childVNode,
78-
getDomSibling(internal, skewedIndex)
79-
);
75+
mount(childInternal, childVNode, getDomSibling(internal, skewedIndex));
8076
}
8177
// If this node suspended during hydration, and no other flags are set:
8278
// @TODO: might be better to explicitly check for MODE_ERRORED here.
@@ -85,11 +81,7 @@ export function patchChildren(internal, children) {
8581
(MODE_HYDRATE | MODE_SUSPENDED)
8682
) {
8783
// We are resuming the hydration of a VNode
88-
mount(
89-
childInternal,
90-
childVNode,
91-
childInternal._dom
92-
);
84+
mount(childInternal, childVNode, childInternal._dom);
9385
} else {
9486
// Morph the old element into the new one, but don't append it to the dom yet
9587
patch(childInternal, childVNode);
@@ -102,7 +94,7 @@ export function patchChildren(internal, children) {
10294

10395
// Perform insert of new dom
10496
if (childInternal.flags & TYPE_DOM) {
105-
rendererState._parentDom.insertBefore(
97+
getCurrentParentDom().insertBefore(
10698
childInternal._dom,
10799
getDomSibling(internal, skewedIndex)
108100
);
@@ -136,13 +128,9 @@ export function patchChildren(internal, children) {
136128

137129
let nextSibling = getDomSibling(internal, skewedIndex + 1);
138130
if (childInternal.flags & TYPE_DOM) {
139-
rendererState._parentDom.insertBefore(childInternal._dom, nextSibling);
131+
getCurrentParentDom().insertBefore(childInternal._dom, nextSibling);
140132
} else {
141-
insertComponentDom(
142-
childInternal,
143-
nextSibling,
144-
rendererState._parentDom
145-
);
133+
insertComponentDom(childInternal, nextSibling, getCurrentParentDom());
146134
}
147135
}
148136

src/diff/commit.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/diff/component.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import options from '../options';
22
import { DIRTY_BIT, FORCE_UPDATE, SKIP_CHILDREN } from '../constants';
3-
import { rendererState } from '../component';
3+
import { getCurrentContext, setCurrentContext } from './renderer';
44

55
/**
66
* Render a function component
77
* @param {import('../internal').Internal} internal The component's backing Internal node
88
* @param {import('../internal').VNode} newVNode The new virtual node
99
* @returns {import('../internal').ComponentChildren} the component's children
1010
*/
11-
export function renderFunctionComponent(
12-
internal,
13-
newVNode,
14-
componentContext
15-
) {
11+
export function renderFunctionComponent(internal, newVNode, componentContext) {
1612
/** @type {import('../internal').Component} */
1713
let c;
1814

@@ -53,10 +49,12 @@ export function renderFunctionComponent(
5349
}
5450
internal.flags &= ~DIRTY_BIT;
5551
if (c.getChildContext != null) {
56-
rendererState._context = internal._context = Object.assign(
57-
{},
58-
rendererState._context,
59-
c.getChildContext()
52+
setCurrentContext(
53+
(internal._context = Object.assign(
54+
{},
55+
getCurrentContext(),
56+
c.getChildContext()
57+
))
6058
);
6159
}
6260

@@ -69,11 +67,7 @@ export function renderFunctionComponent(
6967
* @param {import('../internal').VNode} newVNode The new virtual node
7068
* @returns {import('../internal').ComponentChildren} the component's children
7169
*/
72-
export function renderClassComponent(
73-
internal,
74-
newVNode,
75-
componentContext
76-
) {
70+
export function renderClassComponent(internal, newVNode, componentContext) {
7771
/** @type {import('../internal').Component} */
7872
let c;
7973
let isNew, oldProps, oldState, snapshot;
@@ -163,10 +157,12 @@ export function renderClassComponent(
163157
c.state = c._nextState;
164158

165159
if (c.getChildContext != null) {
166-
rendererState._context = internal._context = Object.assign(
167-
{},
168-
rendererState._context,
169-
c.getChildContext()
160+
setCurrentContext(
161+
(internal._context = Object.assign(
162+
{},
163+
getCurrentContext(),
164+
c.getChildContext()
165+
))
170166
);
171167
}
172168

src/diff/mount.js

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ import { setProperty } from './props';
1717
import { renderClassComponent, renderFunctionComponent } from './component';
1818
import { createInternal } from '../tree';
1919
import options from '../options';
20-
import { rendererState } from '../component';
20+
import {
21+
commitQueue,
22+
getCurrentContext,
23+
getCurrentParentDom,
24+
setCurrentContext,
25+
setCurrentParentDom
26+
} from './renderer';
27+
2128
/**
2229
* Diff two virtual nodes and apply proper changes to the DOM
2330
* @param {import('../internal').Internal} internal The Internal node to mount
@@ -37,37 +44,34 @@ export function mount(internal, newVNode, startDom) {
3744
// the page. Root nodes can occur anywhere in the tree and not just at the
3845
// top.
3946
let prevStartDom = startDom;
40-
let prevParentDom = rendererState._parentDom;
47+
let prevParentDom = getCurrentParentDom();
4148
if (internal.flags & TYPE_ROOT) {
42-
rendererState._parentDom = newVNode.props._parentDom;
49+
let newParentDom = newVNode.props._parentDom;
50+
setCurrentParentDom(newParentDom);
4351

4452
// Note: this is likely always true because we are inside mount()
45-
if (rendererState._parentDom !== prevParentDom) {
53+
if (newParentDom !== prevParentDom) {
4654
startDom = null;
4755
}
4856
}
4957

50-
let prevContext = rendererState._context;
58+
let prevContext = getCurrentContext();
5159
// Necessary for createContext api. Setting this property will pass
5260
// the context value as `this.context` just for this component.
5361
let tmp = newVNode.type.contextType;
54-
let provider = tmp && rendererState._context[tmp._id];
62+
let provider = tmp && prevContext[tmp._id];
5563
let componentContext = tmp
5664
? provider
5765
? provider.props.value
5866
: tmp._defaultValue
59-
: rendererState._context;
67+
: prevContext;
6068

6169
if (provider) provider._subs.add(internal);
6270

6371
let renderResult;
6472

6573
if (internal.flags & TYPE_CLASS) {
66-
renderResult = renderClassComponent(
67-
internal,
68-
null,
69-
componentContext
70-
);
74+
renderResult = renderClassComponent(internal, null, componentContext);
7175
} else {
7276
renderResult = renderFunctionComponent(
7377
internal,
@@ -91,23 +95,19 @@ export function mount(internal, newVNode, startDom) {
9195
renderResult = [renderResult];
9296
}
9397

94-
nextDomSibling = mountChildren(
95-
internal,
96-
renderResult,
97-
startDom
98-
);
98+
nextDomSibling = mountChildren(internal, renderResult, startDom);
9999
}
100100

101101
if (
102102
internal._commitCallbacks != null &&
103103
internal._commitCallbacks.length
104104
) {
105-
rendererState._commitQueue.push(internal);
105+
commitQueue.push(internal);
106106
}
107107

108108
if (
109109
internal.flags & TYPE_ROOT &&
110-
prevParentDom !== rendererState._parentDom
110+
prevParentDom !== getCurrentParentDom()
111111
) {
112112
// If we just mounted a root node/Portal, and it changed the parentDom
113113
// of it's children, then we need to resume the diff from it's previous
@@ -117,10 +117,10 @@ export function mount(internal, newVNode, startDom) {
117117
nextDomSibling = prevStartDom;
118118
}
119119

120-
rendererState._parentDom = prevParentDom;
120+
setCurrentParentDom(prevParentDom);
121121
// In the event this subtree creates a new context for its children, restore
122122
// the previous context for its siblings
123-
rendererState._context = prevContext;
123+
setCurrentContext(prevContext);
124124
} else {
125125
// @TODO: we could just assign this as internal.dom here
126126
let hydrateDom =
@@ -269,14 +269,14 @@ function mountElement(internal, dom) {
269269
dom.innerHTML = newHtml.__html;
270270
}
271271
} else if (newChildren != null) {
272-
const prevParentDom = rendererState._parentDom;
273-
rendererState._parentDom = dom;
272+
const prevParentDom = getCurrentParentDom();
273+
setCurrentParentDom(dom);
274274
mountChildren(
275275
internal,
276276
Array.isArray(newChildren) ? newChildren : [newChildren],
277277
isNew ? null : dom.firstChild
278278
);
279-
rendererState._parentDom = prevParentDom;
279+
setCurrentParentDom(prevParentDom);
280280
}
281281

282282
// (as above, don't diff props during hydration)
@@ -295,11 +295,7 @@ function mountElement(internal, dom) {
295295
* @param {import('../internal').ComponentChild[]} children
296296
* @param {import('../internal').PreactNode} startDom
297297
*/
298-
export function mountChildren(
299-
internal,
300-
children,
301-
startDom
302-
) {
298+
export function mountChildren(internal, children, startDom) {
303299
let internalChildren = (internal._children = []),
304300
i,
305301
childVNode,
@@ -321,11 +317,7 @@ export function mountChildren(
321317
internalChildren[i] = childInternal;
322318

323319
// Morph the old element into the new one, but don't append it to the dom yet
324-
mountedNextChild = mount(
325-
childInternal,
326-
childVNode,
327-
startDom
328-
);
320+
mountedNextChild = mount(childInternal, childVNode, startDom);
329321

330322
newDom = childInternal._dom;
331323

@@ -338,7 +330,7 @@ export function mountChildren(
338330
// The DOM the diff should begin with is now startDom (since we inserted
339331
// newDom before startDom) so ignore mountedNextChild and continue with
340332
// startDom
341-
rendererState._parentDom.insertBefore(newDom, startDom);
333+
getCurrentParentDom().insertBefore(newDom, startDom);
342334
}
343335

344336
if (childInternal.ref) {

0 commit comments

Comments
 (0)