Skip to content

Commit

Permalink
fix(repeat): array sync issue when inserting during leave animation
Browse files Browse the repository at this point in the history
  • Loading branch information
insidewhy committed Dec 18, 2015
1 parent 74caf7f commit 68f7870
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/array-repeat-strategy.js
Expand Up @@ -103,6 +103,45 @@ export class ArrayRepeatStrategy {
}

_standardProcessInstanceMutated(repeat, array, splices) {
if (repeat.__queuedSplices) {
repeat.__queuedSplices.push({ repeat, array, splices });
return;
}

let maybePromise = this._runSplices(repeat, array, splices);
if (maybePromise instanceof Promise) {
let queuedSplices = repeat.__queuedSplices = [];

let runQueuedSplices = () => {
if (! queuedSplices.length) {
delete repeat.__queuedSplices;
return;
}

// TODO: coalesce/normalise queuedSplices here

// TODO: if the splices are coalesced then _runSplices only has to be
// called once here, will be faster and simpler.
let nextSplice = queuedSplices.shift();
let nextPromise = this._runSplices(nextSplice.repeat, nextSplice.array, nextSplice.splices) || Promise.resolve();
nextPromise.then(runQueuedSplices);
}

maybePromise.then(runQueuedSplices);
}
}

/**
* Run a normalised set of splices against the viewSlot children.
* @param repeat The repeat instance.
* @param array The modified array.
* @param splices Records of array changes.
* @return {Promise|undefined} A promise if animations have to be run.
* @pre The splices must be normalised so as:
* * Any item added may not be later removed.
* * Removals are ordered by asending index
*/
_runSplices(repeat, array, splices) {
let removeDelta = 0;
let viewSlot = repeat.viewSlot;
let rmPromises = [];
Expand All @@ -112,6 +151,7 @@ export class ArrayRepeatStrategy {
let removed = splice.removed;

for (let j = 0, jj = removed.length; j < jj; ++j) {
// the rmPromises.length correction works due to the ordered removal precondition
let viewOrPromise = viewSlot.removeAt(splice.index + removeDelta + rmPromises.length, true);
if (viewOrPromise instanceof Promise) {
rmPromises.push(viewOrPromise);
Expand All @@ -121,7 +161,7 @@ export class ArrayRepeatStrategy {
}

if (rmPromises.length > 0) {
Promise.all(rmPromises).then(() => {
return Promise.all(rmPromises).then(() => {
let spliceIndexLow = this._handleAddedSplices(repeat, array, splices);
updateOverrideContexts(repeat.viewSlot.children, spliceIndexLow);
});
Expand Down

0 comments on commit 68f7870

Please sign in to comment.