Skip to content

Commit

Permalink
fix(afs): document's actions should have appropriate types (#2683)
Browse files Browse the repository at this point in the history
Document's snapshots will now come with the `added`, `removed`, and `modified` types as documented, rather than simply `value`.
  • Loading branch information
jamesdaniels authored Nov 24, 2020
1 parent 059547b commit d36544f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/firestore/observable/fromRef.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { asyncScheduler, Observable, SchedulerLike } from 'rxjs';
import { Action, DocumentReference, DocumentSnapshot, Query, QuerySnapshot, Reference } from '../interfaces';
import { map } from 'rxjs/operators';
import { map, pairwise, startWith } from 'rxjs/operators';

function _fromRef<T, R>(ref: Reference<T>, scheduler: SchedulerLike = asyncScheduler): Observable<R> {
return new Observable(subscriber => {
Expand Down Expand Up @@ -28,7 +28,17 @@ export function fromRef<R, T>(ref: DocumentReference<T> | Query<T>, scheduler?:
export function fromDocRef<T>(ref: DocumentReference<T>, scheduler?: SchedulerLike): Observable<Action<DocumentSnapshot<T>>> {
return fromRef<DocumentSnapshot<T>, T>(ref, scheduler)
.pipe(
map(payload => ({ payload, type: 'value' }))
startWith(undefined),
pairwise(),
map(([priorPayload, payload]) => {
if (!payload.exists) {
return { payload, type: 'removed' };
}
if (!priorPayload?.exists) {
return { payload, type: 'added' };
}
return { payload, type: 'modified' };
})
);
}

Expand Down

0 comments on commit d36544f

Please sign in to comment.