Skip to content

Commit ab0f643

Browse files
Make FirestoreClient tree-shakeable (#3983)
1 parent 8e9604f commit ab0f643

File tree

2 files changed

+260
-206
lines changed

2 files changed

+260
-206
lines changed

packages/firestore/src/api/database.ts

+67-33
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,20 @@ import {
5555
OfflineComponentProvider,
5656
OnlineComponentProvider
5757
} from '../core/component_provider';
58-
import { FirestoreClient } from '../core/firestore_client';
58+
import {
59+
FirestoreClient,
60+
firestoreClientAddSnapshotsInSyncListener,
61+
firestoreClientDisableNetwork,
62+
firestoreClientEnableNetwork,
63+
firestoreClientGetDocumentFromLocalCache,
64+
firestoreClientGetDocumentsFromLocalCache,
65+
firestoreClientGetDocumentsViaSnapshotListener,
66+
firestoreClientGetDocumentViaSnapshotListener,
67+
firestoreClientListen,
68+
firestoreClientTransaction,
69+
firestoreClientWaitForPendingWrites,
70+
firestoreClientWrite
71+
} from '../core/firestore_client';
5972
import {
6073
Bound,
6174
Direction,
@@ -101,7 +114,7 @@ import {
101114
validateSetOptions,
102115
valueDescription
103116
} from '../util/input_validation';
104-
import { setLogLevel as setClientLogLevel, logWarn } from '../util/log';
117+
import { logWarn, setLogLevel as setClientLogLevel } from '../util/log';
105118
import { AutoId } from '../util/misc';
106119
import { Deferred } from '../util/promise';
107120
import { FieldPath as ExternalFieldPath } from './field_path';
@@ -461,12 +474,12 @@ export class Firestore implements PublicFirestore, FirebaseService {
461474

462475
enableNetwork(): Promise<void> {
463476
this.ensureClientConfigured();
464-
return this._firestoreClient!.enableNetwork();
477+
return firestoreClientEnableNetwork(this._firestoreClient!);
465478
}
466479

467480
disableNetwork(): Promise<void> {
468481
this.ensureClientConfigured();
469-
return this._firestoreClient!.disableNetwork();
482+
return firestoreClientDisableNetwork(this._firestoreClient!);
470483
}
471484

472485
enablePersistence(settings?: PublicPersistenceSettings): Promise<void> {
@@ -528,7 +541,7 @@ export class Firestore implements PublicFirestore, FirebaseService {
528541

529542
waitForPendingWrites(): Promise<void> {
530543
this.ensureClientConfigured();
531-
return this._firestoreClient!.waitForPendingWrites();
544+
return firestoreClientWaitForPendingWrites(this._firestoreClient!);
532545
}
533546

534547
onSnapshotsInSync(observer: PartialObserver<void>): Unsubscribe;
@@ -537,14 +550,18 @@ export class Firestore implements PublicFirestore, FirebaseService {
537550
this.ensureClientConfigured();
538551

539552
if (isPartialObserver(arg)) {
540-
return this._firestoreClient!.addSnapshotsInSyncListener(
553+
return firestoreClientAddSnapshotsInSyncListener(
554+
this._firestoreClient!,
541555
arg as PartialObserver<void>
542556
);
543557
} else {
544558
const observer: PartialObserver<void> = {
545559
next: arg as () => void
546560
};
547-
return this._firestoreClient!.addSnapshotsInSyncListener(observer);
561+
return firestoreClientAddSnapshotsInSyncListener(
562+
this._firestoreClient!,
563+
observer
564+
);
548565
}
549566
}
550567

@@ -676,7 +693,9 @@ export class Firestore implements PublicFirestore, FirebaseService {
676693
runTransaction<T>(
677694
updateFunction: (transaction: PublicTransaction) => Promise<T>
678695
): Promise<T> {
679-
return this.ensureClientConfigured().transaction(
696+
const firestoreClient = this.ensureClientConfigured();
697+
return firestoreClientTransaction(
698+
firestoreClient,
680699
(transaction: InternalTransaction) => {
681700
return updateFunction(new Transaction(this, transaction));
682701
}
@@ -685,7 +704,6 @@ export class Firestore implements PublicFirestore, FirebaseService {
685704

686705
batch(): PublicWriteBatch {
687706
this.ensureClientConfigured();
688-
689707
return new WriteBatch(this);
690708
}
691709

@@ -966,7 +984,8 @@ export class WriteBatch implements PublicWriteBatch {
966984
this.verifyNotCommitted();
967985
this._committed = true;
968986
if (this._mutations.length > 0) {
969-
return this._firestore.ensureClientConfigured().write(this._mutations);
987+
const firestoreClient = this._firestore.ensureClientConfigured();
988+
return firestoreClientWrite(firestoreClient, this._mutations);
970989
}
971990

972991
return Promise.resolve();
@@ -1080,7 +1099,8 @@ export class DocumentReference<T = DocumentData>
10801099
this._converter !== null,
10811100
options
10821101
);
1083-
return this._firestoreClient.write(
1102+
return firestoreClientWrite(
1103+
this._firestoreClient,
10841104
parsed.toMutations(this._key, Precondition.none())
10851105
);
10861106
}
@@ -1119,13 +1139,14 @@ export class DocumentReference<T = DocumentData>
11191139
);
11201140
}
11211141

1122-
return this._firestoreClient.write(
1142+
return firestoreClientWrite(
1143+
this._firestoreClient,
11231144
parsed.toMutations(this._key, Precondition.exists(true))
11241145
);
11251146
}
11261147

11271148
delete(): Promise<void> {
1128-
return this._firestoreClient.write([
1149+
return firestoreClientWrite(this._firestoreClient, [
11291150
new DeleteMutation(this._key, Precondition.none())
11301151
]);
11311152
}
@@ -1185,7 +1206,8 @@ export class DocumentReference<T = DocumentData>
11851206
complete: args[currArg + 2] as CompleteFn
11861207
};
11871208

1188-
return this._firestoreClient.listen(
1209+
return firestoreClientListen(
1210+
this._firestoreClient,
11891211
newQueryForPath(this._key.path),
11901212
internalOptions,
11911213
observer
@@ -1195,23 +1217,26 @@ export class DocumentReference<T = DocumentData>
11951217
get(options?: GetOptions): Promise<PublicDocumentSnapshot<T>> {
11961218
const firestoreClient = this.firestore.ensureClientConfigured();
11971219
if (options && options.source === 'cache') {
1198-
return firestoreClient
1199-
.getDocumentFromLocalCache(this._key)
1200-
.then(
1201-
doc =>
1202-
new DocumentSnapshot(
1203-
this.firestore,
1204-
this._key,
1205-
doc,
1206-
/*fromCache=*/ true,
1207-
doc instanceof Document ? doc.hasLocalMutations : false,
1208-
this._converter
1209-
)
1210-
);
1220+
return firestoreClientGetDocumentFromLocalCache(
1221+
firestoreClient,
1222+
this._key
1223+
).then(
1224+
doc =>
1225+
new DocumentSnapshot(
1226+
this.firestore,
1227+
this._key,
1228+
doc,
1229+
/*fromCache=*/ true,
1230+
doc instanceof Document ? doc.hasLocalMutations : false,
1231+
this._converter
1232+
)
1233+
);
12111234
} else {
1212-
return firestoreClient
1213-
.getDocumentViaSnapshotListener(this._key, options)
1214-
.then(snapshot => this._convertToDocSnapshot(snapshot));
1235+
return firestoreClientGetDocumentViaSnapshotListener(
1236+
firestoreClient,
1237+
this._key,
1238+
options
1239+
).then(snapshot => this._convertToDocSnapshot(snapshot));
12151240
}
12161241
}
12171242

@@ -2036,7 +2061,12 @@ export class Query<T = DocumentData> implements PublicQuery<T> {
20362061

20372062
validateHasExplicitOrderByForLimitToLast(this._query);
20382063
const firestoreClient = this.firestore.ensureClientConfigured();
2039-
return firestoreClient.listen(this._query, options, observer);
2064+
return firestoreClientListen(
2065+
firestoreClient,
2066+
this._query,
2067+
options,
2068+
observer
2069+
);
20402070
}
20412071

20422072
get(options?: GetOptions): Promise<PublicQuerySnapshot<T>> {
@@ -2045,8 +2075,12 @@ export class Query<T = DocumentData> implements PublicQuery<T> {
20452075

20462076
const firestoreClient = this.firestore.ensureClientConfigured();
20472077
return (options && options.source === 'cache'
2048-
? firestoreClient.getDocumentsFromLocalCache(this._query)
2049-
: firestoreClient.getDocumentsViaSnapshotListener(this._query, options)
2078+
? firestoreClientGetDocumentsFromLocalCache(firestoreClient, this._query)
2079+
: firestoreClientGetDocumentsViaSnapshotListener(
2080+
firestoreClient,
2081+
this._query,
2082+
options
2083+
)
20502084
).then(
20512085
snap =>
20522086
new QuerySnapshot(this.firestore, this._query, snap, this._converter)

0 commit comments

Comments
 (0)