Skip to content

Commit 9270edd

Browse files
committed
refactor: use direct fields for nextSibling and parent
1 parent 18512c7 commit 9270edd

14 files changed

+155
-186
lines changed

packages/qwik/src/core/client/dom-container.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ import {
5151
vnode_applyJournal,
5252
vnode_createErrorDiv,
5353
vnode_getDomParent,
54-
vnode_getNextSibling,
55-
vnode_getParent,
5654
vnode_getProps,
5755
vnode_insertBefore,
5856
vnode_isElementVNode,
@@ -170,13 +168,13 @@ export class DomContainer extends _SharedContainer implements IClientContainer {
170168
return inflateQRL(this, parseQRL(qrl)) as QRL<T>;
171169
}
172170

173-
handleError(err: any, host: HostElement | null): void {
171+
handleError(err: any, host: VNode | null): void {
174172
if (qDev && host) {
175173
if (typeof document !== 'undefined') {
176174
const vHost = host as VirtualVNode;
177175
const journal: VNodeJournal = [];
178-
const vHostParent = vnode_getParent(vHost) as VirtualVNode | ElementVNode | undefined;
179-
const vHostNextSibling = vnode_getNextSibling(vHost);
176+
const vHostParent = vHost.parent;
177+
const vHostNextSibling = vHost.nextSibling as VNode | null;
180178
const vErrorDiv = vnode_createErrorDiv(document, vHost, err, journal);
181179
// If the host is an element node, we need to insert the error div into its parent.
182180
const insertHost = vnode_isElementVNode(vHost) ? vHostParent || vHost : vHost;
@@ -202,15 +200,15 @@ export class DomContainer extends _SharedContainer implements IClientContainer {
202200
errorStore.error = err;
203201
}
204202

205-
setContext<T>(host: HostElement, context: ContextId<T>, value: T): void {
203+
setContext<T>(host: VNode, context: ContextId<T>, value: T): void {
206204
let ctx = this.getHostProp<Array<string | unknown>>(host, QCtxAttr);
207205
if (ctx == null) {
208206
this.setHostProp(host, QCtxAttr, (ctx = []));
209207
}
210208
mapArray_set(ctx, context.id, value, 0, true);
211209
}
212210

213-
resolveContext<T>(host: HostElement, contextId: ContextId<T>): T | undefined {
211+
resolveContext<T>(host: VNode, contextId: ContextId<T>): T | undefined {
214212
while (host) {
215213
const ctx = this.getHostProp<Array<string | unknown>>(host, QCtxAttr);
216214
if (ctx != null && mapArray_has(ctx, contextId.id, 0)) {
@@ -221,19 +219,19 @@ export class DomContainer extends _SharedContainer implements IClientContainer {
221219
return undefined;
222220
}
223221

224-
getParentHost(host: HostElement): HostElement | null {
225-
let vNode = vnode_getParent(host as any);
222+
getParentHost(host: VNode): VNode | null {
223+
let vNode: VNode | null = host.parent;
226224
while (vNode) {
227225
if (vnode_isVirtualVNode(vNode)) {
228226
if (vNode.getProp(OnRenderProp, null) !== null) {
229-
return vNode as any as HostElement;
227+
return vNode;
230228
}
231229
vNode =
232-
vnode_getParent(vNode) ||
230+
vNode.parent ||
233231
// If virtual node, than it could be a slot so we need to read its parent.
234-
vNode.getSlotParent();
232+
vNode.slotParent;
235233
} else {
236-
vNode = vnode_getParent(vNode);
234+
vNode = vNode.parent;
237235
}
238236
}
239237
return null;

packages/qwik/src/core/client/vnode-diff.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ import {
5151
vnode_getDomParentVNode,
5252
vnode_getElementName,
5353
vnode_getFirstChild,
54-
vnode_getNextSibling,
5554
vnode_getNode,
56-
vnode_getParent,
5755
vnode_getProjectionParentComponent,
5856
vnode_getProps,
5957
vnode_getText,
@@ -270,22 +268,22 @@ export const vnode_diff = (
270268
/**
271269
* Advance the `vCurrent` to the next sibling.
272270
*
273-
* Normally this is just `vCurrent = vnode_getNextSibling(vCurrent)`. However, this gets
274-
* complicated if `retrieveChildWithKey` was called, because then we are consuming nodes out of
275-
* order and can't rely on `vnode_getNextSibling` and instead we need to go by `vSiblings`.
271+
* Normally this is just `vCurrent = vCurrent.nextSibling`. However, this gets complicated if
272+
* `retrieveChildWithKey` was called, because then we are consuming nodes out of order and can't
273+
* rely on `nextSibling` and instead we need to go by `vSiblings`.
276274
*/
277275
function peekNextSibling() {
278276
// If we don't have a `vNewNode`, than that means we just reconciled the current node.
279277
// So advance it.
280-
return vCurrent ? vnode_getNextSibling(vCurrent) : null;
278+
return vCurrent ? (vCurrent.nextSibling as VNode | null) : null;
281279
}
282280

283281
/**
284282
* Advance the `vCurrent` to the next sibling.
285283
*
286-
* Normally this is just `vCurrent = vnode_getNextSibling(vCurrent)`. However, this gets
287-
* complicated if `retrieveChildWithKey` was called, because then we are consuming nodes out of
288-
* order and can't rely on `vnode_getNextSibling` and instead we need to go by `vSiblings`.
284+
* Normally this is just `vCurrent = vCurrent.nextSibling`. However, this gets complicated if
285+
* `retrieveChildWithKey` was called, because then we are consuming nodes out of order and can't
286+
* rely on `nextSibling` and instead we need to go by `vSiblings`.
289287
*/
290288
function advanceToNextSibling() {
291289
vCurrent = peekNextSibling();
@@ -534,7 +532,7 @@ export const vnode_diff = (
534532
let vChild: VNode | null = vFirstChild;
535533
while (vChild) {
536534
cleanup(container, vChild);
537-
vChild = vnode_getNextSibling(vChild);
535+
vChild = vChild.nextSibling as VNode | null;
538536
}
539537
vnode_truncate(journal, vCurrent as ElementVNode | VirtualVNode, vFirstChild);
540538
}
@@ -547,7 +545,7 @@ export const vnode_diff = (
547545
while (vCurrent) {
548546
const toRemove = vCurrent;
549547
advanceToNextSibling();
550-
if (vParent === vnode_getParent(toRemove)) {
548+
if (vParent === toRemove.parent) {
551549
cleanup(container, toRemove);
552550
// If we are diffing projection than the parent is not the parent of the node.
553551
// If that is the case we don't want to remove the node from the parent.
@@ -708,7 +706,7 @@ export const vnode_diff = (
708706
if (!isSameElementName || jsxKey !== getKey(vCurrent)) {
709707
// So we have a key and it does not match the current node.
710708
// We need to do a forward search to find it.
711-
// The complication is that once we start taking nodes out of order we can't use `vnode_getNextSibling`
709+
// The complication is that once we start taking nodes out of order we can't use `nextSibling`
712710
vNewNode = retrieveChildWithKey(elementName, jsxKey);
713711
if (vNewNode === null) {
714712
// No existing node with key exists, just create a new one.
@@ -962,7 +960,7 @@ export const vnode_diff = (
962960
vSiblings.set(name + ':' + vKey, vNode);
963961
}
964962
}
965-
vNode = vnode_getNextSibling(vNode);
963+
vNode = vNode.nextSibling as VNode | null;
966964
}
967965
} else {
968966
if (key === null) {
@@ -1129,7 +1127,7 @@ export const vnode_diff = (
11291127
) === null
11301128
: true)
11311129
) {
1132-
componentHost = vnode_getParent(componentHost);
1130+
componentHost = componentHost.parent;
11331131
}
11341132

11351133
const jsxOutput = executeComponent(
@@ -1370,7 +1368,7 @@ export function cleanup(container: ClientContainer, vNode: VNode) {
13701368
let projectionChild = vnode_getFirstChild(projection);
13711369
while (projectionChild) {
13721370
cleanup(container, projectionChild);
1373-
projectionChild = vnode_getNextSibling(projectionChild);
1371+
projectionChild = projectionChild.nextSibling as VNode | null;
13741372
}
13751373

13761374
cleanupStaleUnclaimedProjection(container.$journal$, projection);
@@ -1403,7 +1401,7 @@ export function cleanup(container: ClientContainer, vNode: VNode) {
14031401
*/
14041402
if (vNode.flags & VNodeFlags.Virtual) {
14051403
// The QSlotParent is used to find the slot parent during scheduling
1406-
vNode.getSlotParent();
1404+
vNode.slotParent;
14071405
}
14081406
});
14091407
return;
@@ -1418,25 +1416,25 @@ export function cleanup(container: ClientContainer, vNode: VNode) {
14181416
return;
14191417
}
14201418
// Out of children, go to next sibling
1421-
const vNextSibling = vnode_getNextSibling(vCursor);
1419+
const vNextSibling = vCursor.nextSibling as VNode | null;
14221420
if (vNextSibling) {
14231421
vCursor = vNextSibling;
14241422
continue;
14251423
}
14261424

14271425
// Out of siblings, go to parent
1428-
vParent = vnode_getParent(vCursor);
1426+
vParent = vCursor.parent;
14291427
while (vParent) {
14301428
if (vParent === vNode) {
14311429
// We are back where we started, we are done.
14321430
return;
14331431
}
1434-
const vNextParentSibling = vnode_getNextSibling(vParent);
1432+
const vNextParentSibling = vParent.nextSibling as VNode | null;
14351433
if (vNextParentSibling) {
14361434
vCursor = vNextParentSibling;
14371435
break;
14381436
}
1439-
vParent = vnode_getParent(vParent);
1437+
vParent = vParent.parent;
14401438
}
14411439
if (vParent == null) {
14421440
// We are done.
@@ -1448,7 +1446,7 @@ export function cleanup(container: ClientContainer, vNode: VNode) {
14481446
function cleanupStaleUnclaimedProjection(journal: VNodeJournal, projection: VNode) {
14491447
// we are removing a node where the projection would go after slot render.
14501448
// This is not needed, so we need to cleanup still unclaimed projection
1451-
const projectionParent = vnode_getParent(projection);
1449+
const projectionParent = projection.parent;
14521450
if (projectionParent) {
14531451
const projectionParentType = projectionParent.flags;
14541452
if (

packages/qwik/src/core/client/vnode-namespace.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import {
1717
vnode_getDomParentVNode,
1818
vnode_getElementName,
1919
vnode_getFirstChild,
20-
vnode_getNextSibling,
21-
vnode_getParent,
2220
vnode_isElementVNode,
2321
vnode_isTextVNode,
2422
type VNodeJournal,
@@ -147,7 +145,7 @@ function vnode_cloneElementWithNamespace(
147145

148146
// We need to check if the parent is a foreignObject element
149147
// and get a new namespace data.
150-
const vCursorParent = vnode_getParent(vCursor);
148+
const vCursorParent = vCursor.parent;
151149
// For the first vNode parentNode is not parent from vNode tree, but parent from DOM tree
152150
// this is because vNode is not moved yet.
153151
// rootElement is null only for the first vNode
@@ -206,24 +204,24 @@ function vnode_cloneElementWithNamespace(
206204
return rootElement;
207205
}
208206
// Out of children, go to next sibling
209-
const vNextSibling = vnode_getNextSibling(vCursor);
207+
const vNextSibling = vCursor.nextSibling as VNode | null;
210208
if (vNextSibling) {
211209
vCursor = vNextSibling;
212210
continue;
213211
}
214212
// Out of siblings, go to parent
215-
vParent = vnode_getParent(vCursor);
213+
vParent = vCursor.parent;
216214
while (vParent) {
217215
if (vParent === elementVNode) {
218216
// We are back where we started, we are done.
219217
return rootElement;
220218
}
221-
const vNextParentSibling = vnode_getNextSibling(vParent);
219+
const vNextParentSibling = vParent.nextSibling as VNode | null;
222220
if (vNextParentSibling) {
223221
vCursor = vNextParentSibling;
224222
return rootElement;
225223
}
226-
vParent = vnode_getParent(vParent);
224+
vParent = vParent.parent;
227225
}
228226
if (vParent == null) {
229227
// We are done.

packages/qwik/src/core/client/vnode.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -434,24 +434,24 @@ export function vnode_walkVNode(
434434
return;
435435
}
436436
// Out of children, go to next sibling
437-
const vNextSibling = vnode_getNextSibling(vCursor);
437+
const vNextSibling = vCursor.nextSibling as VNode | null;
438438
if (vNextSibling) {
439439
vCursor = vNextSibling;
440440
continue;
441441
}
442442
// Out of siblings, go to parent
443-
vParent = vnode_getParent(vCursor);
443+
vParent = vCursor.parent;
444444
while (vParent) {
445445
if (vParent === vNode) {
446446
// We are back where we started, we are done.
447447
return;
448448
}
449-
const vNextParentSibling = vnode_getNextSibling(vParent);
449+
const vNextParentSibling = vParent.nextSibling as VNode | null;
450450
if (vNextParentSibling) {
451451
vCursor = vNextParentSibling;
452452
break;
453453
}
454-
vParent = vnode_getParent(vParent);
454+
vParent = vParent.parent;
455455
}
456456
if (vParent == null) {
457457
// We are done.
@@ -507,7 +507,7 @@ export function vnode_getDOMChildNodes(
507507
? vnode_getDOMChildNodes(journal, vNode, true, childNodes as (ElementVNode | TextVNode)[])
508508
: vnode_getDOMChildNodes(journal, vNode, false, childNodes as (Element | Text)[]);
509509
}
510-
vNode = vnode_getNextSibling(vNode);
510+
vNode = vNode.nextSibling as VNode | null;
511511
}
512512
return childNodes;
513513
}
@@ -728,7 +728,7 @@ const vnode_getChildWithIdx = (vNode: VNode, childIdx: number): VNode => {
728728
let child = vnode_getFirstChild(vNode);
729729
assertDefined(child, 'Missing child.');
730730
while (child.flags >>> VNodeFlagsIndex.shift !== childIdx) {
731-
child = vnode_getNextSibling(child);
731+
child = child.nextSibling as VNode | null;
732732
assertDefined(child, 'Missing child.');
733733
}
734734
return child;
@@ -744,7 +744,7 @@ export const vnode_getVNodeForChildNode = (
744744
assertDefined(child, 'Missing child.');
745745
while (child && (child instanceof ElementVNode ? child.element !== childElement : true)) {
746746
if (vnode_isVirtualVNode(child)) {
747-
const next = vnode_getNextSibling(child);
747+
const next = child.nextSibling as VNode | null;
748748
const firstChild = vnode_getFirstChild(child);
749749
if (firstChild) {
750750
next && vNodeStack.push(next);
@@ -753,7 +753,7 @@ export const vnode_getVNodeForChildNode = (
753753
child = next || (vNodeStack.length ? vNodeStack.pop()! : null);
754754
}
755755
} else {
756-
const next = vnode_getNextSibling(child);
756+
const next = child.nextSibling as VNode | null;
757757
if (next) {
758758
child = next;
759759
} else {
@@ -1149,7 +1149,7 @@ export const vnode_getDomParentVNode = (
11491149
includeProjection = true
11501150
): ElementVNode | null => {
11511151
while (vnode && !vnode_isElementVNode(vnode)) {
1152-
vnode = vnode.parent || (includeProjection ? vnode.getSlotParent() : null)!;
1152+
vnode = vnode.parent || (includeProjection ? vnode.slotParent : null)!;
11531153
}
11541154
return vnode;
11551155
};
@@ -1160,7 +1160,7 @@ export const vnode_remove = (
11601160
vToRemove: VNode,
11611161
removeDOM: boolean
11621162
) => {
1163-
assertEqual(vParent, vnode_getParent(vToRemove), 'Parent mismatch.');
1163+
assertEqual(vParent, vToRemove.parent, 'Parent mismatch.');
11641164
if (vnode_isTextVNode(vToRemove)) {
11651165
vnode_ensureTextInflated(journal, vToRemove);
11661166
}
@@ -1208,7 +1208,7 @@ export const vnode_queryDomNodes = (
12081208
let child = vnode_getFirstChild(vNode);
12091209
while (child) {
12101210
vnode_queryDomNodes(child, selector, cb);
1211-
child = vnode_getNextSibling(child);
1211+
child = child.nextSibling as VNode | null;
12121212
}
12131213
}
12141214
};
@@ -1666,10 +1666,6 @@ export const vnode_getProps = (vnode: ElementVNode | VirtualVNode): unknown[] =>
16661666
return vnode.props;
16671667
};
16681668

1669-
export const vnode_getParent = (vnode: VNode): VNode | null => {
1670-
return vnode.parent;
1671-
};
1672-
16731669
export const vnode_isDescendantOf = (vnode: VNode, ancestor: VNode): boolean => {
16741670
let parent: VNode | null = vnode_getParentOrProjectionParent(vnode);
16751671
while (parent) {
@@ -1781,7 +1777,7 @@ export function vnode_toString(
17811777
}
17821778
strings.push('</' + tag + '>');
17831779
}
1784-
vnode = (siblings && vnode_getNextSibling(vnode)) || null;
1780+
vnode = (siblings && vnode.nextSibling) || null;
17851781
} while (vnode);
17861782
return strings.join('\n' + offset);
17871783
}
@@ -1988,10 +1984,10 @@ export const vnode_getProjectionParentComponent = (
19881984
// We found a projection, so we need to go up one more level.
19891985
projectionDepth++;
19901986
}
1991-
vHost = vProjectionParent || vnode_getParent(vHost)!;
1987+
vHost = vProjectionParent || vHost.parent!;
19921988
}
19931989
if (projectionDepth > 0) {
1994-
vHost = vnode_getParent(vHost)!;
1990+
vHost = vHost.parent!;
19951991
}
19961992
}
19971993
return vHost as VirtualVNode | null;

0 commit comments

Comments
 (0)