Skip to content

Commit c13793c

Browse files
committed
Remove _nextDom usage
1 parent 514183f commit c13793c

File tree

5 files changed

+27
-50
lines changed

5 files changed

+27
-50
lines changed

jsx-runtime/src/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ function createVNode(type, props, key, isStaticChildren, __source, __self) {
5050
_parent: null,
5151
_depth: 0,
5252
_dom: null,
53-
_nextDom: undefined,
5453
_component: null,
5554
constructor: undefined,
5655
_original: --vnodeId,

src/create-element.js

-5
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ export function createVNode(type, props, key, ref, original) {
6767
_parent: null,
6868
_depth: 0,
6969
_dom: null,
70-
// _nextDom must be initialized to undefined b/c it will eventually
71-
// be set to dom.nextSibling which can return `null` and it is important
72-
// to be able to distinguish between an uninitialized _nextDom and
73-
// a _nextDom that has been set to `null`
74-
_nextDom: UNDEFINED,
7570
_component: null,
7671
constructor: UNDEFINED,
7772
_original: original == null ? ++vnodeId : original,

src/diff/children.js

+21-33
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ export function diffChildren(
6262

6363
let newChildrenLength = renderResult.length;
6464

65-
newParentVNode._nextDom = oldDom;
66-
constructNewChildrenArray(newParentVNode, renderResult, oldChildren);
67-
oldDom = newParentVNode._nextDom;
65+
oldDom = constructNewChildrenArray(
66+
newParentVNode,
67+
renderResult,
68+
oldChildren,
69+
oldDom
70+
);
6871

6972
for (i = 0; i < newChildrenLength; i++) {
7073
childVNode = newParentVNode._children[i];
@@ -82,7 +85,7 @@ export function diffChildren(
8285
childVNode._index = i;
8386

8487
// Morph the old element into the new one, but don't append it to the dom yet
85-
diff(
88+
let result = diff(
8689
parentDom,
8790
childVNode,
8891
oldVNode,
@@ -117,49 +120,32 @@ export function diffChildren(
117120
oldVNode._children === childVNode._children
118121
) {
119122
oldDom = insert(childVNode, oldDom, parentDom);
120-
} else if (
121-
typeof childVNode.type == 'function' &&
122-
childVNode._nextDom !== UNDEFINED
123-
) {
124-
// Since Fragments or components that return Fragment like VNodes can
125-
// contain multiple DOM nodes as the same level, continue the diff from
126-
// the sibling of last DOM child of this child VNode
127-
oldDom = childVNode._nextDom;
123+
} else if (typeof childVNode.type == 'function' && result !== UNDEFINED) {
124+
oldDom = result;
128125
} else if (newDom) {
129126
oldDom = newDom.nextSibling;
130127
}
131128

132-
// Eagerly cleanup _nextDom. We don't need to persist the value because it
133-
// is only used by `diffChildren` to determine where to resume the diff
134-
// after diffing Components and Fragments. Once we store it the nextDOM
135-
// local var, we can clean up the property. Also prevents us hanging on to
136-
// DOM nodes that may have been unmounted.
137-
childVNode._nextDom = UNDEFINED;
138-
139129
// Unset diffing flags
140130
childVNode._flags &= ~(INSERT_VNODE | MATCHED);
141131
}
142132

143-
// TODO: With new child diffing algo, consider alt ways to diff Fragments.
144-
// Such as dropping oldDom and moving fragments in place
145-
//
146-
// Because the newParentVNode is Fragment-like, we need to set it's
147-
// _nextDom property to the nextSibling of its last child DOM node.
148-
//
149-
// `oldDom` contains the correct value here because if the last child
150-
// is a Fragment-like, then oldDom has already been set to that child's _nextDom.
151-
// If the last child is a DOM VNode, then oldDom will be set to that DOM
152-
// node's nextSibling.
153-
newParentVNode._nextDom = oldDom;
154133
newParentVNode._dom = firstChildDom;
134+
135+
return oldDom;
155136
}
156137

157138
/**
158139
* @param {VNode} newParentVNode
159140
* @param {ComponentChildren[]} renderResult
160141
* @param {VNode[]} oldChildren
161142
*/
162-
function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) {
143+
function constructNewChildrenArray(
144+
newParentVNode,
145+
renderResult,
146+
oldChildren,
147+
oldDom
148+
) {
163149
/** @type {number} */
164150
let i;
165151
/** @type {VNode} */
@@ -309,14 +295,16 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) {
309295
for (i = 0; i < oldChildrenLength; i++) {
310296
oldVNode = oldChildren[i];
311297
if (oldVNode != null && (oldVNode._flags & MATCHED) === 0) {
312-
if (oldVNode._dom == newParentVNode._nextDom) {
313-
newParentVNode._nextDom = getDomSibling(oldVNode);
298+
if (oldVNode._dom == oldDom) {
299+
oldDom = getDomSibling(oldVNode);
314300
}
315301

316302
unmount(oldVNode, oldVNode);
317303
}
318304
}
319305
}
306+
307+
return oldDom;
320308
}
321309

322310
/**

src/diff/index.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export function diff(
244244
tmp != null && tmp.type === Fragment && tmp.key == null;
245245
let renderResult = isTopLevelFragment ? tmp.props.children : tmp;
246246

247-
diffChildren(
247+
oldDom = diffChildren(
248248
parentDom,
249249
isArray(renderResult) ? renderResult : [renderResult],
250250
newVNode,
@@ -281,6 +281,7 @@ export function diff(
281281
while (oldDom && oldDom.nodeType === 8 && oldDom.nextSibling) {
282282
oldDom = oldDom.nextSibling;
283283
}
284+
284285
excessDomChildren[excessDomChildren.indexOf(oldDom)] = null;
285286
newVNode._dom = oldDom;
286287
} else {
@@ -296,7 +297,7 @@ export function diff(
296297
newVNode._children = oldVNode._children;
297298
newVNode._dom = oldVNode._dom;
298299
} else {
299-
newVNode._dom = diffElementNodes(
300+
oldDom = newVNode._dom = diffElementNodes(
300301
oldVNode._dom,
301302
newVNode,
302303
oldVNode,
@@ -310,6 +311,8 @@ export function diff(
310311
}
311312

312313
if ((tmp = options.diffed)) tmp(newVNode);
314+
315+
return newVNode._flags & MODE_SUSPENDED ? undefined : oldDom;
313316
}
314317

315318
/**
@@ -318,8 +321,6 @@ export function diff(
318321
* @param {VNode} root
319322
*/
320323
export function commitRoot(commitQueue, root, refQueue) {
321-
root._nextDom = UNDEFINED;
322-
323324
for (let i = 0; i < refQueue.length; i++) {
324325
applyRef(refQueue[i], refQueue[++i], refQueue[++i]);
325326
}
@@ -632,9 +633,7 @@ export function unmount(vnode, parentVNode, skipRemove) {
632633
removeNode(vnode._dom);
633634
}
634635

635-
// Must be set to `undefined` to properly clean up `_nextDom`
636-
// for which `null` is a valid value. See comment in `create-element.js`
637-
vnode._component = vnode._parent = vnode._dom = vnode._nextDom = UNDEFINED;
636+
vnode._component = vnode._parent = vnode._dom = UNDEFINED;
638637
}
639638

640639
/** The `.render()` method for a PFC backing instance. */

src/internal.d.ts

-4
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ declare global {
150150
* The [first (for Fragments)] DOM child of a VNode
151151
*/
152152
_dom: PreactElement | null;
153-
/**
154-
* The last dom child of a Fragment, or components that return a Fragment
155-
*/
156-
_nextDom: PreactElement | null | undefined;
157153
_component: Component | null;
158154
constructor: undefined;
159155
_original: number;

0 commit comments

Comments
 (0)