Skip to content

Commit eb71edc

Browse files
jamesdanielsdavideast
authored andcommitted
fix(afs): Don't filter empty changes (allow for null set)
1 parent c804664 commit eb71edc

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/firestore/collection/changes.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ export function sortedChanges(query: firebase.firestore.Query, events: firebase.
2828
return fromCollectionRef(query)
2929
.map(changes => changes.payload.docChanges)
3030
.scan((current, changes) => combineChanges(current, changes, events), [])
31-
.map(changes => changes.map(c => ({ type: c.type, payload: c })))
32-
.filter(changes => changes.length > 0);
31+
.map(changes => changes.map(c => ({ type: c.type, payload: c })));
3332
}
3433

3534
/**

src/firestore/collection/collection.spec.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { QueryFn } from '../interfaces';
77

88
import * as firebase from 'firebase/app';
99
import { Observable } from 'rxjs/Observable';
10+
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
1011
import { of } from 'rxjs/observable/of';
1112
import { Subscription } from 'rxjs/Subscription';
1213
import 'rxjs/add/operator/skip';
@@ -73,6 +74,34 @@ describe('AngularFirestoreCollection', () => {
7374
});
7475

7576
});
77+
78+
it('should handle dynamic queries that return empty sets', async (done) => {
79+
const ITEMS = 10;
80+
let count = 0;
81+
let firstIndex = 0;
82+
let pricefilter$ = new BehaviorSubject<number|null>(null);
83+
const randomCollectionName = randomName(afs.firestore);
84+
const ref = afs.firestore.collection(`${randomCollectionName}`);
85+
let names = await createRandomStocks(afs.firestore, ref, ITEMS);
86+
const sub = pricefilter$.switchMap(price => {
87+
return afs.collection(randomCollectionName, ref => price ? ref.where('price', '==', price) : ref).valueChanges()
88+
}).subscribe(data => {
89+
count = count + 1;
90+
// the first time should all be 'added'
91+
if(count === 1) {
92+
expect(data.length).toEqual(ITEMS);
93+
pricefilter$.next(-1);
94+
}
95+
// on the second round, make sure the array is still the same
96+
// length but the updated item is now modified
97+
if(count === 2) {
98+
expect(data.length).toEqual(0);
99+
sub.unsubscribe();
100+
deleteThemAll(names, ref).then(done).catch(done.fail);
101+
}
102+
});
103+
});
104+
76105
});
77106

78107
describe('snapshotChanges()', () => {
@@ -83,7 +112,6 @@ describe('AngularFirestoreCollection', () => {
83112
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
84113
const sub = stocks.snapshotChanges().subscribe(data => {
85114
const ids = data.map(d => d.payload.doc.id);
86-
debugger;
87115
count = count + 1;
88116
// the first time should all be 'added'
89117
if(count === 1) {
@@ -133,7 +161,7 @@ describe('AngularFirestoreCollection', () => {
133161
const ITEMS = 10;
134162
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
135163

136-
const sub = stocks.snapshotChanges(['modified']).subscribe(data => {
164+
const sub = stocks.snapshotChanges(['modified']).skip(1).subscribe(data => {
137165
sub.unsubscribe();
138166
const change = data.filter(x => x.payload.doc.id === names[0])[0];
139167
expect(data.length).toEqual(1);

tools/build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const GLOBALS = {
1414
'rxjs/Subject': 'Rx',
1515
'rxjs/Observer': 'Rx',
1616
'rxjs/Subscription': 'Rx',
17+
'rxjs/BehaviorSubject': 'Rx',
1718
'rxjs/observable/merge': 'Rx.Observable',
1819
'rxjs/operator/share': 'Rx.Observable.prototype',
1920
'rxjs/operator/observeOn': 'Rx.Observable.prototype',

0 commit comments

Comments
 (0)