Skip to content

Commit 94986e6

Browse files
Refactor some FirestoreClient methods (#3507)
1 parent 6f283d5 commit 94986e6

File tree

3 files changed

+156
-124
lines changed

3 files changed

+156
-124
lines changed

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import { cast } from '../../../lite/src/api/util';
3232
import { DocumentSnapshot, QuerySnapshot } from './snapshot';
3333
import {
3434
applyFirestoreDataConverter,
35-
getDocsViaSnapshotListener,
36-
getDocViaSnapshotListener,
3735
SnapshotMetadata,
3836
validateHasExplicitOrderByForLimitToLast
3937
} from '../../../src/api/database';
@@ -65,8 +63,7 @@ export function getDoc<T>(
6563
const ref = cast<DocumentReference<T>>(reference, DocumentReference);
6664
const firestore = cast<Firestore>(ref.firestore, Firestore);
6765
return getFirestoreClient(firestore).then(async firestoreClient => {
68-
const viewSnapshot = await getDocViaSnapshotListener(
69-
firestoreClient,
66+
const viewSnapshot = await firestoreClient.getDocumentViaSnapshotListener(
7067
ref._key
7168
);
7269
return convertToDocSnapshot(firestore, ref, viewSnapshot);
@@ -101,8 +98,7 @@ export function getDocFromServer<T>(
10198
const ref = cast<DocumentReference<T>>(reference, DocumentReference);
10299
const firestore = cast<Firestore>(ref.firestore, Firestore);
103100
return getFirestoreClient(firestore).then(async firestoreClient => {
104-
const viewSnapshot = await getDocViaSnapshotListener(
105-
firestoreClient,
101+
const viewSnapshot = await firestoreClient.getDocumentViaSnapshotListener(
106102
ref._key,
107103
{ source: 'server' }
108104
);
@@ -118,8 +114,7 @@ export function getDocs<T>(
118114

119115
validateHasExplicitOrderByForLimitToLast(internalQuery._query);
120116
return getFirestoreClient(firestore).then(async firestoreClient => {
121-
const snapshot = await getDocsViaSnapshotListener(
122-
firestoreClient,
117+
const snapshot = await firestoreClient.getDocumentsViaSnapshotListener(
123118
internalQuery._query
124119
);
125120
return new QuerySnapshot(firestore, internalQuery, snapshot);
@@ -145,8 +140,7 @@ export function getDocsFromServer<T>(
145140
const internalQuery = cast<Query<T>>(query, Query);
146141
const firestore = cast<Firestore>(query.firestore, Firestore);
147142
return getFirestoreClient(firestore).then(async firestoreClient => {
148-
const snapshot = await getDocsViaSnapshotListener(
149-
firestoreClient,
143+
const snapshot = await firestoreClient.getDocumentsViaSnapshotListener(
150144
internalQuery._query,
151145
{ source: 'server' }
152146
);

packages/firestore/src/api/database.ts

Lines changed: 6 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ import { getLogLevel, logError, LogLevel, setLogLevel } from '../util/log';
8383
import { AutoId } from '../util/misc';
8484
import { Deferred } from '../util/promise';
8585
import { FieldPath as ExternalFieldPath } from './field_path';
86-
8786
import {
8887
CredentialsProvider,
8988
CredentialsSettings,
@@ -1234,9 +1233,9 @@ export class DocumentReference<T = firestore.DocumentData>
12341233
validateBetweenNumberOfArgs('DocumentReference.get', arguments, 0, 1);
12351234
validateGetOptions('DocumentReference.get', options);
12361235

1236+
const firestoreClient = this.firestore.ensureClientConfigured();
12371237
if (options && options.source === 'cache') {
1238-
return this.firestore
1239-
.ensureClientConfigured()
1238+
return firestoreClient
12401239
.getDocumentFromLocalCache(this._key)
12411240
.then(
12421241
doc =>
@@ -1250,11 +1249,9 @@ export class DocumentReference<T = firestore.DocumentData>
12501249
)
12511250
);
12521251
} else {
1253-
return getDocViaSnapshotListener(
1254-
this._firestoreClient,
1255-
this._key,
1256-
options
1257-
).then(snapshot => this._convertToDocSnapshot(snapshot));
1252+
return firestoreClient
1253+
.getDocumentViaSnapshotListener(this._key, options)
1254+
.then(snapshot => this._convertToDocSnapshot(snapshot));
12581255
}
12591256
}
12601257

@@ -1286,68 +1283,6 @@ export class DocumentReference<T = firestore.DocumentData>
12861283
}
12871284
}
12881285

1289-
/**
1290-
* Retrieves a latency-compensated document from the backend via a
1291-
* SnapshotListener.
1292-
*/
1293-
export function getDocViaSnapshotListener(
1294-
firestoreClient: FirestoreClient,
1295-
key: DocumentKey,
1296-
options?: firestore.GetOptions
1297-
): Promise<ViewSnapshot> {
1298-
const result = new Deferred<ViewSnapshot>();
1299-
const unlisten = firestoreClient.listen(
1300-
newQueryForPath(key.path),
1301-
{
1302-
includeMetadataChanges: true,
1303-
waitForSyncWhenOnline: true
1304-
},
1305-
{
1306-
next: (snap: ViewSnapshot) => {
1307-
// Remove query first before passing event to user to avoid
1308-
// user actions affecting the now stale query.
1309-
unlisten();
1310-
1311-
const exists = snap.docs.has(key);
1312-
if (!exists && snap.fromCache) {
1313-
// TODO(dimond): If we're online and the document doesn't
1314-
// exist then we resolve with a doc.exists set to false. If
1315-
// we're offline however, we reject the Promise in this
1316-
// case. Two options: 1) Cache the negative response from
1317-
// the server so we can deliver that even when you're
1318-
// offline 2) Actually reject the Promise in the online case
1319-
// if the document doesn't exist.
1320-
result.reject(
1321-
new FirestoreError(
1322-
Code.UNAVAILABLE,
1323-
'Failed to get document because the client is ' + 'offline.'
1324-
)
1325-
);
1326-
} else if (
1327-
exists &&
1328-
snap.fromCache &&
1329-
options &&
1330-
options.source === 'server'
1331-
) {
1332-
result.reject(
1333-
new FirestoreError(
1334-
Code.UNAVAILABLE,
1335-
'Failed to get document from server. (However, this ' +
1336-
'document does exist in the local cache. Run again ' +
1337-
'without setting source to "server" to ' +
1338-
'retrieve the cached document.)'
1339-
)
1340-
);
1341-
} else {
1342-
result.resolve(snap);
1343-
}
1344-
},
1345-
error: e => result.reject(e)
1346-
}
1347-
);
1348-
return result.promise;
1349-
}
1350-
13511286
export class SnapshotMetadata implements firestore.SnapshotMetadata {
13521287
constructor(
13531288
readonly hasPendingWrites: boolean,
@@ -2170,56 +2105,14 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
21702105
const firestoreClient = this.firestore.ensureClientConfigured();
21712106
return (options && options.source === 'cache'
21722107
? firestoreClient.getDocumentsFromLocalCache(this._query)
2173-
: getDocsViaSnapshotListener(firestoreClient, this._query, options)
2108+
: firestoreClient.getDocumentsViaSnapshotListener(this._query, options)
21742109
).then(
21752110
snap =>
21762111
new QuerySnapshot(this.firestore, this._query, snap, this._converter)
21772112
);
21782113
}
21792114
}
21802115

2181-
/**
2182-
* Retrieves a latency-compensated query snapshot from the backend via a
2183-
* SnapshotListener.
2184-
*/
2185-
export function getDocsViaSnapshotListener(
2186-
firestore: FirestoreClient,
2187-
query: InternalQuery,
2188-
options?: firestore.GetOptions
2189-
): Promise<ViewSnapshot> {
2190-
const result = new Deferred<ViewSnapshot>();
2191-
const unlisten = firestore.listen(
2192-
query,
2193-
{
2194-
includeMetadataChanges: true,
2195-
waitForSyncWhenOnline: true
2196-
},
2197-
{
2198-
next: snapshot => {
2199-
// Remove query first before passing event to user to avoid
2200-
// user actions affecting the now stale query.
2201-
unlisten();
2202-
2203-
if (snapshot.fromCache && options && options.source === 'server') {
2204-
result.reject(
2205-
new FirestoreError(
2206-
Code.UNAVAILABLE,
2207-
'Failed to get documents from server. (However, these ' +
2208-
'documents may exist in the local cache. Run again ' +
2209-
'without setting source to "server" to ' +
2210-
'retrieve the cached documents.)'
2211-
)
2212-
);
2213-
} else {
2214-
result.resolve(snapshot);
2215-
}
2216-
},
2217-
error: e => result.reject(e)
2218-
}
2219-
);
2220-
return result.promise;
2221-
}
2222-
22232116
export class QuerySnapshot<T = firestore.DocumentData>
22242117
implements firestore.QuerySnapshot<T> {
22252118
private _cachedChanges: Array<firestore.DocumentChange<T>> | null = null;

0 commit comments

Comments
 (0)