@@ -83,7 +83,6 @@ import { getLogLevel, logError, LogLevel, setLogLevel } from '../util/log';
83
83
import { AutoId } from '../util/misc' ;
84
84
import { Deferred } from '../util/promise' ;
85
85
import { FieldPath as ExternalFieldPath } from './field_path' ;
86
-
87
86
import {
88
87
CredentialsProvider ,
89
88
CredentialsSettings ,
@@ -1234,9 +1233,9 @@ export class DocumentReference<T = firestore.DocumentData>
1234
1233
validateBetweenNumberOfArgs ( 'DocumentReference.get' , arguments , 0 , 1 ) ;
1235
1234
validateGetOptions ( 'DocumentReference.get' , options ) ;
1236
1235
1236
+ const firestoreClient = this . firestore . ensureClientConfigured ( ) ;
1237
1237
if ( options && options . source === 'cache' ) {
1238
- return this . firestore
1239
- . ensureClientConfigured ( )
1238
+ return firestoreClient
1240
1239
. getDocumentFromLocalCache ( this . _key )
1241
1240
. then (
1242
1241
doc =>
@@ -1250,11 +1249,9 @@ export class DocumentReference<T = firestore.DocumentData>
1250
1249
)
1251
1250
) ;
1252
1251
} 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 ) ) ;
1258
1255
}
1259
1256
}
1260
1257
@@ -1286,68 +1283,6 @@ export class DocumentReference<T = firestore.DocumentData>
1286
1283
}
1287
1284
}
1288
1285
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
-
1351
1286
export class SnapshotMetadata implements firestore . SnapshotMetadata {
1352
1287
constructor (
1353
1288
readonly hasPendingWrites : boolean ,
@@ -2170,56 +2105,14 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
2170
2105
const firestoreClient = this . firestore . ensureClientConfigured ( ) ;
2171
2106
return ( options && options . source === 'cache'
2172
2107
? firestoreClient . getDocumentsFromLocalCache ( this . _query )
2173
- : getDocsViaSnapshotListener ( firestoreClient , this . _query , options )
2108
+ : firestoreClient . getDocumentsViaSnapshotListener ( this . _query , options )
2174
2109
) . then (
2175
2110
snap =>
2176
2111
new QuerySnapshot ( this . firestore , this . _query , snap , this . _converter )
2177
2112
) ;
2178
2113
}
2179
2114
}
2180
2115
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
-
2223
2116
export class QuerySnapshot < T = firestore . DocumentData >
2224
2117
implements firestore . QuerySnapshot < T > {
2225
2118
private _cachedChanges : Array < firestore . DocumentChange < T > > | null = null ;
0 commit comments