Skip to content

Commit d36544f

Browse files
authored
fix(afs): document's actions should have appropriate types (#2683)
Document's snapshots will now come with the `added`, `removed`, and `modified` types as documented, rather than simply `value`.
1 parent 059547b commit d36544f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/firestore/observable/fromRef.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { asyncScheduler, Observable, SchedulerLike } from 'rxjs';
22
import { Action, DocumentReference, DocumentSnapshot, Query, QuerySnapshot, Reference } from '../interfaces';
3-
import { map } from 'rxjs/operators';
3+
import { map, pairwise, startWith } from 'rxjs/operators';
44

55
function _fromRef<T, R>(ref: Reference<T>, scheduler: SchedulerLike = asyncScheduler): Observable<R> {
66
return new Observable(subscriber => {
@@ -28,7 +28,17 @@ export function fromRef<R, T>(ref: DocumentReference<T> | Query<T>, scheduler?:
2828
export function fromDocRef<T>(ref: DocumentReference<T>, scheduler?: SchedulerLike): Observable<Action<DocumentSnapshot<T>>> {
2929
return fromRef<DocumentSnapshot<T>, T>(ref, scheduler)
3030
.pipe(
31-
map(payload => ({ payload, type: 'value' }))
31+
startWith(undefined),
32+
pairwise(),
33+
map(([priorPayload, payload]) => {
34+
if (!payload.exists) {
35+
return { payload, type: 'removed' };
36+
}
37+
if (!priorPayload?.exists) {
38+
return { payload, type: 'added' };
39+
}
40+
return { payload, type: 'modified' };
41+
})
3242
);
3343
}
3444

0 commit comments

Comments
 (0)