Skip to content

Commit

Permalink
Add onSnapshotsInSync to firestore-exp (#3320)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian authored Jun 30, 2020
1 parent c3cd471 commit 982acf9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 23 deletions.
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

0 comments on commit 982acf9

Please sign in to comment.