Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Only break early by node equality in updateDom for HNodes #743

Merged
merged 2 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,9 +706,6 @@ function createDom(
}

function updateDom(previous: any, dnode: InternalDNode, projectionOptions: ProjectionOptions, parentNode: Element, parentInstance: WidgetBase) {
if (previous === dnode) {
return false;
}
if (isWNode(dnode)) {
const { instance, rendered: previousRendered } = previous;
if (instance && previousRendered) {
Expand All @@ -730,6 +727,9 @@ function updateDom(previous: any, dnode: InternalDNode, projectionOptions: Proje
}
}
else {
if (previous === dnode) {
return false;
}
const domNode = previous.domNode!;
let textUpdated = false;
let updated = false;
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,45 @@ describe('vdom', () => {
assert.strictEqual(thirdTextNodeChild.data, '3');
});

it('Do not break early for the same WNode', () => {
class Foo extends WidgetBase<any> {
render() {
const children = this.children.map((child: any, index: number) => {
child.properties.selected = this.properties.selected === index;
return child;
});

return v('div', children);
}
}

class Bar extends WidgetBase<any> {
render() {
return v('div', [ this.properties.selected ? 'selected' : 'not selected' ]);
}
}

const widget = new Foo();
widget.__setChildren__([
w(Bar, { key: '1' }),
w(Bar, { key: '2' })
]);
widget.__setProperties__({ selected: 0 });
const projection = dom.create(widget.__render__(), widget);
const root = projection.domNode.childNodes[0];
assert.lengthOf(root.childNodes, 2);
let firstTextNode = root.childNodes[0].childNodes[0] as Text;
let secondTextNode = root.childNodes[1].childNodes[0] as Text;
assert.strictEqual(firstTextNode.data, 'selected');
assert.strictEqual(secondTextNode.data, 'not selected');
widget.__setProperties__({ selected: 1 });
projection.update(widget.__render__());
firstTextNode = root.childNodes[0].childNodes[0] as Text;
secondTextNode = root.childNodes[1].childNodes[0] as Text;
assert.strictEqual(firstTextNode.data, 'not selected');
assert.strictEqual(secondTextNode.data, 'selected');
});

it('should throw an error when attempting to merge an array of node', () => {
class Foo extends WidgetBase {
render() {
Expand Down