Skip to content

Make FirestoreClient tree-shakeable #3983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 67 additions & 33 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,20 @@ import {
OfflineComponentProvider,
OnlineComponentProvider
} from '../core/component_provider';
import { FirestoreClient } from '../core/firestore_client';
import {
FirestoreClient,
firestoreClientAddSnapshotsInSyncListener,
firestoreClientDisableNetwork,
firestoreClientEnableNetwork,
firestoreClientGetDocumentFromLocalCache,
firestoreClientGetDocumentsFromLocalCache,
firestoreClientGetDocumentsViaSnapshotListener,
firestoreClientGetDocumentViaSnapshotListener,
firestoreClientListen,
firestoreClientTransaction,
firestoreClientWaitForPendingWrites,
firestoreClientWrite
} from '../core/firestore_client';
import {
Bound,
Direction,
Expand Down Expand Up @@ -101,7 +114,7 @@ import {
validateSetOptions,
valueDescription
} from '../util/input_validation';
import { setLogLevel as setClientLogLevel, logWarn } from '../util/log';
import { logWarn, setLogLevel as setClientLogLevel } from '../util/log';
import { AutoId } from '../util/misc';
import { Deferred } from '../util/promise';
import { FieldPath as ExternalFieldPath } from './field_path';
Expand Down Expand Up @@ -461,12 +474,12 @@ export class Firestore implements PublicFirestore, FirebaseService {

enableNetwork(): Promise<void> {
this.ensureClientConfigured();
return this._firestoreClient!.enableNetwork();
return firestoreClientEnableNetwork(this._firestoreClient!);
}

disableNetwork(): Promise<void> {
this.ensureClientConfigured();
return this._firestoreClient!.disableNetwork();
return firestoreClientDisableNetwork(this._firestoreClient!);
}

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

waitForPendingWrites(): Promise<void> {
this.ensureClientConfigured();
return this._firestoreClient!.waitForPendingWrites();
return firestoreClientWaitForPendingWrites(this._firestoreClient!);
}

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

if (isPartialObserver(arg)) {
return this._firestoreClient!.addSnapshotsInSyncListener(
return firestoreClientAddSnapshotsInSyncListener(
this._firestoreClient!,
arg as PartialObserver<void>
);
} else {
const observer: PartialObserver<void> = {
next: arg as () => void
};
return this._firestoreClient!.addSnapshotsInSyncListener(observer);
return firestoreClientAddSnapshotsInSyncListener(
this._firestoreClient!,
observer
);
}
}

Expand Down Expand Up @@ -676,7 +693,9 @@ export class Firestore implements PublicFirestore, FirebaseService {
runTransaction<T>(
updateFunction: (transaction: PublicTransaction) => Promise<T>
): Promise<T> {
return this.ensureClientConfigured().transaction(
this.ensureClientConfigured();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assign to a temporary rather than referring to this._firestoreClient!?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return firestoreClientTransaction(
this._firestoreClient!,
(transaction: InternalTransaction) => {
return updateFunction(new Transaction(this, transaction));
}
Expand All @@ -685,7 +704,6 @@ export class Firestore implements PublicFirestore, FirebaseService {

batch(): PublicWriteBatch {
this.ensureClientConfigured();

return new WriteBatch(this);
}

Expand Down Expand Up @@ -966,7 +984,8 @@ export class WriteBatch implements PublicWriteBatch {
this.verifyNotCommitted();
this._committed = true;
if (this._mutations.length > 0) {
return this._firestore.ensureClientConfigured().write(this._mutations);
const firestoreClient = this._firestore.ensureClientConfigured();
return firestoreClientWrite(firestoreClient, this._mutations);
}

return Promise.resolve();
Expand Down Expand Up @@ -1080,7 +1099,8 @@ export class DocumentReference<T = DocumentData>
this._converter !== null,
options
);
return this._firestoreClient.write(
return firestoreClientWrite(
this._firestoreClient,
parsed.toMutations(this._key, Precondition.none())
);
}
Expand Down Expand Up @@ -1119,13 +1139,14 @@ export class DocumentReference<T = DocumentData>
);
}

return this._firestoreClient.write(
return firestoreClientWrite(
this._firestoreClient,
parsed.toMutations(this._key, Precondition.exists(true))
);
}

delete(): Promise<void> {
return this._firestoreClient.write([
return firestoreClientWrite(this._firestoreClient, [
new DeleteMutation(this._key, Precondition.none())
]);
}
Expand Down Expand Up @@ -1185,7 +1206,8 @@ export class DocumentReference<T = DocumentData>
complete: args[currArg + 2] as CompleteFn
};

return this._firestoreClient.listen(
return firestoreClientListen(
this._firestoreClient,
newQueryForPath(this._key.path),
internalOptions,
observer
Expand All @@ -1195,23 +1217,26 @@ export class DocumentReference<T = DocumentData>
get(options?: GetOptions): Promise<PublicDocumentSnapshot<T>> {
const firestoreClient = this.firestore.ensureClientConfigured();
if (options && options.source === 'cache') {
return firestoreClient
.getDocumentFromLocalCache(this._key)
.then(
doc =>
new DocumentSnapshot(
this.firestore,
this._key,
doc,
/*fromCache=*/ true,
doc instanceof Document ? doc.hasLocalMutations : false,
this._converter
)
);
return firestoreClientGetDocumentFromLocalCache(
firestoreClient,
this._key
).then(
doc =>
new DocumentSnapshot(
this.firestore,
this._key,
doc,
/*fromCache=*/ true,
doc instanceof Document ? doc.hasLocalMutations : false,
this._converter
)
);
} else {
return firestoreClient
.getDocumentViaSnapshotListener(this._key, options)
.then(snapshot => this._convertToDocSnapshot(snapshot));
return firestoreClientGetDocumentViaSnapshotListener(
firestoreClient,
this._key,
options
).then(snapshot => this._convertToDocSnapshot(snapshot));
}
}

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

validateHasExplicitOrderByForLimitToLast(this._query);
const firestoreClient = this.firestore.ensureClientConfigured();
return firestoreClient.listen(this._query, options, observer);
return firestoreClientListen(
firestoreClient,
this._query,
options,
observer
);
}

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

const firestoreClient = this.firestore.ensureClientConfigured();
return (options && options.source === 'cache'
? firestoreClient.getDocumentsFromLocalCache(this._query)
: firestoreClient.getDocumentsViaSnapshotListener(this._query, options)
? firestoreClientGetDocumentsFromLocalCache(firestoreClient, this._query)
: firestoreClientGetDocumentsViaSnapshotListener(
firestoreClient,
this._query,
options
)
).then(
snap =>
new QuerySnapshot(this.firestore, this._query, snap, this._converter)
Expand Down
Loading