Skip to content

Commit c6aa319

Browse files
committed
Fix
1 parent 1b18c8a commit c6aa319

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

packages/firestore-compat/test/bundle.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ apiDescribe('Bundles', (persistence: boolean) => {
188188
await db.doc('coll-1/b').set({ k: 'b', bar: 0 });
189189

190190
const accumulator = new EventsAccumulator<firestore.QuerySnapshot>();
191-
db.collection('coll-1').onSnapshot(accumulator.storeEvent);
191+
const unsubscribe = db.collection('coll-1').onSnapshot(accumulator.storeEvent);
192192
await accumulator.awaitEvent();
193193

194194
const progress = await db.loadBundle(
@@ -207,6 +207,8 @@ apiDescribe('Bundles', (persistence: boolean) => {
207207

208208
snap = await (await db.namedQuery('limit-to-last'))!.get();
209209
expect(toDataArray(snap)).to.deep.equal([{ k: 'a', bar: 0 }]);
210+
211+
unsubscribe();
210212
});
211213
});
212214

packages/firestore-compat/test/get_options.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ apiDescribe('GetOptions', (persistence: boolean) => {
5555

5656
it('get document while offline with default get options', () => {
5757
const initialData = { key: 'value' };
58-
return withTestDocAndInitialData(persistence, initialData, docRef => {
58+
return withTestDocAndInitialData(persistence, initialData, async docRef => {
5959
// Register a snapshot to force the data to stay in the cache and not be
6060
// garbage collected.
61-
docRef.onSnapshot(() => {});
62-
return docRef
61+
const unsubscribe = docRef.onSnapshot(() => {});
62+
await docRef
6363
.get()
6464
.then(ignored => docRef.firestore.disableNetwork())
6565
.then(() => docRef.get())
@@ -69,6 +69,7 @@ apiDescribe('GetOptions', (persistence: boolean) => {
6969
expect(doc.metadata.hasPendingWrites).to.be.false;
7070
expect(doc.data()).to.deep.equal(initialData);
7171
});
72+
unsubscribe();
7273
});
7374
});
7475

packages/firestore/src/util/async_observer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ export class AsyncObserver<T> implements Observer<T> {
3636
constructor(private observer: Partial<Observer<T>>) {}
3737

3838
next(value: T): void {
39+
if (this.muted) return;
3940
if (this.observer.next) {
4041
this.scheduleEvent(this.observer.next, value);
4142
}
4243
}
4344

4445
error(error: FirestoreError): void {
46+
if (this.muted) return;
4547
if (this.observer.error) {
4648
this.scheduleEvent(this.observer.error, error);
4749
} else {
@@ -54,12 +56,10 @@ export class AsyncObserver<T> implements Observer<T> {
5456
}
5557

5658
private scheduleEvent<E>(eventHandler: EventHandler<E>, event: E): void {
57-
if (!this.muted) {
58-
setTimeout(() => {
59-
if (!this.muted) {
60-
eventHandler(event);
61-
}
62-
}, 0);
63-
}
59+
setTimeout(() => {
60+
if (!this.muted) {
61+
eventHandler(event);
62+
}
63+
}, 0);
6464
}
6565
}

packages/firestore/test/integration/api/database.test.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -840,15 +840,15 @@ apiDescribe('Database', persistence => {
840840
const gotInitialSnapshot = new Deferred<void>();
841841
const docA = doc(coll, 'a');
842842

843-
onSnapshot(docA, snap => {
843+
const unsubscribe1 = onSnapshot(docA, snap => {
844844
events.push('doc');
845845
gotInitialSnapshot.resolve();
846846
});
847847
await gotInitialSnapshot.promise;
848848
events = [];
849849

850850
const done = new Deferred<void>();
851-
onSnapshotsInSync(db, () => {
851+
const unsubscribe2 = onSnapshotsInSync(db, () => {
852852
events.push('snapshots-in-sync');
853853
if (events.length === 3) {
854854
// We should have an initial snapshots-in-sync event, then a snapshot
@@ -865,6 +865,8 @@ apiDescribe('Database', persistence => {
865865

866866
await setDoc(docA, { foo: 3 });
867867
await done.promise;
868+
unsubscribe1();
869+
unsubscribe2();
868870
});
869871
});
870872

@@ -1085,24 +1087,25 @@ apiDescribe('Database', persistence => {
10851087
});
10861088

10871089
it('DocumentSnapshot events for nonexistent document', () => {
1088-
return withTestCollection(persistence, {}, col => {
1090+
return withTestCollection(persistence, {}, async col => {
10891091
const docA = doc(col);
10901092
const storeEvent = new EventsAccumulator<DocumentSnapshot>();
1091-
onSnapshot(docA, storeEvent.storeEvent);
1092-
return storeEvent.awaitEvent().then(snap => {
1093+
const unsubscribe = onSnapshot(docA, storeEvent.storeEvent);
1094+
await storeEvent.awaitEvent().then(snap => {
10931095
expect(snap.exists()).to.be.false;
10941096
expect(snap.data()).to.equal(undefined);
1095-
return storeEvent.assertNoAdditionalEvents();
10961097
});
1098+
await storeEvent.assertNoAdditionalEvents();
1099+
unsubscribe()
10971100
});
10981101
});
10991102

11001103
it('DocumentSnapshot events for add data to document', () => {
1101-
return withTestCollection(persistence, {}, col => {
1104+
return withTestCollection(persistence, {}, async col => {
11021105
const docA = doc(col);
11031106
const storeEvent = new EventsAccumulator<DocumentSnapshot>();
1104-
onSnapshot(docA, { includeMetadataChanges: true }, storeEvent.storeEvent);
1105-
return storeEvent
1107+
const unsubscribe = onSnapshot(docA, { includeMetadataChanges: true }, storeEvent.storeEvent);
1108+
await storeEvent
11061109
.awaitEvent()
11071110
.then(snap => {
11081111
expect(snap.exists()).to.be.false;
@@ -1124,6 +1127,7 @@ apiDescribe('Database', persistence => {
11241127
// TODO(b/295872012): Figure out a way to check for all scenarios.
11251128
// expect(snap.metadata.hasPendingWrites).to.be.false;
11261129
});
1130+
unsubscribe();
11271131
});
11281132
});
11291133

0 commit comments

Comments
 (0)