Skip to content

Commit 97b26e3

Browse files
jymdmanjamesdaniels
authored andcommitted
fix(firestore): Update to rxjs pipeable operators (#1623)
1 parent 014be21 commit 97b26e3

File tree

9 files changed

+62
-67
lines changed

9 files changed

+62
-67
lines changed

src/firestore/collection/changes.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { fromCollectionRef } from '../observable/fromRef';
22
import { Query, DocumentChangeType, DocumentChange } from '@firebase/firestore-types';
3-
import { Observable } from 'rxjs/Observable';
4-
import 'rxjs/add/operator/map';
5-
import 'rxjs/add/operator/filter';
6-
import 'rxjs/add/operator/scan';
3+
import { Observable } from 'rxjs';
4+
import { map, filter, scan } from 'rxjs/operators';
75

86
import { DocumentChangeAction, Action } from '../interfaces';
97

@@ -14,9 +12,10 @@ import { DocumentChangeAction, Action } from '../interfaces';
1412
*/
1513
export function docChanges(query: Query): Observable<DocumentChangeAction[]> {
1614
return fromCollectionRef(query)
17-
.map(action =>
18-
action.payload.docChanges
19-
.map(change => ({ type: change.type, payload: change })));
15+
.pipe(
16+
map(action =>
17+
action.payload.docChanges
18+
.map(change => ({ type: change.type, payload: change }))));
2019
}
2120

2221
/**
@@ -25,9 +24,10 @@ export function docChanges(query: Query): Observable<DocumentChangeAction[]> {
2524
*/
2625
export function sortedChanges(query: Query, events: DocumentChangeType[]): Observable<DocumentChangeAction[]> {
2726
return fromCollectionRef(query)
28-
.map(changes => changes.payload.docChanges)
29-
.scan((current, changes) => combineChanges(current, changes, events), [])
30-
.map(changes => changes.map(c => ({ type: c.type, payload: c })));
27+
.pipe(
28+
map(changes => changes.payload.docChanges),
29+
scan((current, changes) => combineChanges(current, changes, events), []),
30+
map(changes => changes.map(c => ({ type: c.type, payload: c }))));
3131
}
3232

3333
/**

src/firestore/collection/collection.spec.ts

+18-21
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ import { AngularFirestoreCollection } from './collection';
66
import { QueryFn } from '../interfaces';
77

88
import { FirebaseApp as FBApp } from '@firebase/app-types';
9-
import { Observable } from 'rxjs/Observable';
10-
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
11-
import { of } from 'rxjs/observable/of';
12-
import { Subscription } from 'rxjs/Subscription';
13-
import 'rxjs/add/operator/skip';
9+
import { Observable, BehaviorSubject, Subscription } from 'rxjs';
10+
import { skip, take, switchMap } from 'rxjs/operators';
1411

1512
import { TestBed, inject } from '@angular/core/testing';
1613
import { COMMON_CONFIG } from '../test-config';
@@ -80,7 +77,7 @@ describe('AngularFirestoreCollection', () => {
8077
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
8178
const changes = stocks.valueChanges();
8279
const sub = changes.subscribe(() => {}).add(
83-
changes.take(1).subscribe(data => {
80+
changes.pipe(take(1)).subscribe(data => {
8481
expect(data.length).toEqual(ITEMS);
8582
sub.unsubscribe();
8683
})
@@ -93,8 +90,8 @@ describe('AngularFirestoreCollection', () => {
9390
const ITEMS = 4;
9491
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
9592
const changes = stocks.valueChanges();
96-
changes.take(1).subscribe(() => {}).add(() => {
97-
const sub = changes.take(1).subscribe(data => {
93+
changes.pipe(take(1)).subscribe(() => {}).add(() => {
94+
const sub = changes.pipe(take(1)).subscribe(data => {
9895
expect(data.length).toEqual(ITEMS);
9996
}).add(() => {
10097
deleteThemAll(names, ref).then(done).catch(done.fail);
@@ -110,9 +107,9 @@ describe('AngularFirestoreCollection', () => {
110107
const randomCollectionName = randomName(afs.firestore);
111108
const ref = afs.firestore.collection(`${randomCollectionName}`);
112109
let names = await createRandomStocks(afs.firestore, ref, ITEMS);
113-
const sub = pricefilter$.switchMap(price => {
110+
const sub = pricefilter$.pipe(switchMap(price => {
114111
return afs.collection(randomCollectionName, ref => price ? ref.where('price', '==', price) : ref).valueChanges()
115-
}).subscribe(data => {
112+
})).subscribe(data => {
116113
count = count + 1;
117114
// the first time should all be 'added'
118115
if(count === 1) {
@@ -161,7 +158,7 @@ describe('AngularFirestoreCollection', () => {
161158
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
162159
const changes = stocks.snapshotChanges();
163160
const sub = changes.subscribe(() => {}).add(
164-
changes.take(1).subscribe(data => {
161+
changes.pipe(take(1)).subscribe(data => {
165162
expect(data.length).toEqual(ITEMS);
166163
sub.unsubscribe();
167164
})
@@ -174,8 +171,8 @@ describe('AngularFirestoreCollection', () => {
174171
const ITEMS = 4;
175172
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
176173
const changes = stocks.snapshotChanges();
177-
changes.take(1).subscribe(() => {}).add(() => {
178-
const sub = changes.take(1).subscribe(data => {
174+
changes.pipe(take(1)).subscribe(() => {}).add(() => {
175+
const sub = changes.pipe(take(1)).subscribe(data => {
179176
expect(data.length).toEqual(ITEMS);
180177
}).add(() => {
181178
deleteThemAll(names, ref).then(done).catch(done.fail);
@@ -214,7 +211,7 @@ describe('AngularFirestoreCollection', () => {
214211
const ITEMS = 10;
215212
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
216213

217-
const sub = stocks.snapshotChanges(['modified']).skip(1).subscribe(data => {
214+
const sub = stocks.snapshotChanges(['modified']).pipe(skip(1)).subscribe(data => {
218215
sub.unsubscribe();
219216
const change = data.filter(x => x.payload.doc.id === names[0])[0];
220217
expect(data.length).toEqual(1);
@@ -231,7 +228,7 @@ describe('AngularFirestoreCollection', () => {
231228
let { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
232229
const nextId = ref.doc('a').id;
233230

234-
const sub = stocks.snapshotChanges(['added']).skip(1).subscribe(data => {
231+
const sub = stocks.snapshotChanges(['added']).pipe(skip(1)).subscribe(data => {
235232
sub.unsubscribe();
236233
const change = data.filter(x => x.payload.doc.id === nextId)[0];
237234
expect(data.length).toEqual(ITEMS + 1);
@@ -252,7 +249,7 @@ describe('AngularFirestoreCollection', () => {
252249
const nextId = ref.doc('a').id;
253250
let count = 0;
254251

255-
const sub = stocks.snapshotChanges(['added', 'modified']).skip(1).take(2).subscribe(data => {
252+
const sub = stocks.snapshotChanges(['added', 'modified']).pipe(skip(1),take(2)).subscribe(data => {
256253
count += 1;
257254
if (count == 1) {
258255
const change = data.filter(x => x.payload.doc.id === nextId)[0];
@@ -279,7 +276,7 @@ describe('AngularFirestoreCollection', () => {
279276
const ITEMS = 10;
280277
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
281278

282-
const sub = stocks.snapshotChanges(['added', 'removed']).skip(1).subscribe(data => {
279+
const sub = stocks.snapshotChanges(['added', 'removed']).pipe(skip(1)).subscribe(data => {
283280
sub.unsubscribe();
284281
const change = data.filter(x => x.payload.doc.id === names[0]);
285282
expect(data.length).toEqual(ITEMS - 1);
@@ -339,7 +336,7 @@ describe('AngularFirestoreCollection', () => {
339336
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
340337
const changes = stocks.stateChanges();
341338
const sub = changes.subscribe(() => {}).add(
342-
changes.take(1).subscribe(data => {
339+
changes.pipe(take(1)).subscribe(data => {
343340
expect(data.length).toEqual(ITEMS);
344341
sub.unsubscribe();
345342
})
@@ -352,8 +349,8 @@ describe('AngularFirestoreCollection', () => {
352349
const ITEMS = 4;
353350
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
354351
const changes = stocks.stateChanges();
355-
changes.take(1).subscribe(() => {}).add(() => {
356-
const sub = changes.take(1).subscribe(data => {
352+
changes.pipe(take(1)).subscribe(() => {}).add(() => {
353+
const sub = changes.pipe(take(1)).subscribe(data => {
357354
expect(data.length).toEqual(ITEMS);
358355
}).add(() => {
359356
deleteThemAll(names, ref).then(done).catch(done.fail);
@@ -383,7 +380,7 @@ describe('AngularFirestoreCollection', () => {
383380
let count = 0;
384381
let { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
385382

386-
const sub = stocks.stateChanges(['added']).skip(1).subscribe(data => {
383+
const sub = stocks.stateChanges(['added']).pipe(skip(1)).subscribe(data => {
387384
sub.unsubscribe();
388385
expect(data.length).toEqual(1);
389386
expect(data[0].payload.doc.data().price).toEqual(2);

src/firestore/collection/collection.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { DocumentChangeType, CollectionReference, Query, DocumentReference } from '@firebase/firestore-types';
2-
import { Observable } from 'rxjs/Observable';
3-
import { Subscriber } from 'rxjs/Subscriber';
2+
import { Observable, Subscriber } from 'rxjs';
43
import { fromCollectionRef } from '../observable/fromRef';
5-
import 'rxjs/add/operator/map';
6-
import 'rxjs/add/operator/filter';
4+
import { map, filter } from 'rxjs/operators';
75

86
import { Injectable } from '@angular/core';
97

@@ -12,8 +10,6 @@ import { docChanges, sortedChanges } from './changes';
1210
import { AngularFirestoreDocument } from '../document/document';
1311
import { AngularFirestore } from '../firestore';
1412

15-
import 'rxjs/add/observable/of';
16-
1713
export function validateEventsArray(events?: DocumentChangeType[]) {
1814
if(!events || events!.length === 0) {
1915
events = ['added', 'removed', 'modified'];
@@ -79,8 +75,10 @@ export class AngularFirestoreCollection<T> {
7975
docChanges(this.query)
8076
)
8177
)
82-
.map(actions => actions.filter(change => events.indexOf(change.type) > -1))
83-
.filter(changes => changes.length > 0);
78+
.pipe(
79+
map(actions => actions.filter(change => events.indexOf(change.type) > -1)),
80+
filter(changes => changes.length > 0)
81+
);
8482
}
8583

8684
/**
@@ -111,7 +109,9 @@ export class AngularFirestoreCollection<T> {
111109
const fromCollectionRef$ = fromCollectionRef(this.query);
112110
const scheduled$ = this.afs.scheduler.runOutsideAngular(fromCollectionRef$);
113111
return this.afs.scheduler.keepUnstableUntilFirst(scheduled$)
114-
.map(actions => actions.payload.docs.map(a => a.data()) as T[]);
112+
.pipe(
113+
map(actions => actions.payload.docs.map(a => a.data()) as T[])
114+
);
115115
}
116116

117117
/**

src/firestore/document/document.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { AngularFirestoreModule } from '../firestore.module';
44
import { AngularFirestoreDocument } from '../document/document';
55

66
import { FirebaseApp as FBApp } from '@firebase/app-types';
7-
import { Observable } from 'rxjs/Observable';
8-
import { Subscription } from 'rxjs/Subscription';
7+
import { Observable, Subscription } from 'rxjs';
8+
import { take } from 'rxjs/operators';
99

1010
import { TestBed, inject } from '@angular/core/testing';
1111
import { COMMON_CONFIG } from '../test-config';
@@ -57,7 +57,7 @@ describe('AngularFirestoreDocument', () => {
5757
const stock = new AngularFirestoreDocument<Stock>(ref, afs);
5858
await stock.set(FAKE_STOCK_DATA);
5959
const obs$ = stock.valueChanges();
60-
obs$.take(1).subscribe(async (data: Stock) => {
60+
obs$.pipe(take(1)).subscribe(async (data: Stock) => {
6161
expect(JSON.stringify(data)).toBe(JSON.stringify(FAKE_STOCK_DATA));
6262
stock.delete().then(done).catch(done.fail);
6363
});

src/firestore/document/document.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { DocumentReference, SetOptions, DocumentSnapshot } from '@firebase/firestore-types';
2-
import { Observable } from 'rxjs/Observable';
3-
import { Subscriber } from 'rxjs/Subscriber';
2+
import { Observable, Subscriber } from 'rxjs';
43
import { QueryFn, AssociatedReference, Action } from '../interfaces';
54
import { fromDocRef } from '../observable/fromRef';
6-
import 'rxjs/add/operator/map';
5+
import { map } from 'rxjs/operators';
76

87
import { Injectable } from '@angular/core';
98

@@ -90,8 +89,10 @@ export class AngularFirestoreDocument<T> {
9089
* Listen to unwrapped snapshot updates from the document.
9190
*/
9291
valueChanges(): Observable<T|null> {
93-
return this.snapshotChanges().map(action => {
94-
return action.payload.exists ? action.payload.data() as T : null;
95-
});
92+
return this.snapshotChanges().pipe(
93+
map(action => {
94+
return action.payload.exists ? action.payload.data() as T : null;
95+
})
96+
);
9697
}
9798
}

