(combined: DocumentChange<T>[], change: DocumentChange<T>)
| 96 | * and so we have greater control over change detection (by breaking ===) |
| 97 | */ |
| 98 | export function combineChange<T>(combined: DocumentChange<T>[], change: DocumentChange<T>): DocumentChange<T>[] { |
| 99 | switch (change.type) { |
| 100 | case 'added': |
| 101 | if (combined[change.newIndex]?.doc.ref.isEqual(change.doc.ref)) { |
| 102 | // Not sure why the duplicates are getting fired |
| 103 | } else { |
| 104 | return sliceAndSplice(combined, change.newIndex, 0, change); |
| 105 | } |
| 106 | break; |
| 107 | case 'modified': |
| 108 | if (combined[change.oldIndex] == null || combined[change.oldIndex].doc.ref.isEqual(change.doc.ref)) { |
| 109 | // When an item changes position we first remove it |
| 110 | // and then add it's new position |
| 111 | if (change.oldIndex !== change.newIndex) { |
| 112 | const copiedArray = combined.slice(); |
| 113 | copiedArray.splice(change.oldIndex, 1); |
| 114 | copiedArray.splice(change.newIndex, 0, change); |
| 115 | return copiedArray; |
| 116 | } else { |
| 117 | return sliceAndSplice(combined, change.newIndex, 1, change); |
| 118 | } |
| 119 | } |
| 120 | break; |
| 121 | case 'removed': |
| 122 | if (combined[change.oldIndex]?.doc.ref.isEqual(change.doc.ref)) { |
| 123 | return sliceAndSplice(combined, change.oldIndex, 1); |
| 124 | } |
| 125 | break; |
| 126 | } |
| 127 | return combined; |
| 128 | } |
no test coverage detected