Skip to content

Commit eba3fa6

Browse files
authored
Merge branch 'main' into save-bytes-with-ecma-2020
2 parents aa6d6e5 + 9c44db8 commit eba3fa6

File tree

4 files changed

+51
-51
lines changed

4 files changed

+51
-51
lines changed

src/component.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ BaseComponent.prototype.setState = function (update, callback) {
4848

4949
if (update) {
5050
assign(s, update);
51+
} else {
52+
return;
5153
}
5254

53-
// Skip update if updater function returned null
54-
if (update == NULL) return;
55-
5655
if (this._vnode) {
5756
if (callback) {
5857
this._stateCallbacks.push(callback);

src/diff/children.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function diffChildren(
128128
firstChildDom = newDom;
129129
}
130130

131-
let shouldPlace = !!(childVNode._flags & INSERT_VNODE);
131+
let shouldPlace = childVNode._flags & INSERT_VNODE;
132132
if (shouldPlace || oldVNode._children === childVNode._children) {
133133
oldDom = insert(childVNode, oldDom, parentDom, shouldPlace);
134134
} else if (typeof childVNode.type == 'function' && result !== UNDEFINED) {
@@ -251,9 +251,7 @@ function constructNewChildrenArray(
251251
// Here, we define isMounting for the purposes of the skew diffing
252252
// algorithm. Nodes that are unsuspending are considered mounting and we detect
253253
// this by checking if oldVNode._original == null
254-
const isMounting = oldVNode == NULL || oldVNode._original == NULL;
255-
256-
if (isMounting) {
254+
if (oldVNode == NULL || oldVNode._original == NULL) {
257255
if (matchingIndex == -1) {
258256
// When the array of children is growing we need to decrease the skew
259257
// as we are adding a new element to the array.
@@ -340,7 +338,7 @@ function constructNewChildrenArray(
340338
* @param {VNode} parentVNode
341339
* @param {PreactElement} oldDom
342340
* @param {PreactElement} parentDom
343-
* @param {boolean} shouldPlace
341+
* @param {number} shouldPlace
344342
* @returns {PreactElement}
345343
*/
346344
function insert(parentVNode, oldDom, parentDom, shouldPlace) {

src/diff/index.js

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,25 @@ export function diff(
7575
if (newVNode.constructor != UNDEFINED) return NULL;
7676

7777
// If the previous diff bailed out, resume creating/hydrating.
78-
if (oldVNode._flags & MODE_SUSPENDED) {
79-
isHydrating = !!(oldVNode._flags & MODE_HYDRATE);
80-
if (oldVNode._component._excess) {
81-
excessDomChildren = oldVNode._component._excess;
82-
oldDom = newVNode._dom = oldVNode._dom = excessDomChildren[0];
83-
oldVNode._component._excess = null;
84-
}
78+
if (
79+
oldVNode._flags & MODE_SUSPENDED &&
80+
// @ts-expect-error This is 1 or 0 (true or false)
81+
(isHydrating = oldVNode._flags & MODE_HYDRATE)
82+
) {
83+
excessDomChildren = oldVNode._component._excess;
84+
oldDom = excessDomChildren[0];
85+
oldVNode._component._excess = NULL;
8586
}
8687

8788
if ((tmp = options._diff)) tmp(newVNode);
8889

8990
outer: if (typeof newType == 'function') {
9091
try {
91-
let c, isNew, oldProps, oldState, snapshot, clearProcessingException;
92-
let newProps = newVNode.props;
92+
let c,
93+
oldProps,
94+
oldState,
95+
snapshot,
96+
newProps = newVNode.props;
9397
const isClassComponent =
9498
'prototype' in newType && newType.prototype.render;
9599

@@ -108,7 +112,6 @@ export function diff(
108112
c = newVNode._component = oldVNode._component;
109113
if (c._bits & COMPONENT_PENDING_ERROR) {
110114
c._bits |= COMPONENT_PROCESSING_EXCEPTION;
111-
clearProcessingException = true;
112115
}
113116
} else {
114117
// Instantiate the new component
@@ -131,7 +134,6 @@ export function diff(
131134
if (!c.state) c.state = {};
132135
c.context = componentContext;
133136
c._globalContext = globalContext;
134-
isNew = true;
135137
c._bits |= COMPONENT_DIRTY;
136138
c._renderCallbacks = [];
137139
c._stateCallbacks = [];
@@ -158,7 +160,7 @@ export function diff(
158160
c._vnode = newVNode;
159161

160162
// Invoke pre-render lifecycle methods
161-
if (isNew) {
163+
if (!oldVNode._component) {
162164
if (
163165
isClassComponent &&
164166
newType.getDerivedStateFromProps == NULL &&
@@ -268,21 +270,24 @@ export function diff(
268270
globalContext = assign({}, globalContext, c.getChildContext());
269271
}
270272

271-
if (isClassComponent && !isNew && c.getSnapshotBeforeUpdate != NULL) {
273+
if (
274+
isClassComponent &&
275+
oldVNode._component &&
276+
c.getSnapshotBeforeUpdate != NULL
277+
) {
272278
snapshot = c.getSnapshotBeforeUpdate(oldProps, oldState);
273279
}
274280

275281
let isTopLevelFragment =
276282
tmp != NULL && tmp.type === Fragment && tmp.key == NULL;
277-
let renderResult = tmp;
278283

279284
if (isTopLevelFragment) {
280-
renderResult = cloneNode(tmp.props.children);
285+
tmp = cloneNode(tmp.props.children);
281286
}
282287

283288
oldDom = diffChildren(
284289
parentDom,
285-
isArray(renderResult) ? renderResult : [renderResult],
290+
isArray(tmp) ? tmp : [tmp],
286291
newVNode,
287292
oldVNode,
288293
globalContext,
@@ -302,7 +307,7 @@ export function diff(
302307
commitQueue.push(c);
303308
}
304309

305-
if (clearProcessingException) {
310+
if (c._bits & COMPONENT_PENDING_ERROR) {
306311
c._bits &= ~(COMPONENT_PROCESSING_EXCEPTION | COMPONENT_PENDING_ERROR);
307312
}
308313
} catch (e) {
@@ -311,7 +316,7 @@ export function diff(
311316
if (isHydrating || excessDomChildren != NULL) {
312317
if (e.then) {
313318
let commentMarkersToFind = 0,
314-
done = false;
319+
done;
315320

316321
newVNode._flags |= isHydrating
317322
? MODE_HYDRATE | MODE_SUSPENDED
@@ -329,21 +334,22 @@ export function diff(
329334
// We exclude the open and closing marker from
330335
// the future excessDomChildren but any nested one
331336
// needs to be included for future suspensions.
332-
if (child.nodeType == 8 && child.data == '$s') {
333-
if (commentMarkersToFind > 0) {
334-
newVNode._component._excess.push(child);
335-
}
336-
commentMarkersToFind++;
337-
excessDomChildren[i] = NULL;
338-
} else if (child.nodeType == 8 && child.data == '/$s') {
339-
commentMarkersToFind--;
340-
if (commentMarkersToFind > 0) {
341-
newVNode._component._excess.push(child);
337+
if (child.nodeType == 8) {
338+
if (child.data == '$s') {
339+
if (commentMarkersToFind) {
340+
newVNode._component._excess.push(child);
341+
}
342+
commentMarkersToFind++;
343+
} else if (child.data == '/$s') {
344+
commentMarkersToFind--;
345+
if (commentMarkersToFind) {
346+
newVNode._component._excess.push(child);
347+
}
348+
done = commentMarkersToFind == 0;
349+
oldDom = excessDomChildren[i];
342350
}
343-
done = commentMarkersToFind === 0;
344-
oldDom = excessDomChildren[i];
345351
excessDomChildren[i] = NULL;
346-
} else if (commentMarkersToFind > 0) {
352+
} else if (commentMarkersToFind) {
347353
newVNode._component._excess.push(child);
348354
excessDomChildren[i] = NULL;
349355
}
@@ -662,13 +668,11 @@ function diffElementNodes(
662668
export function applyRef(ref, value, vnode) {
663669
try {
664670
if (typeof ref == 'function') {
665-
let hasRefUnmount = typeof ref._unmount == 'function';
666-
if (hasRefUnmount) {
667-
// @ts-ignore TS doesn't like moving narrowing checks into variables
671+
if (typeof ref._unmount == 'function') {
668672
ref._unmount();
669673
}
670674

671-
if (!hasRefUnmount || value != NULL) {
675+
if (typeof ref._unmount != 'function' || value != NULL) {
672676
// Store the cleanup function on the function
673677
// instance object itself to avoid shape
674678
// transitioning vnode
@@ -691,10 +695,8 @@ export function unmount(vnode, parentVNode, skipRemove) {
691695
let r;
692696
if (options.unmount) options.unmount(vnode);
693697

694-
if ((r = vnode.ref)) {
695-
if (!r.current || r.current == vnode._dom) {
696-
applyRef(r, NULL, parentVNode);
697-
}
698+
if ((r = vnode.ref) && (!r.current || r.current == vnode._dom)) {
699+
applyRef(r, NULL, parentVNode);
698700
}
699701

700702
if ((r = vnode._component) != NULL) {

src/render.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function render(vnode, parentDom) {
1818
if (options._root) options._root(vnode, parentDom);
1919

2020
// @ts-expect-error
21-
let isHydrating = !!(vnode && vnode._flags & MODE_HYDRATE);
21+
let isHydrating = vnode && vnode._flags & MODE_HYDRATE;
2222

2323
// To be able to support calling `render()` multiple times on the same
2424
// DOM node, we need to obtain a reference to the previous tree. We do
@@ -27,7 +27,7 @@ export function render(vnode, parentDom) {
2727
// means that we are mounting a new tree for the first time.
2828
let oldVNode = isHydrating ? NULL : parentDom._children;
2929

30-
vnode = parentDom._children = createElement(Fragment, NULL, [vnode]);
30+
parentDom._children = createElement(Fragment, NULL, [vnode]);
3131

3232
// List of effects that need to be called after diffing.
3333
let commitQueue = [],
@@ -37,7 +37,7 @@ export function render(vnode, parentDom) {
3737
parentDom,
3838
// Determine the new vnode tree and store it on the DOM element on
3939
// our custom `_children` property.
40-
vnode,
40+
parentDom._children,
4141
oldVNode || EMPTY_OBJ,
4242
EMPTY_OBJ,
4343
parentDom.namespaceURI,
@@ -48,13 +48,14 @@ export function render(vnode, parentDom) {
4848
: NULL,
4949
commitQueue,
5050
oldVNode ? oldVNode._dom : parentDom.firstChild,
51+
// @ts-expect-error we are doing a bit-wise operation so it's either 0 or true
5152
isHydrating,
5253
refQueue,
5354
parentDom.ownerDocument
5455
);
5556

5657
// Flush all queued effects
57-
commitRoot(commitQueue, vnode, refQueue);
58+
commitRoot(commitQueue, parentDom._children, refQueue);
5859
}
5960

6061
/**

0 commit comments

Comments
 (0)