src/firestore/firestore.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { AngularFirestoreModule } from './firestore.module';
44
import { AngularFirestoreDocument } from './document/document';
55
import { AngularFirestoreCollection } from './collection/collection';
66

7-
import { Observable } from 'rxjs/Observable';
8-
import { Subscription } from 'rxjs/Subscription';
7+
import { Observable, Subscription } from 'rxjs';
98

109
import { TestBed, inject } from '@angular/core/testing';
1110
import { COMMON_CONFIG } from './test-config';

src/firestore/firestore.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { InjectionToken, NgZone, PLATFORM_ID } from '@angular/core';
22
import { FirebaseFirestore, CollectionReference, DocumentReference } from '@firebase/firestore-types';
33

4-
import { Observable } from 'rxjs/Observable';
5-
import { Subscriber } from 'rxjs/Subscriber';
6-
import { from } from 'rxjs/observable/from';
4+
import { Observable, Subscriber } from 'rxjs';
5+
import { map, catchError } from 'rxjs/operators';
76
import { of } from 'rxjs/observable/of';
8-
import 'rxjs/add/operator/map';
9-
import 'rxjs/add/operator/catch';
10-
7+
import { from } from 'rxjs/observable/from';
118
import { FirebaseOptions } from '@firebase/app-types';
129
import { Injectable, Inject, Optional } from '@angular/core';
1310

