Skip to content

Commit

Permalink
Merge pull request #2940 from preactjs/golfing-render
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored Jan 19, 2021
2 parents 7bc661b + 1b7fb44 commit 565928d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
3 changes: 0 additions & 3 deletions src/diff/catch-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export function _catchError(error, vnode) {
/** @type {import('../internal').Component} */
let component, ctor, handled;

const wasHydrating = vnode._hydrating;

for (; (vnode = vnode._parent); ) {
if ((component = vnode._component) && !component._processingException) {
try {
Expand All @@ -28,7 +26,6 @@ export function _catchError(error, vnode) {

// This is an error boundary. Mark it as having bailed out, and whether it was mid-hydration.
if (handled) {
vnode._hydrating = wasHydrating;
return (component._pendingError = component);
}
} catch (e) {
Expand Down
24 changes: 13 additions & 11 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { commitRoot, diff } from './diff/index';
import { createElement, Fragment } from './create-element';
import options from './options';

const IS_HYDRATE = EMPTY_OBJ;

/**
* Render a Preact virtual node into a DOM element
* @param {import('./internal').ComponentChild} vnode The virtual node to render
Expand All @@ -16,10 +14,10 @@ const IS_HYDRATE = EMPTY_OBJ;
export function render(vnode, parentDom, replaceNode) {
if (options._root) options._root(vnode, parentDom);

// We abuse the `replaceNode` parameter in `hydrate()` to signal if we
// are in hydration mode or not by passing `IS_HYDRATE` instead of a
// DOM element.
let isHydrating = replaceNode === IS_HYDRATE;
// We abuse the `replaceNode` parameter in `hydrate()` to signal if we are in
// hydration mode or not by passing the `hydrate` function instead of a DOM
// element..
let isHydrating = typeof replaceNode === 'function';

// To be able to support calling `render()` multiple times on the same
// DOM node, we need to obtain a reference to the previous tree. We do
Expand All @@ -29,27 +27,31 @@ export function render(vnode, parentDom, replaceNode) {
let oldVNode = isHydrating
? null
: (replaceNode && replaceNode._children) || parentDom._children;
vnode = createElement(Fragment, null, [vnode]);

vnode = (
(!isHydrating && replaceNode) ||
parentDom
)._children = createElement(Fragment, null, [vnode]);

// List of effects that need to be called after diffing.
let commitQueue = [];
diff(
parentDom,
// Determine the new vnode tree and store it on the DOM element on
// our custom `_children` property.
((isHydrating ? parentDom : replaceNode || parentDom)._children = vnode),
vnode,
oldVNode || EMPTY_OBJ,
EMPTY_OBJ,
parentDom.ownerSVGElement !== undefined,
replaceNode && !isHydrating
!isHydrating && replaceNode
? [replaceNode]
: oldVNode
? null
: parentDom.childNodes.length
? EMPTY_ARR.slice.call(parentDom.childNodes)
: null,
commitQueue,
replaceNode || EMPTY_OBJ,
(!isHydrating && replaceNode) || EMPTY_OBJ,
isHydrating
);

Expand All @@ -64,5 +66,5 @@ export function render(vnode, parentDom, replaceNode) {
* update
*/
export function hydrate(vnode, parentDom) {
render(vnode, parentDom, IS_HYDRATE);
render(vnode, parentDom, hydrate);
}

0 comments on commit 565928d

Please sign in to comment.