MCPcopy
hub / github.com/angular/angularfire / docChanges

Function docChanges

src/compat/firestore/collection/changes.ts:13–48  ·  view source on GitHub ↗
(query: Query, scheduler?: SchedulerLike)

Source from the content-addressed store, hash-verified

11 * order of occurence.
12 */
13export 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.

Callers 3

stateChangesMethod · 0.90
stateChangesMethod · 0.90
sortedChangesFunction · 0.85

Calls 3

fromCollectionRefFunction · 0.90
pushMethod · 0.80
forEachMethod · 0.65

Tested by

no test coverage detected