Skip to content

Commit 185943f

Browse files
jamesdanielsdavideast
authored andcommitted
fix(afs): get options are optional, zone work, and doc get (angular#1849)
1 parent 4f8c7cc commit 185943f

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/core/angularfire2.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,17 @@ export class FirebaseZoneScheduler {
5151
export const runOutsideAngular = (zone: NgZone) => <T>(obs$: Observable<T>): Observable<T> => {
5252
return new Observable<T>(subscriber => {
5353
return zone.runOutsideAngular(() => {
54-
return obs$.subscribe(
55-
value => zone.run(() => subscriber.next(value)),
56-
error => zone.run(() => subscriber.error(error)),
57-
() => zone.run(() => subscriber.complete()),
58-
);
54+
runInZone(zone)(obs$).subscribe(subscriber);
5955
});
6056
});
6157
}
58+
59+
export const runInZone = (zone: NgZone) => <T>(obs$: Observable<T>): Observable<T> => {
60+
return new Observable<T>(subscriber => {
61+
return obs$.subscribe(
62+
value => zone.run(() => subscriber.next(value)),
63+
error => zone.run(() => subscriber.error(error)),
64+
() => zone.run(() => subscriber.complete()),
65+
);
66+
});
67+
}

src/firestore/collection/collection.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { fromCollectionRef } from '../observable/fromRef';
33
import { map, filter, scan } from 'rxjs/operators';
44
import { firestore } from 'firebase/app';
55

6-
import { DocumentChangeType, CollectionReference, Query, DocumentReference, DocumentData, QueryFn, AssociatedReference, DocumentChangeAction, DocumentChange } from '../interfaces';
6+
import { DocumentChangeType, CollectionReference, Query, DocumentReference, DocumentData, DocumentChangeAction } from '../interfaces';
77
import { docChanges, sortedChanges } from './changes';
88
import { AngularFirestoreDocument } from '../document/document';
99
import { AngularFirestore } from '../firestore';
10+
import { runInZone } from 'angularfire2';
1011

1112
export function validateEventsArray(events?: DocumentChangeType[]) {
1213
if(!events || events!.length === 0) {
@@ -116,8 +117,10 @@ export class AngularFirestoreCollection<T=DocumentData> {
116117
* Retrieve the results of the query once.
117118
* @param options
118119
*/
119-
get(options: firestore.GetOptions) {
120-
return from(this.query.get(options));
120+
get(options?: firestore.GetOptions) {
121+
return from(this.query.get(options)).pipe(
122+
runInZone(this.afs.scheduler.zone)
123+
);
121124
}
122125

123126
/**

src/firestore/document/document.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Observable, Subscriber } from 'rxjs';
2-
import { DocumentReference, SetOptions, DocumentData, QueryFn, AssociatedReference, Action, DocumentSnapshot } from '../interfaces';
1+
import { Observable, from } from 'rxjs';
2+
import { DocumentReference, SetOptions, DocumentData, QueryFn, Action, DocumentSnapshot } from '../interfaces';
33
import { fromDocRef } from '../observable/fromRef';
44
import { map } from 'rxjs/operators';
55

6-
import { Injectable } from '@angular/core';
7-
86
import { AngularFirestore, associateQuery } from '../firestore';
97
import { AngularFirestoreCollection } from '../collection/collection';
8+
import { firestore } from 'firebase/app';
9+
import { runInZone } from 'angularfire2';
1010

1111
/**
1212
* AngularFirestoreDocument service
@@ -94,4 +94,14 @@ export class AngularFirestoreDocument<T=DocumentData> {
9494
})
9595
);
9696
}
97+
98+
/**
99+
* Retrieve the document once.
100+
* @param options
101+
*/
102+
get(options?: firestore.GetOptions) {
103+
return from(this.ref.get(options)).pipe(
104+
runInZone(this.afs.scheduler.zone)
105+
);
106+
}
97107
}

0 commit comments

Comments
 (0)