Skip to content

Instantly share code, notes, and snippets.

@okhobb
Created October 18, 2019 18:13
Show Gist options
  • Save okhobb/6873e751f4e9959690aece852e502e3f to your computer and use it in GitHub Desktop.
Save okhobb/6873e751f4e9959690aece852e502e3f to your computer and use it in GitHub Desktop.
private _applyChanges(changes: IterableChanges<T>) {
let singleAddIndex: number|undefined = undefined;
let singleRemoveIndex: number|undefined = undefined;
let hasMultipleMutations: boolean = false;
let singleMutatedItem: T|undefined = undefined;
changes.forEachOperation((item: IterableChangeRecord<any>, adjustedPreviousIndex: number | null, currentIndex: number | null) => {
if (! hasMultipleMutations) {
if (adjustedPreviousIndex === null && currentIndex !== null) {
if (! singleAddIndex) {
singleAddIndex = currentIndex;
singleMutatedItem = item.item;
} else {
hasMultipleMutations = true;
}
}
if (adjustedPreviousIndex !== null && currentIndex === null) {
if (! singleRemoveIndex) {
singleRemoveIndex = adjustedPreviousIndex;
} else {
hasMultipleMutations = true;
}
}
}
});
if (! hasMultipleMutations && singleAddIndex !== undefined && singleRemoveIndex !== undefined && singleAddIndex === singleRemoveIndex) {
const viewRef = <EmbeddedViewRef<PpNgForOfContext<T>>>this._viewContainer.get(singleAddIndex);
viewRef.context.$implicit = singleMutatedItem;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment