Skip to content

Commit

Permalink
Merge pull request #349 from bigopon/fix-repeat-mutation
Browse files Browse the repository at this point in the history
fix(Repeat): ignore mutation while processing instance changed
  • Loading branch information
EisenbergEffect committed Jun 4, 2018
2 parents 70bbb38 + a372db5 commit b04e3df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/repeat.js
Expand Up @@ -135,7 +135,11 @@ export class Repeat extends AbstractRepeater {
if (!this.isOneTime && !this._observeInnerCollection()) {
this._observeCollection();
}
this.ignoreMutation = true;
this.strategy.instanceChanged(this, items);
this.observerLocator.taskQueue.queueMicroTask(() => {
this.ignoreMutation = false;
});
}

_getInnerCollection() {
Expand All @@ -153,6 +157,9 @@ export class Repeat extends AbstractRepeater {
if (!this.collectionObserver) {
return;
}
if (this.ignoreMutation) {
return;
}
this.strategy.instanceMutated(this, collection, changes);
}

Expand Down
23 changes: 23 additions & 0 deletions test/repeat-integration.spec.js
Expand Up @@ -1031,6 +1031,29 @@ describe('Repeat array (pure)', describeArrayTests.bind(this, true));

describe('Repeat array (not pure)', describeArrayTests.bind(this, false));

describe('instancesChanges and instancesMutated together', () => {
it('handles together correctly', () => {
let template = `<template><section if.bind='show'><div repeat.for="item of items">\${item}</div></section></template>`;
let viewModel = {
show: false,
// items: [{ a: 'a' }, { a: 'b' }]
items: null,
};
let controller = createController(template, viewModel, false);
let taskQueue = container.get(TaskQueue);
taskQueue.queueMicroTask(() => {
viewModel.show = true;
viewModel.items = ['a', 'b', 'c', 'd'];
viewModel.items.splice(1, 1, { a: 'bb' });
viewModel.items.push({ a: 'aa' });
});
taskQueue.flushMicroTaskQueue();
taskQueue.queueMicroTask(() => {
console.log(host.children.length, select(controller, 'div').length);
});
});
});

describe('Repeat map [k, v]', () => {
let viewModel, controller;
let obj = {};
Expand Down

0 comments on commit b04e3df

Please sign in to comment.