Skip to content

Commit

Permalink
Fix syncTrees from generating too many patches
Browse files Browse the repository at this point in the history
This is a reversion of the change found in
e282dbc and a test to ensure
the prevention of future regressions.
  • Loading branch information
mAAdhaTTah committed Aug 12, 2017
1 parent 65c2b99 commit 744c283
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/diffhtml/lib/tree/sync.js
Expand Up @@ -71,7 +71,7 @@ export default function syncTree(oldTree, newTree, patches, parentTree, specialC
// then splice it into the parent (if it exists) and run a sync.
if (retVal && retVal !== newTree) {
newTree.childNodes = [].concat(retVal);
syncTree(null, retVal, patches, newTree);
syncTree(oldTree !== empty ? oldTree : null, retVal, patches, newTree);
newTree = retVal;
}
});
Expand Down
28 changes: 28 additions & 0 deletions packages/diffhtml/test/tree.js
Expand Up @@ -8,6 +8,8 @@ import {
} from 'assert';
import createTree from '../lib/tree/create';
import syncTree from '../lib/tree/sync';
import { MiddlewareCache } from '../lib/util/caches';
import parse from '../lib/util/parse';
import validateMemory from './util/validateMemory';

describe('Tree', function() {
Expand Down Expand Up @@ -714,6 +716,32 @@ describe('Tree', function() {
});
});

it('will not generate patches when returning old element', () => {
const hook = (oldTree, newTree) =>
oldTree && oldTree.attributes && oldTree.attributes.class === 'text' ?
oldTree :
newTree;
MiddlewareCache.SyncTreeHookCache.add(hook);

const oldTree = parse(`
<div class="parent"><div class="child"><span class="text">Hello world!</span></div></div>
`).childNodes[0];
const newTree = parse(`
<div class="parent"><div class="child"><span class="image">Goodbye world!</span></div></div>
`).childNodes[0];

const patches = syncTree(oldTree, newTree);

deepEqual(patches, {
TREE_OPS: [],
NODE_VALUE: [],
SET_ATTRIBUTE: [],
REMOVE_ATTRIBUTE: [],
});

MiddlewareCache.SyncTreeHookCache.delete(hook);
});

describe.skip('Attribute & Property Differences', () => {
it('will convert className to class', () => {
const oldTree = createTree('div');
Expand Down

0 comments on commit 744c283

Please sign in to comment.