Skip to content

Commit 7588ffd

Browse files
committed
cleanup observer marshalling
1 parent 8a181e1 commit 7588ffd

File tree

2 files changed

+54
-63
lines changed

2 files changed

+54
-63
lines changed

common/api-review/firestore.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(query
456456
}): Unsubscribe;
457457

458458
// @public
459-
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(query: Query<AppModelType, DbModelType>, onNext?: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void): Unsubscribe;
459+
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(query: Query<AppModelType, DbModelType>, onNext: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void): Unsubscribe;
460460

461461
// @public
462462
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(query: Query<AppModelType, DbModelType>, options: SnapshotListenOptions, onNext: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void): Unsubscribe;

packages/firestore/src/api/reference_impl.ts

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
650650
*/
651651
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
652652
query: Query<AppModelType, DbModelType>,
653-
onNext?: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void,
653+
onNext: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void,
654654
onError?: (error: FirestoreError) => void,
655655
onCompletion?: () => void
656656
): Unsubscribe;
@@ -810,7 +810,7 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
810810
...args: unknown[]
811811
): Unsubscribe {
812812
if (reference instanceof Firestore) {
813-
return onSnapshotBundle(reference as Firestore, args);
813+
return onSnapshotBundle(reference as Firestore, ...args);
814814
}
815815

816816
// onSnapshot for Query or Document.
@@ -821,8 +821,7 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
821821
};
822822
let currArg = 0;
823823
if (typeof args[currArg] === 'object' && !isPartialObserver(args[currArg])) {
824-
options = args[currArg] as SnapshotListenOptions;
825-
currArg++;
824+
options = args[currArg++] as SnapshotListenOptions;
826825
}
827826

828827
const internalOptions = {
@@ -869,7 +868,6 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
869868
firestore = cast(query.firestore, Firestore);
870869
internalQuery = query._query;
871870
const userDataWriter = new ExpUserDataWriter(firestore);
872-
873871
observer = {
874872
next: snapshot => {
875873
if (args[currArg]) {
@@ -1012,75 +1010,68 @@ function onSnapshotBundle<AppModelType, DbModelType extends DocumentData>(
10121010
options = args[curArg++] as SnapshotListenOptions;
10131011
}
10141012

1015-
let error: undefined | ((error: FirestoreError) => void) = undefined;
1016-
let complete: undefined | (() => void) = undefined;
1017-
10181013
if (json.bundleSource === 'QuerySnapshot') {
1019-
let next:
1020-
| undefined
1021-
| ((snapshot: QuerySnapshot<AppModelType, DbModelType>) => void);
10221014
if (typeof args[curArg] === 'object' && isPartialObserver(args[1])) {
1023-
const userObserver = args[curArg] as PartialObserver<
1015+
const userObserver = args[curArg++] as PartialObserver<
10241016
QuerySnapshot<AppModelType, DbModelType>
10251017
>;
1026-
next = userObserver.next?.bind(userObserver);
1027-
error = userObserver.error?.bind(userObserver);
1028-
complete = userObserver.complete?.bind(userObserver);
1029-
curArg++;
1018+
return onSnapshotQuerySnapshotBundle(
1019+
db,
1020+
json,
1021+
options,
1022+
{
1023+
next: userObserver.next!,
1024+
error: userObserver.error,
1025+
complete: userObserver.complete
1026+
},
1027+
args[curArg] as FirestoreDataConverter<DbModelType>
1028+
);
10301029
} else {
1031-
next = args[curArg++] as (
1032-
snapshot: QuerySnapshot<AppModelType, DbModelType>
1033-
) => void;
1034-
error = args[curArg++] as (error: FirestoreError) => void;
1035-
complete = args[curArg++] as () => void;
1030+
return onSnapshotQuerySnapshotBundle(
1031+
db,
1032+
json,
1033+
options,
1034+
{
1035+
next: args[curArg++] as (
1036+
snapshot: QuerySnapshot<AppModelType, DbModelType>
1037+
) => void,
1038+
error: args[curArg++] as (error: FirestoreError) => void,
1039+
complete: args[curArg++] as () => void
1040+
},
1041+
args[curArg] as FirestoreDataConverter<DbModelType>
1042+
);
10361043
}
1037-
const converter = args[curArg] as
1038-
| FirestoreDataConverter<DbModelType>
1039-
| undefined;
1040-
return onSnapshotQuerySnapshotBundle(
1041-
db,
1042-
json,
1043-
options,
1044-
{
1045-
next: next!,
1046-
error,
1047-
complete
1048-
},
1049-
converter
1050-
);
10511044
} else if (json.bundleSource === 'DocumentSnapshot') {
1052-
let next:
1053-
| undefined
1054-
| ((snapshot: DocumentSnapshot<AppModelType, DbModelType>) => void);
10551045
if (typeof args[curArg] === 'object' && isPartialObserver(args[1])) {
1056-
const userObserver = args[curArg] as PartialObserver<
1046+
const userObserver = args[curArg++] as PartialObserver<
10571047
DocumentSnapshot<AppModelType, DbModelType>
10581048
>;
1059-
next = userObserver.next?.bind(userObserver);
1060-
error = userObserver.error?.bind(userObserver);
1061-
complete = userObserver.complete?.bind(userObserver);
1062-
curArg++;
1049+
return onSnapshotDocumentSnapshotBundle(
1050+
db,
1051+
json,
1052+
options,
1053+
{
1054+
next: userObserver.next!,
1055+
error: userObserver.error,
1056+
complete: userObserver.complete
1057+
},
1058+
args[curArg] as FirestoreDataConverter<DbModelType>
1059+
);
10631060
} else {
1064-
next = args[curArg++] as (
1065-
snapshot: DocumentSnapshot<AppModelType, DbModelType>
1066-
) => void;
1067-
error = args[curArg++] as (error: FirestoreError) => void;
1068-
complete = args[curArg++] as () => void;
1061+
return onSnapshotDocumentSnapshotBundle(
1062+
db,
1063+
json,
1064+
options,
1065+
{
1066+
next: args[curArg++] as (
1067+
snapshot: DocumentSnapshot<AppModelType, DbModelType>
1068+
) => void,
1069+
error: args[curArg++] as (error: FirestoreError) => void,
1070+
complete: args[curArg++] as () => void
1071+
},
1072+
args[curArg] as FirestoreDataConverter<DbModelType>
1073+
);
10691074
}
1070-
const converter = args[curArg] as
1071-
| FirestoreDataConverter<DbModelType>
1072-
| undefined;
1073-
return onSnapshotDocumentSnapshotBundle(
1074-
db,
1075-
json,
1076-
options,
1077-
{
1078-
next: next!,
1079-
error,
1080-
complete
1081-
},
1082-
converter
1083-
);
10841075
} else {
10851076
throw new FirestoreError(
10861077
Code.INVALID_ARGUMENT,

0 commit comments

Comments
 (0)