Skip to content

Commit a5f73fb

Browse files
committed
review feedback
1 parent e2b769a commit a5f73fb

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

packages/firebase/index.d.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,37 @@ declare namespace firebase.firestore {
11121112
readonly merge?: boolean;
11131113
}
11141114

1115+
/**
1116+
* An options object that configures the behavior of `get()` calls on
1117+
* `DocumentReference` and `Query`. By providing a `GetOptions` object, these
1118+
* methods can be configured to fetch results only from the server, only from
1119+
* the local cache or attempt to fetch results from the server and fall back to
1120+
* the cache (which is the default).
1121+
*/
1122+
export interface GetOptions {
1123+
/**
1124+
* Describes whether we should get from server or cache.
1125+
*
1126+
* Setting to 'default' (or not setting at all), causes Firestore to try to
1127+
* retrieve an up-to-date (server-retrieved) snapshot, but fall back to
1128+
* returning cached data if the server can't be reached.
1129+
*
1130+
* Setting to 'server' causes Firestore to avoid the cache, generating an
1131+
* error if the server cannot be reached. Note that the cache will still be
1132+
* updated if the server request succeeds. Also note that latency-compensation
1133+
* still takes effect, so any pending write operations will be visible in the
1134+
* returned data (merged into the server-provided data).
1135+
*
1136+
* Setting to 'cache' causes Firestore to immediately return a value from the
1137+
* cache, ignoring the server completely (implying that the returned value
1138+
* may be stale with respect to the value on the server.) If there is no data
1139+
* in the cache to satisfy the `get()` call, `DocumentReference.get()` will
1140+
* return an error and `QuerySnapshot.get()` will return an empty
1141+
* `QuerySnapshot` with no documents.
1142+
*/
1143+
readonly source?: 'default' | 'server' | 'cache';
1144+
}
1145+
11151146
/**
11161147
* A `DocumentReference` refers to a document location in a Firestore database
11171148
* and can be used to write, read, or listen to the location. The document at
@@ -1213,14 +1244,16 @@ declare namespace firebase.firestore {
12131244
/**
12141245
* Reads the document referred to by this `DocumentReference`.
12151246
*
1216-
* Note: get() attempts to provide up-to-date data when possible by waiting
1217-
* for data from the server, but it may return cached data or fail if you
1218-
* are offline and the server cannot be reached.
1247+
* Note: By default, get() attempts to provide up-to-date data when possible
1248+
* by waiting for data from the server, but it may return cached data or fail
1249+
* if you are offline and the server cannot be reached. This behavior can be
1250+
* altered via the `GetOptions` parameter.
12191251
*
1252+
* @param options An object to configure the get behavior.
12201253
* @return A Promise resolved with a DocumentSnapshot containing the
12211254
* current document contents.
12221255
*/
1223-
get(): Promise<DocumentSnapshot>;
1256+
get(options?: GetOptions): Promise<DocumentSnapshot>;
12241257

12251258
/**
12261259
* Attaches a listener for DocumentSnapshot events. You may either pass
@@ -1598,9 +1631,15 @@ declare namespace firebase.firestore {
15981631
/**
15991632
* Executes the query and returns the results as a QuerySnapshot.
16001633
*
1634+
* Note: By default, get() attempts to provide up-to-date data when possible
1635+
* by waiting for data from the server, but it may return cached data or fail
1636+
* if you are offline and the server cannot be reached. This behavior can be
1637+
* altered via the `GetOptions` parameter.
1638+
*
1639+
* @param options An object to configure the get behavior.
16011640
* @return A Promise that will be resolved with the results of the Query.
16021641
*/
1603-
get(): Promise<QuerySnapshot>;
1642+
get(options?: GetOptions): Promise<QuerySnapshot>;
16041643

16051644
/**
16061645
* Attaches a listener for QuerySnapshot events. You may either pass

packages/firestore/src/api/database.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,13 +1028,13 @@ export class DocumentReference implements firestore.DocumentReference {
10281028
);
10291029
}, reject);
10301030
} else {
1031-
this.getViaSnapshotListener_(resolve, reject, options);
1031+
this.getViaSnapshotListener(resolve, reject, options);
10321032
}
10331033
}
10341034
);
10351035
}
10361036

1037-
private getViaSnapshotListener_(
1037+
private getViaSnapshotListener(
10381038
resolve: Resolver<firestore.DocumentSnapshot>,
10391039
reject: Rejecter,
10401040
options?: firestore.GetOptions
@@ -1061,7 +1061,7 @@ export class DocumentReference implements firestore.DocumentReference {
10611061
// if the document doesn't exist.
10621062
reject(
10631063
new FirestoreError(
1064-
Code.ABORTED,
1064+
Code.UNAVAILABLE,
10651065
'Failed to get document because the client is ' + 'offline.'
10661066
)
10671067
);
@@ -1076,7 +1076,7 @@ export class DocumentReference implements firestore.DocumentReference {
10761076
Code.UNAVAILABLE,
10771077
'Failed to get document from server. (However, this ' +
10781078
'document does exist in the local cache. Run again ' +
1079-
'without setting source to FIRGetSourceServer to ' +
1079+
'without setting source to "server" to ' +
10801080
'retrieve the cached document.)'
10811081
)
10821082
);
@@ -1679,13 +1679,13 @@ export class Query implements firestore.Query {
16791679
resolve(new QuerySnapshot(this.firestore, this._query, viewSnap));
16801680
}, reject);
16811681
} else {
1682-
this.getViaSnapshotListener_(resolve, reject, options);
1682+
this.getViaSnapshotListener(resolve, reject, options);
16831683
}
16841684
}
16851685
);
16861686
}
16871687

1688-
private getViaSnapshotListener_(
1688+
private getViaSnapshotListener(
16891689
resolve: Resolver<firestore.QuerySnapshot>,
16901690
reject: Rejecter,
16911691
options?: firestore.GetOptions

packages/firestore/src/core/firestore_client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { debug } from '../util/log';
4949
import { Deferred } from '../util/promise';
5050

5151
import { DatabaseId, DatabaseInfo } from './database_info';
52-
import { Query as InternalQuery } from './query';
52+
import { Query } from './query';
5353
import { Transaction } from './transaction';
5454
import { OnlineState } from './types';
5555
import { ViewSnapshot } from './view_snapshot';
@@ -348,7 +348,7 @@ export class FirestoreClient {
348348
}
349349

350350
listen(
351-
query: InternalQuery,
351+
query: Query,
352352
observer: Observer<ViewSnapshot>,
353353
options: ListenOptions
354354
): QueryListener {
@@ -385,7 +385,7 @@ export class FirestoreClient {
385385
});
386386
}
387387

388-
getDocumentsFromLocalCache(query: InternalQuery): Promise<ViewSnapshot> {
388+
getDocumentsFromLocalCache(query: Query): Promise<ViewSnapshot> {
389389
return this.asyncQueue
390390
.enqueue(() => {
391391
return this.localStore.executeQuery(query);

packages/firestore/test/integration/util/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function toDataArray(
9090
export function toDataMap(
9191
docSet: firestore.QuerySnapshot
9292
): { [field: string]: firestore.DocumentData } {
93-
let docsData = {};
93+
const docsData = {};
9494
docSet.forEach(doc => {
9595
docsData[doc.id] = doc.data();
9696
});

0 commit comments

Comments
 (0)