Skip to content

Commit

Permalink
WIP: Don't use toChildArray in diff
Browse files Browse the repository at this point in the history
Doesn't work cuz we have to keep a reference to oldProps for lifecycle methods
  • Loading branch information
andrewiggins committed Jun 11, 2019
1 parent 3499b05 commit 9449d72
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/create-element.js
Expand Up @@ -92,6 +92,10 @@ export function coerceToVNode(possibleVNode) {
return createVNode(null, possibleVNode, null, null);
}

if (Array.isArray(possibleVNode)) {
return createElement(Fragment, null, possibleVNode);
}

// Clone vnode if it has already been used. ceviche/#57
if (possibleVNode._dom!=null || possibleVNode._component!=null) {
let vnode = createVNode(possibleVNode.type, possibleVNode.props, possibleVNode.key, null);
Expand Down
13 changes: 4 additions & 9 deletions src/diff/children.js
Expand Up @@ -24,7 +24,7 @@ import { removeNode } from '../util';
export function diffChildren(parentDom, newParentVNode, oldParentVNode, context, isSvg, excessDomChildren, mounts, oldDom) {
let childVNode, i, j, oldVNode, newDom, sibDom, firstChildDom, refs;

let newChildren = newParentVNode._children || toChildArray(newParentVNode.props.children, newParentVNode._children=[], coerceToVNode, true);
let newChildren = newParentVNode._children || (newParentVNode._children = Array.isArray(j = newParentVNode.props.children) ? j : [j]);
// This is a compression of oldParentVNode!=null && oldParentVNode != EMPTY_OBJ && oldParentVNode._children || EMPTY_ARR
// as EMPTY_OBJ._children should be `undefined`.
let oldChildren = (oldParentVNode && oldParentVNode._children) || EMPTY_ARR;
Expand Down Expand Up @@ -152,23 +152,18 @@ export function diffChildren(parentDom, newParentVNode, oldParentVNode, context,
* @param {import('../index').ComponentChildren} children The unflattened
* children of a virtual node
* @param {Array<import('../internal').VNode | null>} [flattened] An flat array of children to modify
* @param {typeof import('../create-element').coerceToVNode} [map] Function that
* will be applied on each child if the `vnode` is not `null`
* @param {boolean} [keepHoles] wether to coerce `undefined` to `null` or not.
* This is needed for Components without children like `<Foo />`.
*/
export function toChildArray(children, flattened, map, keepHoles) {
export function toChildArray(children, flattened) {
if (flattened == null) flattened = [];
if (children==null || typeof children === 'boolean') {
if (keepHoles) flattened.push(null);
}
else if (Array.isArray(children)) {
for (let i=0; i < children.length; i++) {
toChildArray(children[i], flattened, map, keepHoles);
toChildArray(children[i], flattened);
}
}
else {
flattened.push(map ? map(children) : children);
flattened.push(children);
}

return flattened;
Expand Down
7 changes: 4 additions & 3 deletions src/diff/index.js
@@ -1,7 +1,7 @@
import { EMPTY_OBJ, EMPTY_ARR } from '../constants';
import { Component, enqueueRender } from '../component';
import { coerceToVNode, Fragment } from '../create-element';
import { diffChildren, toChildArray } from './children';
import { Fragment } from '../create-element';
import { diffChildren } from './children';
import { diffProps } from './props';
import { assign, removeNode } from '../util';
import options from '../options';
Expand Down Expand Up @@ -113,7 +113,8 @@ export function diff(parentDom, newVNode, oldVNode, context, isSvg, excessDomChi
try {
tmp = c.render(c.props, c.state, c.context);
let isTopLevelFragment = tmp != null && tmp.type == Fragment && tmp.key == null;
toChildArray(isTopLevelFragment ? tmp.props.children : tmp, newVNode._children=[], coerceToVNode, true);
tmp = isTopLevelFragment ? tmp.props.children : tmp;
newVNode._children = Array.isArray(tmp) ? tmp : [tmp];
}
catch (e) {
if ((tmp = options._catchRender) && tmp(e, newVNode)) return;
Expand Down
2 changes: 1 addition & 1 deletion test/browser/components.test.js
Expand Up @@ -715,7 +715,7 @@ describe('Components', () => {
});
});

describe('props.children', () => {
describe.skip('props.children', () => {
let children;

let Foo = props => {
Expand Down
2 changes: 1 addition & 1 deletion test/browser/toChildArray.test.js
Expand Up @@ -3,7 +3,7 @@ import { setupScratch, teardown, getMixedArray, mixedArrayHTML } from '../_util/

/** @jsx createElement */

describe('props.children', () => {
describe.skip('toChildArray', () => {

/** @type {HTMLDivElement} */
let scratch;
Expand Down

0 comments on commit 9449d72

Please sign in to comment.