@@ -125,7 +122,9 @@ export class AngularFirestore {
125122
shouldEnablePersistence ? from(this.firestore.enablePersistence().then(() => true, () => false))
126123
: of(false)
127124
)
128-
.catch(() => of(false)); // https://github.com/firebase/firebase-js-sdk/issues/608
125+
.pipe(
126+
catchError(() => of(false))
127+
); // https://github.com/firebase/firebase-js-sdk/issues/608
129128
}
130129

131130
/**

src/firestore/interfaces.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Subscriber } from 'rxjs/Subscriber';
1+
import { Subscriber } from 'rxjs';
22
import { DocumentChangeType, DocumentChange, CollectionReference, Query } from '@firebase/firestore-types';
33

44
export interface DocumentChangeAction {

src/firestore/observable/fromRef.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { DocumentReference, Query, QuerySnapshot, DocumentSnapshot } from '@firebase/firestore-types';
2-
import { Observable } from 'rxjs/Observable';
3-
import { Subscriber } from 'rxjs/Subscriber';
2+
import { Observable, Subscriber } from 'rxjs';
43
import { Action, Reference } from '../interfaces';
5-
6-
import 'rxjs/add/operator/map';
7-
import 'rxjs/add/operator/share';
4+
import { map, share } from 'rxjs/operators';
85

96
function _fromRef<T, R>(ref: Reference<T>): Observable<R> {
107
return new Observable(subscriber => {
@@ -14,14 +11,16 @@ function _fromRef<T, R>(ref: Reference<T>): Observable<R> {
1411
}
1512

1613
export function fromRef<R>(ref: DocumentReference | Query) {
17-
return _fromRef<typeof ref, R>(ref).share();
14+
return _fromRef<typeof ref, R>(ref).pipe(share());
1815
}
1916

2017
export function fromDocRef(ref: DocumentReference): Observable<Action<DocumentSnapshot>>{
2118
return fromRef<DocumentSnapshot>(ref)
22-
.map(payload => ({ payload, type: 'value' }));
19+
.pipe(
20+
map(payload => ({ payload, type: 'value' }))
21+
);
2322
}
2423

2524
export function fromCollectionRef(ref: Query): Observable<Action<QuerySnapshot>> {
26-
return fromRef<QuerySnapshot>(ref).map(payload => ({ payload, type: 'query' }))
25+
return fromRef<QuerySnapshot>(ref).pipe(map(payload => ({ payload, type: 'query' })));
2726
}

0 commit comments

Comments
 (0)