From ac1ab29ad91c5f8c01e73ebbc828c4fb2cf0dfb9 Mon Sep 17 00:00:00 2001 From: James Daniels Date: Tue, 21 Aug 2018 13:09:46 -0700 Subject: [PATCH] fix(afs): Gracefully handle duplicate emissions on modified/deleted --- src/firestore/collection/changes.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/firestore/collection/changes.ts b/src/firestore/collection/changes.ts index dfa415bd0..f02260539 100644 --- a/src/firestore/collection/changes.ts +++ b/src/firestore/collection/changes.ts @@ -61,17 +61,21 @@ export function combineChange(combined: DocumentChange[], change: Document } break; case 'modified': - // When an item changes position we first remove it - // and then add it's new position - if(change.oldIndex !== change.newIndex) { - combined.splice(change.oldIndex, 1); - combined.splice(change.newIndex, 0, change); - } else { - combined.splice(change.newIndex, 1, change); + if (combined[change.oldIndex] == null || combined[change.oldIndex].doc.id == change.doc.id) { + // When an item changes position we first remove it + // and then add it's new position + if(change.oldIndex !== change.newIndex) { + combined.splice(change.oldIndex, 1); + combined.splice(change.newIndex, 0, change); + } else { + combined.splice(change.newIndex, 1, change); + } } break; case 'removed': - combined.splice(change.oldIndex, 1); + if (combined[change.oldIndex] && combined[change.oldIndex].doc.id == change.doc.id) { + combined.splice(change.oldIndex, 1); + } break; } return combined;