Skip to content

Commit 4ffb664

Browse files
Accept Compat types in firestore-exp API (#4056)
1 parent 3a19f9e commit 4ffb664

File tree

12 files changed

+91
-13
lines changed

12 files changed

+91
-13
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
indexedDbStoragePrefix
5454
} from '../../../src/local/indexeddb_persistence';
5555
import { PersistenceSettings } from '../../../exp-types';
56+
import { cast } from '../../../src/util/input_validation';
5657

5758
/** DOMException error code constants. */
5859
const DOM_EXCEPTION_INVALID_STATE = 11;
@@ -169,6 +170,7 @@ export function enableIndexedDbPersistence(
169170
firestore: FirebaseFirestore,
170171
persistenceSettings?: PersistenceSettings
171172
): Promise<void> {
173+
firestore = cast(firestore, FirebaseFirestore);
172174
verifyNotInitialized(firestore);
173175

174176
const client = ensureFirestoreConfigured(firestore);
@@ -212,6 +214,7 @@ export function enableIndexedDbPersistence(
212214
export function enableMultiTabIndexedDbPersistence(
213215
firestore: FirebaseFirestore
214216
): Promise<void> {
217+
firestore = cast(firestore, FirebaseFirestore);
215218
verifyNotInitialized(firestore);
216219

217220
const client = ensureFirestoreConfigured(firestore);
@@ -366,6 +369,7 @@ export function clearIndexedDbPersistence(
366369
export function waitForPendingWrites(
367370
firestore: FirebaseFirestore
368371
): Promise<void> {
372+
firestore = cast(firestore, FirebaseFirestore);
369373
const client = ensureFirestoreConfigured(firestore);
370374
return firestoreClientWaitForPendingWrites(client);
371375
}
@@ -377,6 +381,7 @@ export function waitForPendingWrites(
377381
* @return A promise that is resolved once the network has been enabled.
378382
*/
379383
export function enableNetwork(firestore: FirebaseFirestore): Promise<void> {
384+
firestore = cast(firestore, FirebaseFirestore);
380385
const client = ensureFirestoreConfigured(firestore);
381386
return firestoreClientEnableNetwork(client);
382387
}
@@ -390,6 +395,7 @@ export function enableNetwork(firestore: FirebaseFirestore): Promise<void> {
390395
* @return A promise that is resolved once the network has been disabled.
391396
*/
392397
export function disableNetwork(firestore: FirebaseFirestore): Promise<void> {
398+
firestore = cast(firestore, FirebaseFirestore);
393399
const client = ensureFirestoreConfigured(firestore);
394400
return firestoreClientDisableNetwork(client);
395401
}

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export interface SnapshotListenOptions {
131131
export function getDoc<T>(
132132
reference: DocumentReference<T>
133133
): Promise<DocumentSnapshot<T>> {
134+
reference = cast<DocumentReference<T>>(reference, DocumentReference);
134135
const firestore = cast(reference.firestore, FirebaseFirestore);
135136
const client = ensureFirestoreConfigured(firestore);
136137

@@ -175,6 +176,7 @@ export class ExpUserDataWriter extends AbstractUserDataWriter {
175176
export function getDocFromCache<T>(
176177
reference: DocumentReference<T>
177178
): Promise<DocumentSnapshot<T>> {
179+
reference = cast<DocumentReference<T>>(reference, DocumentReference);
178180
const firestore = cast(reference.firestore, FirebaseFirestore);
179181
const client = ensureFirestoreConfigured(firestore);
180182
const userDataWriter = new ExpUserDataWriter(firestore);
@@ -210,6 +212,7 @@ export function getDocFromCache<T>(
210212
export function getDocFromServer<T>(
211213
reference: DocumentReference<T>
212214
): Promise<DocumentSnapshot<T>> {
215+
reference = cast<DocumentReference<T>>(reference, DocumentReference);
213216
const firestore = cast(reference.firestore, FirebaseFirestore);
214217
const client = ensureFirestoreConfigured(firestore);
215218

@@ -240,6 +243,7 @@ export function getDocFromServer<T>(
240243
* @return A Promise that will be resolved with the results of the query.
241244
*/
242245
export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>> {
246+
query = cast<Query<T>>(query, Query);
243247
const firestore = cast(query.firestore, FirebaseFirestore);
244248
const client = ensureFirestoreConfigured(firestore);
245249
const userDataWriter = new ExpUserDataWriter(firestore);
@@ -271,6 +275,7 @@ export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>> {
271275
export function getDocsFromCache<T>(
272276
query: Query<T>
273277
): Promise<QuerySnapshot<T>> {
278+
query = cast<Query<T>>(query, Query);
274279
const firestore = cast(query.firestore, FirebaseFirestore);
275280
const client = ensureFirestoreConfigured(firestore);
276281
const userDataWriter = new ExpUserDataWriter(firestore);
@@ -294,6 +299,7 @@ export function getDocsFromCache<T>(
294299
export function getDocsFromServer<T>(
295300
query: Query<T>
296301
): Promise<QuerySnapshot<T>> {
302+
query = cast<Query<T>>(query, Query);
297303
const firestore = cast(query.firestore, FirebaseFirestore);
298304
const client = ensureFirestoreConfigured(firestore);
299305
const userDataWriter = new ExpUserDataWriter(firestore);
@@ -348,6 +354,7 @@ export function setDoc<T>(
348354
data: T,
349355
options?: SetOptions
350356
): Promise<void> {
357+
reference = cast<DocumentReference<T>>(reference, DocumentReference);
351358
const firestore = cast(reference.firestore, FirebaseFirestore);
352359

353360
const convertedValue = applyFirestoreDataConverter(
@@ -412,6 +419,7 @@ export function updateDoc(
412419
value?: unknown,
413420
...moreFieldsAndValues: unknown[]
414421
): Promise<void> {
422+
reference = cast<DocumentReference<unknown>>(reference, DocumentReference);
415423
const firestore = cast(reference.firestore, FirebaseFirestore);
416424

417425
const dataReader = newUserDataReader(firestore);
@@ -701,6 +709,10 @@ export function onSnapshot<T>(
701709
reference: Query<T> | DocumentReference<T>,
702710
...args: unknown[]
703711
): Unsubscribe {
712+
if (reference instanceof Compat) {
713+
reference = reference._delegate;
714+
}
715+
704716
let options: SnapshotListenOptions = {
705717
includeMetadataChanges: false
706718
};
@@ -733,23 +745,28 @@ export function onSnapshot<T>(
733745
next: snapshot => {
734746
if (args[currArg]) {
735747
(args[currArg] as NextFn<DocumentSnapshot<T>>)(
736-
convertToDocSnapshot(firestore, reference, snapshot)
748+
convertToDocSnapshot(
749+
firestore,
750+
reference as DocumentReference<T>,
751+
snapshot
752+
)
737753
);
738754
}
739755
},
740756
error: args[currArg + 1] as ErrorFn,
741757
complete: args[currArg + 2] as CompleteFn
742758
};
743759
} else {
744-
firestore = cast(reference.firestore, FirebaseFirestore);
745-
internalQuery = reference._query;
760+
const query = cast<Query<T>>(reference, Query);
761+
firestore = cast(query.firestore, FirebaseFirestore);
762+
internalQuery = query._query;
746763
const userDataWriter = new ExpUserDataWriter(firestore);
747764

748765
observer = {
749766
next: snapshot => {
750767
if (args[currArg]) {
751768
(args[currArg] as NextFn<QuerySnapshot<T>>)(
752-
new QuerySnapshot(firestore, userDataWriter, reference, snapshot)
769+
new QuerySnapshot(firestore, userDataWriter, query, snapshot)
753770
);
754771
}
755772
},
@@ -832,6 +849,7 @@ export function onSnapshotsInSync(
832849
firestore: FirebaseFirestore,
833850
arg: unknown
834851
): Unsubscribe {
852+
firestore = cast(firestore, FirebaseFirestore);
835853
const client = ensureFirestoreConfigured(firestore);
836854

837855
const observer = isPartialObserver(arg)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { WriteBatch } from '../../../lite/src/api/write_batch';
1919
import { FirebaseFirestore } from './database';
2020
import { executeWrite } from './reference';
2121
import { ensureFirestoreConfigured } from '../../../src/api/database';
22+
import { cast } from '../../../src/util/input_validation';
2223

2324
export { WriteBatch };
2425

@@ -34,6 +35,7 @@ export { WriteBatch };
3435
* writes.
3536
*/
3637
export function writeBatch(firestore: FirebaseFirestore): WriteBatch {
38+
firestore = cast(firestore, FirebaseFirestore);
3739
ensureFirestoreConfigured(firestore);
3840
return new WriteBatch(firestore, mutations =>
3941
executeWrite(firestore, mutations)

packages/firestore/lite/src/api/database.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ import {
3535
LRU_DEFAULT_CACHE_SIZE_BYTES,
3636
LRU_MINIMUM_CACHE_SIZE_BYTES
3737
} from '../../../src/local/lru_garbage_collector';
38-
import { validateIsNotUsedTogether } from '../../../src/util/input_validation';
38+
import {
39+
cast,
40+
validateIsNotUsedTogether
41+
} from '../../../src/util/input_validation';
3942

4043
declare module '@firebase/component' {
4144
interface NameServiceMapping {
@@ -316,6 +319,7 @@ export function getFirestore(app: FirebaseApp): FirebaseFirestore {
316319
* terminated.
317320
*/
318321
export function terminate(firestore: FirebaseFirestore): Promise<void> {
322+
firestore = cast(firestore, FirebaseFirestore);
319323
_removeServiceInstance(firestore.app, 'firestore/lite');
320324
return firestore._delete();
321325
}

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import { DeleteMutation, Precondition } from '../../../src/model/mutation';
7474
import { applyFirestoreDataConverter } from '../../../src/api/database';
7575
import { FieldPath } from './field_path';
7676
import {
77+
cast,
7778
validateCollectionPath,
7879
validateDocumentPath,
7980
validateNonEmptyArgument,
@@ -1147,6 +1148,10 @@ export function collection(
11471148
path: string,
11481149
...pathSegments: string[]
11491150
): CollectionReference<DocumentData> {
1151+
if (parent instanceof Compat) {
1152+
parent = parent._delegate;
1153+
}
1154+
11501155
validateNonEmptyArgument('collection', 'path', path);
11511156
if (parent instanceof FirebaseFirestore) {
11521157
const absolutePath = ResourcePath.fromString(path, ...pathSegments);
@@ -1194,6 +1199,8 @@ export function collectionGroup(
11941199
firestore: FirebaseFirestore,
11951200
collectionId: string
11961201
): Query<DocumentData> {
1202+
firestore = cast(firestore, FirebaseFirestore);
1203+
11971204
validateNonEmptyArgument('collectionGroup', 'collection id', collectionId);
11981205
if (collectionId.indexOf('/') >= 0) {
11991206
throw new FirestoreError(
@@ -1272,6 +1279,10 @@ export function doc<T>(
12721279
path?: string,
12731280
...pathSegments: string[]
12741281
): DocumentReference {
1282+
if (parent instanceof Compat) {
1283+
parent = parent._delegate;
1284+
}
1285+
12751286
// We allow omission of 'pathString' but explicitly prohibit passing in both
12761287
// 'undefined' and 'null'.
12771288
if (arguments.length === 1) {
@@ -1341,6 +1352,7 @@ export class LiteUserDataWriter extends AbstractUserDataWriter {
13411352
export function getDoc<T>(
13421353
reference: DocumentReference<T>
13431354
): Promise<DocumentSnapshot<T>> {
1355+
reference = cast<DocumentReference<T>>(reference, DocumentReference);
13441356
const datastore = getDatastore(reference.firestore);
13451357
const userDataWriter = new LiteUserDataWriter(reference.firestore);
13461358

@@ -1372,6 +1384,7 @@ export function getDoc<T>(
13721384
* @return A Promise that will be resolved with the results of the query.
13731385
*/
13741386
export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>> {
1387+
query = cast<Query<T>>(query, Query);
13751388
validateHasExplicitOrderByForLimitToLast(query._query);
13761389

13771390
const datastore = getDatastore(query.firestore);
@@ -1443,6 +1456,7 @@ export function setDoc<T>(
14431456
data: T,
14441457
options?: SetOptions
14451458
): Promise<void> {
1459+
reference = cast<DocumentReference<T>>(reference, DocumentReference);
14461460
const convertedValue = applyFirestoreDataConverter(
14471461
reference._converter,
14481462
data,
@@ -1518,6 +1532,7 @@ export function updateDoc(
15181532
value?: unknown,
15191533
...moreFieldsAndValues: unknown[]
15201534
): Promise<void> {
1535+
reference = cast<DocumentReference<unknown>>(reference, DocumentReference);
15211536
const dataReader = newUserDataReader(reference.firestore);
15221537

15231538
// For Compat types, we have to "extract" the underlying types before
@@ -1567,7 +1582,10 @@ export function updateDoc(
15671582
* @return A Promise resolved once the document has been successfully
15681583
* deleted from the backend.
15691584
*/
1570-
export function deleteDoc(reference: DocumentReference): Promise<void> {
1585+
export function deleteDoc(
1586+
reference: DocumentReference<unknown>
1587+
): Promise<void> {
1588+
reference = cast<DocumentReference<unknown>>(reference, DocumentReference);
15711589
const datastore = getDatastore(reference.firestore);
15721590
return invokeCommitRpc(datastore, [
15731591
new DeleteMutation(reference._key, Precondition.none())
@@ -1592,6 +1610,7 @@ export function addDoc<T>(
15921610
reference: CollectionReference<T>,
15931611
data: T
15941612
): Promise<DocumentReference<T>> {
1613+
reference = cast<CollectionReference<T>>(reference, CollectionReference);
15951614
const docRef = doc(reference);
15961615

15971616
const convertedValue = applyFirestoreDataConverter(
@@ -1628,6 +1647,13 @@ export function refEqual<T>(
16281647
left: DocumentReference<T> | CollectionReference<T>,
16291648
right: DocumentReference<T> | CollectionReference<T>
16301649
): boolean {
1650+
if (left instanceof Compat) {
1651+
left = left._delegate;
1652+
}
1653+
if (right instanceof Compat) {
1654+
right = right._delegate;
1655+
}
1656+
16311657
if (
16321658
(left instanceof DocumentReference ||
16331659
left instanceof CollectionReference) &&
@@ -1652,6 +1678,13 @@ export function refEqual<T>(
16521678
* Firestore database.
16531679
*/
16541680
export function queryEqual<T>(left: Query<T>, right: Query<T>): boolean {
1681+
if (left instanceof Compat) {
1682+
left = left._delegate;
1683+
}
1684+
if (right instanceof Compat) {
1685+
right = right._delegate;
1686+
}
1687+
16551688
if (left instanceof Query && right instanceof Query) {
16561689
return (
16571690
left.firestore === right.firestore &&

packages/firestore/lite/src/api/snapshot.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ export function snapshotEqual<T>(
286286
left: DocumentSnapshot<T> | QuerySnapshot<T>,
287287
right: DocumentSnapshot<T> | QuerySnapshot<T>
288288
): boolean {
289+
if (left instanceof Compat) {
290+
left = left._delegate;
291+
}
292+
if (right instanceof Compat) {
293+
right = right._delegate;
294+
}
295+
289296
if (left instanceof DocumentSnapshot && right instanceof DocumentSnapshot) {
290297
return (
291298
left._firestore === right._firestore &&

packages/firestore/lite/src/api/transaction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
} from './reference';
4545
import { FieldPath } from './field_path';
4646
import { getDatastore } from './components';
47+
import { cast } from '../../../src/util/input_validation';
4748
import { Compat } from '../../../src/compat/compat';
4849

4950
// TODO(mrschmidt) Consider using `BaseTransaction` as the base class in the
@@ -264,6 +265,7 @@ export function runTransaction<T>(
264265
firestore: FirebaseFirestore,
265266
updateFunction: (transaction: Transaction) => Promise<T>
266267
): Promise<T> {
268+
firestore = cast(firestore, FirebaseFirestore);
267269
const datastore = getDatastore(firestore);
268270
const deferred = new Deferred<T>();
269271
new TransactionRunner<T>(

packages/firestore/lite/src/api/write_batch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { FirebaseFirestore } from './database';
3838
import { invokeCommitRpc } from '../../../src/remote/datastore';
3939
import { FieldPath } from './field_path';
4040
import { getDatastore } from './components';
41+
import { cast } from '../../../src/util/input_validation';
4142
import { Compat } from '../../../src/compat/compat';
4243

4344
/**
@@ -269,6 +270,7 @@ export function validateReference<T>(
269270
* writes.
270271
*/
271272
export function writeBatch(firestore: FirebaseFirestore): WriteBatch {
273+
firestore = cast(firestore, FirebaseFirestore);
272274
const datastore = getDatastore(firestore);
273275
return new WriteBatch(firestore, writes =>
274276
invokeCommitRpc(datastore, writes)

packages/firestore/src/api/database.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ export class DocumentReference<T = PublicDocumentData>
662662
if (other instanceof Compat) {
663663
other = other._delegate;
664664
}
665+
665666
if (!(other instanceof ExpDocumentReference)) {
666667
return false;
667668
}

packages/firestore/src/api/field_path.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class FieldPath extends Compat<ExpFieldPath> implements PublicFieldPath {
5555
if (other instanceof Compat) {
5656
other = other._delegate;
5757
}
58+
5859
if (!(other instanceof ExpFieldPath)) {
5960
return false;
6061
}

0 commit comments

Comments
 (0)