Skip to content

Commit f6dcb38

Browse files
committed
review
1 parent c0f627c commit f6dcb38

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

packages/database/src/core/PersistentConnection.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ interface OutstandingPut {
7878
}
7979

8080
interface OutstandingGet {
81-
action: string;
8281
request: object;
8382
onComplete: (response: { [k: string]: unknown }) => void;
8483
}
@@ -223,18 +222,22 @@ export class PersistentConnection extends ServerActions {
223222
const index = this.outstandingGets_.length - 1;
224223

225224
if (!this.connected_) {
226-
const self = this;
227225
setTimeout(() => {
228-
const get = self.outstandingGets_[index];
226+
const get = this.outstandingGets_[index];
229227
if (get === undefined || outstandingGet !== get) {
230228
return;
231229
}
232-
delete self.outstandingGets_[index];
233-
self.outstandingGetCount_--;
234-
if (self.outstandingGetCount_ === 0) {
235-
self.outstandingGets_ = [];
230+
delete this.outstandingGets_[index];
231+
this.outstandingGetCount_--;
232+
if (this.outstandingGetCount_ === 0) {
233+
this.outstandingGets_ = [];
236234
}
237-
self.log_('get ' + index + ' timed out on connection');
235+
this.log_('get ' + index + ' timed out on connection');
236+
// It's possible that we were once able to reach the server,
237+
// but not anymore. If that's the case, the client could have
238+
// an active listener with cached data for this get. The Repo
239+
// will try to retrieve this cached data if we can't connect
240+
// here.
238241
deferred.reject(new Error('Client is offline.'));
239242
}, GET_CONNECT_TIMEOUT);
240243
}
@@ -263,7 +266,7 @@ export class PersistentConnection extends ServerActions {
263266
}
264267
assert(
265268
query.getQueryParams().isDefault() ||
266-
!query.getQueryParams().loadsAllData(),
269+
!query.getQueryParams().loadsAllData(),
267270
'listen() called for non-default but complete query'
268271
);
269272
assert(
@@ -285,20 +288,16 @@ export class PersistentConnection extends ServerActions {
285288

286289
private sendGet_(index: number) {
287290
const get = this.outstandingGets_[index];
288-
this.sendRequest(
289-
get.action,
290-
get.request,
291-
(message: { [k: string]: unknown }) => {
292-
delete this.outstandingGets_[index];
293-
this.outstandingGetCount_--;
294-
if (this.outstandingGetCount_ === 0) {
295-
this.outstandingGets_ = [];
296-
}
297-
if (get.onComplete) {
298-
get.onComplete(message);
299-
}
291+
this.sendRequest('g', get.request, (message: { [k: string]: unknown }) => {
292+
delete this.outstandingGets_[index];
293+
this.outstandingGetCount_--;
294+
if (this.outstandingGetCount_ === 0) {
295+
this.outstandingGets_ = [];
300296
}
301-
);
297+
if (get.onComplete) {
298+
get.onComplete(message);
299+
}
300+
});
302301
}
303302

304303
private sendListen_(listenSpec: ListenSpec) {
@@ -353,8 +352,8 @@ export class PersistentConnection extends ServerActions {
353352
const indexPath = query.path.toString();
354353
warn(
355354
`Using an unspecified index. Your data will be downloaded and ` +
356-
`filtered on the client. Consider adding ${indexSpec} at ` +
357-
`${indexPath} to your security rules for better performance.`
355+
`filtered on the client. Consider adding ${indexSpec} at ` +
356+
`${indexPath} to your security rules for better performance.`
358357
);
359358
}
360359
}
@@ -372,7 +371,7 @@ export class PersistentConnection extends ServerActions {
372371
//If we're connected we want to let the server know to unauthenticate us. If we're not connected, simply delete
373372
//the credential so we dont become authenticated next time we connect.
374373
if (this.connected_) {
375-
this.sendRequest('unauth', {}, () => { });
374+
this.sendRequest('unauth', {}, () => {});
376375
}
377376
}
378377

@@ -436,7 +435,7 @@ export class PersistentConnection extends ServerActions {
436435

437436
assert(
438437
query.getQueryParams().isDefault() ||
439-
!query.getQueryParams().loadsAllData(),
438+
!query.getQueryParams().loadsAllData(),
440439
'unlisten() called for non-default but complete query'
441440
);
442441
const listen = this.removeListen_(pathString, queryId);
@@ -694,8 +693,8 @@ export class PersistentConnection extends ServerActions {
694693
} else {
695694
error(
696695
'Unrecognized action received from server: ' +
697-
stringify(action) +
698-
'\nAre you using the latest client?'
696+
stringify(action) +
697+
'\nAre you using the latest client?'
699698
);
700699
}
701700
}

packages/database/src/core/Repo.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,32 @@ export class Repo {
297297
return this.nextWriteId_++;
298298
}
299299

300+
/**
301+
* The purpose of `getValue` is to return the latest known value
302+
* satisfying `query`.
303+
*
304+
* If the client is connected, this method will send a request
305+
* to the server. If the client is not connected, then either:
306+
*
307+
* 1. The client was once connected, but not anymore.
308+
* 2. The client has never connected, this is the first operation
309+
* this repo is handling.
310+
*
311+
* In case (1), it's possible that the client still has an active
312+
* listener, with cached data. Since this is the latest known
313+
* value satisfying the query, that's what getValue will return.
314+
* If there is no cached data, `getValue` surfaces an "offline"
315+
* error.
316+
*
317+
* In case (2), `getValue` will trigger a time-limited connection
318+
* attempt. If the client is unable to connect to the server, the
319+
* will surface an "offline" error because there cannot be any
320+
* cached data. On the other hand, if the client is able to connect,
321+
* `getValue` will return the server's value for the query, if one
322+
* exists.
323+
*
324+
* @param query - The query to surface a value for.
325+
*/
300326
getValue(query: Query): Promise<DataSnapshot> {
301327
return this.server_.get(query).then(
302328
payload => {

0 commit comments

Comments
 (0)