@@ -307,29 +307,29 @@ export class Repo {
307
307
* The purpose of `getValue` is to return the latest known value
308
308
* satisfying `query`.
309
309
*
310
- * If the client is connected, this method will send a request
311
- * to the server. If the client is not connected, then either:
310
+ * This method will first check for in-memory cached values
311
+ * belonging to active listeners. If they are found, such values
312
+ * are considered to be the most up-to-date.
312
313
*
313
- * 1. The client was once connected, but not anymore.
314
- * 2. The client has never connected, this is the first operation
315
- * this repo is handling.
316
- *
317
- * In case (1), it's possible that the client still has an active
318
- * listener, with cached data. Since this is the latest known
319
- * value satisfying the query, that's what getValue will return.
320
- * If there is no cached data, `getValue` surfaces an "offline"
321
- * error.
322
- *
323
- * In case (2), `getValue` will trigger a time-limited connection
324
- * attempt. If the client is unable to connect to the server, it
325
- * will surface an "offline" error because there cannot be any
326
- * cached data. On the other hand, if the client is able to connect,
327
- * `getValue` will return the server's value for the query, if one
328
- * exists.
314
+ * If the client is not connected, this method will try to
315
+ * establish a connection and request the value for `query`. If
316
+ * the client is not able to retrieve the query result, it reports
317
+ * an error.
329
318
*
330
319
* @param query - The query to surface a value for.
331
320
*/
332
321
getValue ( query : Query ) : Promise < DataSnapshot > {
322
+ // Only active queries are cached. There is no persisted cache.
323
+ const cached = this . serverSyncTree_ . calcCompleteEventCache ( query . path ) ;
324
+ if ( ! cached . isEmpty ( ) ) {
325
+ return Promise . resolve (
326
+ new DataSnapshot (
327
+ cached ,
328
+ query . getRef ( ) ,
329
+ query . getQueryParams ( ) . getIndex ( )
330
+ )
331
+ ) ;
332
+ }
333
333
return this . server_ . get ( query ) . then (
334
334
payload => {
335
335
const node = nodeFromJSON ( payload as string ) ;
@@ -347,22 +347,7 @@ export class Repo {
347
347
) ;
348
348
} ,
349
349
err => {
350
- this . log_ (
351
- 'get for query ' +
352
- stringify ( query ) +
353
- ' falling back to cache after error: ' +
354
- err
355
- ) ;
356
- const cached = this . serverSyncTree_ . calcCompleteEventCache ( query . path ) ;
357
- if ( ! cached . isEmpty ( ) ) {
358
- return Promise . resolve (
359
- new DataSnapshot (
360
- cached ,
361
- query . getRef ( ) ,
362
- query . getQueryParams ( ) . getIndex ( )
363
- )
364
- ) ;
365
- }
350
+ this . log_ ( 'get for query ' + stringify ( query ) + ' failed: ' + err ) ;
366
351
return Promise . reject ( new Error ( err as string ) ) ;
367
352
}
368
353
) ;
0 commit comments