Skip to content

Commit 1a0ce7e

Browse files
Review
1 parent b18ed91 commit 1a0ce7e

File tree

1 file changed

+74
-74
lines changed

1 file changed

+74
-74
lines changed

packages/firestore/exp/test/shim.ts

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ import {
2929
deleteField,
3030
disableNetwork,
3131
doc,
32+
DocumentReference as DocumentReferenceExp,
3233
enableIndexedDbPersistence,
3334
enableMultiTabIndexedDbPersistence,
3435
enableNetwork,
35-
DocumentReference as DocumentReferenceExp,
3636
FieldPath as FieldPathExp,
3737
getDoc,
38+
getDocFromCache,
39+
getDocFromServer,
3840
getQuery,
3941
getQueryFromCache,
4042
getQueryFromServer,
4143
increment,
44+
initializeFirestore,
4245
onSnapshot,
4346
onSnapshotsInSync,
4447
parent,
@@ -51,10 +54,7 @@ import {
5154
terminate,
5255
updateDoc,
5356
waitForPendingWrites,
54-
writeBatch,
55-
getDocFromCache,
56-
getDocFromServer,
57-
initializeFirestore
57+
writeBatch
5858
} from '../../exp/index.node';
5959
import { UntypedFirestoreDataConverter } from '../../src/api/user_data_reader';
6060
import { isPartialObserver, PartialObserver } from '../../src/api/observer';
@@ -350,40 +350,12 @@ export class DocumentReference<T = legacy.DocumentData>
350350
onCompletion?: () => void
351351
): () => void;
352352
onSnapshot(...args: any): () => void {
353-
let options: legacy.SnapshotListenOptions = {};
354-
let userObserver: PartialObserver<DocumentSnapshot<T>>;
355-
356-
if (isPartialObserver(args[0])) {
357-
userObserver = args[0] as PartialObserver<DocumentSnapshot<T>>;
358-
} else if (isPartialObserver(args[1])) {
359-
options = args[0];
360-
userObserver = args[1];
361-
} else if (typeof args[0] === 'function') {
362-
userObserver = {
363-
next: args[0],
364-
error: args[1],
365-
complete: args[2]
366-
};
367-
} else {
368-
options = args[0];
369-
userObserver = {
370-
next: args[1],
371-
error: args[2],
372-
complete: args[3]
373-
};
374-
}
375-
376-
const apiObserver: PartialObserver<exp.DocumentSnapshot<T>> = {
377-
next: snapshot => {
378-
if (userObserver!.next) {
379-
userObserver!.next(new DocumentSnapshot(snapshot));
380-
}
381-
},
382-
error: userObserver.error?.bind(userObserver),
383-
complete: userObserver.complete?.bind(userObserver)
384-
};
385-
386-
return onSnapshot(this._delegate, options, apiObserver);
353+
const options = extractSnapshotOptions(args);
354+
const observer = wrapObserver<DocumentSnapshot<T>, exp.DocumentSnapshot<T>>(
355+
args,
356+
snap => new DocumentSnapshot(snap)
357+
);
358+
return onSnapshot(this._delegate, options, observer);
387359
}
388360

389361
withConverter<U>(
@@ -539,40 +511,12 @@ export class Query<T = legacy.DocumentData> implements legacy.Query<T> {
539511
onCompletion?: () => void
540512
): () => void;
541513
onSnapshot(...args: any): () => void {
542-
let options: legacy.SnapshotListenOptions = {};
543-
let userObserver: PartialObserver<QuerySnapshot<T>>;
544-
545-
if (isPartialObserver(args[0])) {
546-
userObserver = args[0] as PartialObserver<QuerySnapshot<T>>;
547-
} else if (isPartialObserver(args[1])) {
548-
options = args[0];
549-
userObserver = args[1];
550-
} else if (typeof args[0] === 'function') {
551-
userObserver = {
552-
next: args[0],
553-
error: args[1],
554-
complete: args[2]
555-
};
556-
} else {
557-
options = args[0];
558-
userObserver = {
559-
next: args[1],
560-
error: args[2],
561-
complete: args[3]
562-
};
563-
}
564-
565-
const apiObserver: PartialObserver<exp.QuerySnapshot<T>> = {
566-
next: snapshot => {
567-
if (userObserver!.next) {
568-
userObserver!.next(new QuerySnapshot(snapshot));
569-
}
570-
},
571-
error: userObserver.error?.bind(userObserver),
572-
complete: userObserver.complete?.bind(userObserver)
573-
};
574-
575-
return onSnapshot(this._delegate, options, apiObserver);
514+
const options = extractSnapshotOptions(args);
515+
const observer = wrapObserver<QuerySnapshot<T>, exp.QuerySnapshot<T>>(
516+
args,
517+
snap => new QuerySnapshot(snap)
518+
);
519+
return onSnapshot(this._delegate, options, observer);
576520
}
577521

578522
withConverter<U>(converter: legacy.FirestoreDataConverter<U>): Query<U> {
@@ -698,7 +642,7 @@ export class FieldValue implements legacy.FieldValue {
698642
}
699643

700644
export class FieldPath implements legacy.FieldPath {
701-
private fieldNames: string[];
645+
private readonly fieldNames: string[];
702646

703647
constructor(...fieldNames: string[]) {
704648
this.fieldNames = fieldNames;
@@ -766,3 +710,59 @@ function unwrap(value: any): any {
766710
return value;
767711
}
768712
}
713+
714+
/**
715+
* Creates an observer that can be passed to the firestore-exp SDK. The
716+
* observer converts all observed values into the format expected by the shim.
717+
*
718+
* @param args The list of arguments from an `onSnapshot` call.
719+
* @param wrapper The function that converts the firestore-exp type into the
720+
* type used by this shim.
721+
*/
722+
function wrapObserver<ShimType, ExpType>(
723+
args: any,
724+
wrapper: (val: ExpType) => ShimType
725+
): PartialObserver<ExpType> {
726+
let userObserver: PartialObserver<ShimType>;
727+
if (isPartialObserver(args[0])) {
728+
userObserver = args[0] as PartialObserver<ShimType>;
729+
} else if (isPartialObserver(args[1])) {
730+
userObserver = args[1];
731+
} else if (typeof args[0] === 'function') {
732+
userObserver = {
733+
next: args[0],
734+
error: args[1],
735+
complete: args[2]
736+
};
737+
} else {
738+
userObserver = {
739+
next: args[1],
740+
error: args[2],
741+
complete: args[3]
742+
};
743+
}
744+
745+
return {
746+
next: val => {
747+
if (userObserver!.next) {
748+
userObserver!.next(wrapper(val));
749+
}
750+
},
751+
error: userObserver.error?.bind(userObserver),
752+
complete: userObserver.complete?.bind(userObserver)
753+
};
754+
}
755+
756+
/**
757+
* Iterates the list of arguments from an `onSnapshot` call and returns the
758+
* first argument that may be an `SnapshotListenOptions` object. Returns an
759+
* empty object if none is found.
760+
*/
761+
function extractSnapshotOptions(args: any): exp.SnapshotListenOptions {
762+
for (const arg of args) {
763+
if (typeof arg === 'object' && !isPartialObserver(arg)) {
764+
return arg as exp.SnapshotListenOptions;
765+
}
766+
}
767+
return {};
768+
}

0 commit comments

Comments
 (0)