(ref: DatabaseQuery,
event: ListenEvent,
listenType = 'on',
scheduler: SchedulerLike = asyncScheduler
)
| 15 | * @param scheduler - Rxjs scheduler |
| 16 | */ |
| 17 | export function fromRef<T>(ref: DatabaseQuery, |
| 18 | event: ListenEvent, |
| 19 | listenType = 'on', |
| 20 | scheduler: SchedulerLike = asyncScheduler |
| 21 | ): Observable<AngularFireAction<DatabaseSnapshot<T>>> { |
| 22 | return new Observable<SnapshotPrevKey<T>>(subscriber => { |
| 23 | let fn: any = null; |
| 24 | fn = ref[listenType](event, (snapshot, prevKey) => { |
| 25 | scheduler.schedule(() => { |
| 26 | subscriber.next({ snapshot, prevKey }); |
| 27 | }); |
| 28 | if (listenType === 'once') { |
| 29 | scheduler.schedule(() => subscriber.complete()); |
| 30 | } |
| 31 | }, err => { |
| 32 | scheduler.schedule(() => subscriber.error(err)); |
| 33 | }); |
| 34 | |
| 35 | if (listenType === 'on') { |
| 36 | return { |
| 37 | unsubscribe() { |
| 38 | if (fn != null) { |
| 39 | ref.off(event, fn); |
| 40 | } |
| 41 | } |
| 42 | }; |
| 43 | } else { |
| 44 | return { |
| 45 | // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 46 | unsubscribe() { |
| 47 | } |
| 48 | }; |
| 49 | } |
| 50 | }).pipe( |
| 51 | map(payload => { |
| 52 | const { snapshot, prevKey } = payload; |
| 53 | let key: string | null = null; |
| 54 | if (snapshot.exists()) { |
| 55 | key = snapshot.key; |
| 56 | } |
| 57 | return { type: event, payload: snapshot, prevKey, key }; |
| 58 | }), |
| 59 | share() |
| 60 | ); |
| 61 | } |
no test coverage detected