(query: Query, scheduler?: SchedulerLike)
| 11 | * order of occurence. |
| 12 | */ |
| 13 | export function docChanges<T>(query: Query, scheduler?: SchedulerLike): Observable<DocumentChangeAction<T>[]> { |
| 14 | return fromCollectionRef(query, scheduler) |
| 15 | .pipe( |
| 16 | startWith<Action<QuerySnapshot<firebase.firestore.DocumentData>>, undefined>(undefined), |
| 17 | pairwise(), |
| 18 | map((actionTuple: ActionTupe) => { |
| 19 | const [priorAction, action] = actionTuple; |
| 20 | const docChanges = action.payload.docChanges(); |
| 21 | const actions = docChanges.map(change => ({ type: change.type, payload: change })); |
| 22 | // the metadata has changed from the prior emission |
| 23 | if (priorAction && JSON.stringify(priorAction.payload.metadata) !== JSON.stringify(action.payload.metadata)) { |
| 24 | // go through all the docs in payload and figure out which ones changed |
| 25 | action.payload.docs.forEach((currentDoc, currentIndex) => { |
| 26 | const docChange = docChanges.find(d => d.doc.ref.isEqual(currentDoc.ref)); |
| 27 | const priorDoc = priorAction?.payload.docs.find(d => d.ref.isEqual(currentDoc.ref)); |
| 28 | if (docChange && JSON.stringify(docChange.doc.metadata) === JSON.stringify(currentDoc.metadata) || |
| 29 | !docChange && priorDoc && JSON.stringify(priorDoc.metadata) === JSON.stringify(currentDoc.metadata)) { |
| 30 | // document doesn't appear to have changed, don't log another action |
| 31 | } else { |
| 32 | // since the actions are processed in order just push onto the array |
| 33 | actions.push({ |
| 34 | type: 'modified', |
| 35 | payload: { |
| 36 | oldIndex: currentIndex, |
| 37 | newIndex: currentIndex, |
| 38 | type: 'modified', |
| 39 | doc: currentDoc |
| 40 | } |
| 41 | }); |
| 42 | } |
| 43 | }); |
| 44 | } |
| 45 | return actions as DocumentChangeAction<T>[]; |
| 46 | }), |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Return a stream of document changes on a query. These results are in sort order. |
no test coverage detected