Skip to content

Commit 982acf9

Browse files
Add onSnapshotsInSync to firestore-exp (#3320)
1 parent c3cd471 commit 982acf9

File tree

4 files changed

+71
-23
lines changed

4 files changed

+71
-23
lines changed

.changeset/few-snails-walk.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/firestore/exp/index.node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export {
5454
getDocFromCache,
5555
getDocFromServer,
5656
onSnapshot,
57+
onSnapshotsInSync,
5758
setDoc,
5859
updateDoc,
5960
deleteDoc,

packages/firestore/exp/src/api/reference.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { cast } from '../../../lite/src/api/util';
2929
import { DocumentSnapshot, QuerySnapshot } from './snapshot';
3030
import {
3131
addDocSnapshotListener,
32+
addSnapshotsInSyncListener,
3233
addQuerySnapshotListener,
3334
applyFirestoreDataConverter,
3435
getDocsViaSnapshotListener,
@@ -431,6 +432,45 @@ export function onSnapshot<T>(
431432
};
432433
}
433434

435+
// TODO(firestorexp): Make sure these overloads are tested via the Firestore
436+
// integration tests
437+
export function onSnapshotsInSync(
438+
firestore: firestore.FirebaseFirestore,
439+
observer: {
440+
next?: (value: void) => void;
441+
error?: (error: firestore.FirestoreError) => void;
442+
complete?: () => void;
443+
}
444+
): Unsubscribe;
445+
export function onSnapshotsInSync(
446+
firestore: firestore.FirebaseFirestore,
447+
onSync: () => void
448+
): Unsubscribe;
449+
export function onSnapshotsInSync(
450+
firestore: firestore.FirebaseFirestore,
451+
arg: unknown
452+
): Unsubscribe {
453+
const firestoreImpl = cast(firestore, Firestore);
454+
const observer = isPartialObserver(arg)
455+
? (arg as PartialObserver<void>)
456+
: {
457+
next: arg as () => void
458+
};
459+
460+
const asyncObserver = firestoreImpl
461+
._getFirestoreClient()
462+
.then(firestoreClient =>
463+
addSnapshotsInSyncListener(firestoreClient, observer)
464+
);
465+
466+
// TODO(firestorexp): Add test that verifies that we don't raise a snapshot if
467+
// unsubscribe is called before `asyncObserver` resolves.
468+
return () => {
469+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
470+
asyncObserver.then(unsubscribe => unsubscribe());
471+
};
472+
}
473+
434474
/**
435475
* Converts a ViewSnapshot that contains the single document specified by `ref`
436476
* to a DocumentSnapshot.

packages/firestore/src/api/database.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -462,37 +462,19 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
462462
this.ensureClientConfigured();
463463

464464
if (isPartialObserver(arg)) {
465-
return this.onSnapshotsInSyncInternal(arg as PartialObserver<void>);
465+
return addSnapshotsInSyncListener(
466+
this._firestoreClient!,
467+
arg as PartialObserver<void>
468+
);
466469
} else {
467470
validateArgType('Firestore.onSnapshotsInSync', 'function', 1, arg);
468471
const observer: PartialObserver<void> = {
469472
next: arg as () => void
470473
};
471-
return this.onSnapshotsInSyncInternal(observer);
474+
return addSnapshotsInSyncListener(this._firestoreClient!, observer);
472475
}
473476
}
474477

475-
private onSnapshotsInSyncInternal(
476-
observer: PartialObserver<void>
477-
): Unsubscribe {
478-
const errHandler = (err: Error): void => {
479-
throw fail('Uncaught Error in onSnapshotsInSync');
480-
};
481-
const asyncObserver = new AsyncObserver<void>({
482-
next: () => {
483-
if (observer.next) {
484-
observer.next();
485-
}
486-
},
487-
error: errHandler
488-
});
489-
this._firestoreClient!.addSnapshotsInSyncListener(asyncObserver);
490-
return () => {
491-
asyncObserver.mute();
492-
this._firestoreClient!.removeSnapshotsInSyncListener(asyncObserver);
493-
};
494-
}
495-
496478
ensureClientConfigured(): FirestoreClient {
497479
if (!this._firestoreClient) {
498480
// Kick off starting the client but don't actually wait for it.
@@ -675,6 +657,29 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
675657
}
676658
}
677659

660+
/** Registers the listener for onSnapshotsInSync() */
661+
export function addSnapshotsInSyncListener(
662+
firestoreClient: FirestoreClient,
663+
observer: PartialObserver<void>
664+
): Unsubscribe {
665+
const errHandler = (err: Error): void => {
666+
throw fail('Uncaught Error in onSnapshotsInSync');
667+
};
668+
const asyncObserver = new AsyncObserver<void>({
669+
next: () => {
670+
if (observer.next) {
671+
observer.next();
672+
}
673+
},
674+
error: errHandler
675+
});
676+
firestoreClient.addSnapshotsInSyncListener(asyncObserver);
677+
return () => {
678+
asyncObserver.mute();
679+
firestoreClient.removeSnapshotsInSyncListener(asyncObserver);
680+
};
681+
}
682+
678683
/**
679684
* A reference to a transaction.
680685
*/

0 commit comments

Comments
 (0)