-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathfromRef.ts
26 lines (22 loc) · 947 Bytes
/
fromRef.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { DocumentReference, Query } from '@firebase/firestore-types';
import { Observable, Subscriber } from 'rxjs';
import { Action, Reference, DocumentSnapshot, QuerySnapshot } from '../interfaces';
import { map, share } from 'rxjs/operators';
function _fromRef<T, R>(ref: Reference<T>): Observable<R> {
return new Observable(subscriber => {
const unsubscribe = ref.onSnapshot(subscriber);
return { unsubscribe };
});
}
export function fromRef<R>(ref: DocumentReference | Query) {
return _fromRef<typeof ref, R>(ref).pipe(share());
}
export function fromDocRef<T>(ref: DocumentReference): Observable<Action<DocumentSnapshot<T>>>{
return fromRef<DocumentSnapshot<T>>(ref)
.pipe(
map(payload => ({ payload, type: 'value' }))
);
}
export function fromCollectionRef<T>(ref: Query): Observable<Action<QuerySnapshot<T>>> {
return fromRef<QuerySnapshot<T>>(ref).pipe(map(payload => ({ payload, type: 'query' })));
}