Skip to content

Add onSnapshotsInSync to firestore-exp #3320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/few-snails-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
1 change: 1 addition & 0 deletions packages/firestore/exp/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export {
getDocFromCache,
getDocFromServer,
onSnapshot,
onSnapshotsInSync,
setDoc,
updateDoc,
deleteDoc,
Expand Down
40 changes: 40 additions & 0 deletions packages/firestore/exp/src/api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { cast } from '../../../lite/src/api/util';
import { DocumentSnapshot, QuerySnapshot } from './snapshot';
import {
addDocSnapshotListener,
addSnapshotsInSyncListener,
addQuerySnapshotListener,
applyFirestoreDataConverter,
getDocsViaSnapshotListener,
Expand Down Expand Up @@ -431,6 +432,45 @@ export function onSnapshot<T>(
};
}

// TODO(firestorexp): Make sure these overloads are tested via the Firestore
// integration tests
export function onSnapshotsInSync(
firestore: firestore.FirebaseFirestore,
observer: {
next?: (value: void) => void;
error?: (error: firestore.FirestoreError) => void;
complete?: () => void;
}
): Unsubscribe;
export function onSnapshotsInSync(
firestore: firestore.FirebaseFirestore,
onSync: () => void
): Unsubscribe;
export function onSnapshotsInSync(
firestore: firestore.FirebaseFirestore,
arg: unknown
): Unsubscribe {
const firestoreImpl = cast(firestore, Firestore);
const observer = isPartialObserver(arg)
? (arg as PartialObserver<void>)
: {
next: arg as () => void
};

const asyncObserver = firestoreImpl
._getFirestoreClient()
.then(firestoreClient =>
addSnapshotsInSyncListener(firestoreClient, observer)
);

// TODO(firestorexp): Add test that verifies that we don't raise a snapshot if
// unsubscribe is called before `asyncObserver` resolves.
return () => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
asyncObserver.then(unsubscribe => unsubscribe());
};
}

/**
* Converts a ViewSnapshot that contains the single document specified by `ref`
* to a DocumentSnapshot.
Expand Down
51 changes: 28 additions & 23 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,37 +462,19 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
this.ensureClientConfigured();

if (isPartialObserver(arg)) {
return this.onSnapshotsInSyncInternal(arg as PartialObserver<void>);
return addSnapshotsInSyncListener(
this._firestoreClient!,
arg as PartialObserver<void>
);
} else {
validateArgType('Firestore.onSnapshotsInSync', 'function', 1, arg);
const observer: PartialObserver<void> = {
next: arg as () => void
};
return this.onSnapshotsInSyncInternal(observer);
return addSnapshotsInSyncListener(this._firestoreClient!, observer);
}
}

private onSnapshotsInSyncInternal(
observer: PartialObserver<void>
): Unsubscribe {
const errHandler = (err: Error): void => {
throw fail('Uncaught Error in onSnapshotsInSync');
};
const asyncObserver = new AsyncObserver<void>({
next: () => {
if (observer.next) {
observer.next();
}
},
error: errHandler
});
this._firestoreClient!.addSnapshotsInSyncListener(asyncObserver);
return () => {
asyncObserver.mute();
this._firestoreClient!.removeSnapshotsInSyncListener(asyncObserver);
};
}

ensureClientConfigured(): FirestoreClient {
if (!this._firestoreClient) {
// Kick off starting the client but don't actually wait for it.
Expand Down Expand Up @@ -675,6 +657,29 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
}
}

/** Registers the listener for onSnapshotsInSync() */
export function addSnapshotsInSyncListener(
firestoreClient: FirestoreClient,
observer: PartialObserver<void>
): Unsubscribe {
const errHandler = (err: Error): void => {
throw fail('Uncaught Error in onSnapshotsInSync');
};
const asyncObserver = new AsyncObserver<void>({
next: () => {
if (observer.next) {
observer.next();
}
},
error: errHandler
});
firestoreClient.addSnapshotsInSyncListener(asyncObserver);
return () => {
asyncObserver.mute();
firestoreClient.removeSnapshotsInSyncListener(asyncObserver);
};
}

/**
* A reference to a transaction.
*/
Expand Down