Skip to content

Commit 04a0fea

Browse files
authored
RTDB get shouldn't send requests for listened-to data (#4299)
1 parent 898bf58 commit 04a0fea

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

.changeset/hip-turtles-travel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/database': patch
3+
---
4+
5+
get()s issued for queries that are being listened to no longer send backend requests.

packages/database/src/core/Repo.ts

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -307,29 +307,29 @@ export class Repo {
307307
* The purpose of `getValue` is to return the latest known value
308308
* satisfying `query`.
309309
*
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.
312313
*
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.
329318
*
330319
* @param query - The query to surface a value for.
331320
*/
332321
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+
}
333333
return this.server_.get(query).then(
334334
payload => {
335335
const node = nodeFromJSON(payload as string);
@@ -347,22 +347,7 @@ export class Repo {
347347
);
348348
},
349349
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);
366351
return Promise.reject(new Error(err as string));
367352
}
368353
);

packages/database/test/query.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3128,6 +3128,12 @@ describe('Query Tests', () => {
31283128
}
31293129
});
31303130

3131+
it('get returns the latest value', async () => {
3132+
const node = getRandomNode() as Reference;
3133+
await node.set({ foo: 'bar' });
3134+
expect(node.get()).to.eventually.equal({ foo: 'bar' });
3135+
});
3136+
31313137
it('get reads from cache if database is not connected', async () => {
31323138
const node = getRandomNode() as Reference;
31333139
const node2 = getFreshRepo(node.path);

0 commit comments

Comments
 (0)