Skip to content

Commit a68deb2

Browse files
Establish connection only when needed
This delays the first connection attempt until we have information to send to the backend.
1 parent 6e35e53 commit a68deb2

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

packages/database/src/core/PersistentConnection.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ export class PersistentConnection extends ServerActions {
165165
'Auth override specified in options, but not supported on non Node.js platforms'
166166
);
167167
}
168-
this.scheduleConnect_(0);
169168

170169
VisibilityMonitor.getInstance().on('visible', this.onVisible_, this);
171170

@@ -194,6 +193,8 @@ export class PersistentConnection extends ServerActions {
194193
}
195194

196195
get(query: QueryContext): Promise<string> {
196+
this.initConnection_();
197+
197198
const deferred = new Deferred<string>();
198199
const request = {
199200
p: query._path.toString(),
@@ -249,6 +250,8 @@ export class PersistentConnection extends ServerActions {
249250
tag: number | null,
250251
onComplete: (a: string, b: unknown) => void
251252
) {
253+
this.initConnection_();
254+
252255
const queryId = query._queryIdentifier;
253256
const pathString = query._path.toString();
254257
this.log_('Listen called for ' + pathString + ' ' + queryId);
@@ -490,6 +493,8 @@ export class PersistentConnection extends ServerActions {
490493
data: unknown,
491494
onComplete?: (a: string, b: string) => void
492495
) {
496+
this.initConnection_();
497+
493498
if (this.connected_) {
494499
this.sendOnDisconnect_('o', pathString, data, onComplete);
495500
} else {
@@ -509,6 +514,7 @@ export class PersistentConnection extends ServerActions {
509514
if (this.connected_) {
510515
this.sendOnDisconnect_('om', pathString, data, onComplete);
511516
} else {
517+
this.initConnection_();
512518
this.onDisconnectRequestQueue_.push({
513519
pathString,
514520
action: 'om',
@@ -521,6 +527,8 @@ export class PersistentConnection extends ServerActions {
521527
pathString: string,
522528
onComplete?: (a: string, b: string) => void
523529
) {
530+
this.initConnection_();
531+
524532
if (this.connected_) {
525533
this.sendOnDisconnect_('oc', pathString, null, onComplete);
526534
} else {
@@ -576,6 +584,8 @@ export class PersistentConnection extends ServerActions {
576584
onComplete: (a: string, b: string | null) => void,
577585
hash?: string
578586
) {
587+
this.initConnection_();
588+
579589
const request: { [k: string]: unknown } = {
580590
/*path*/ p: pathString,
581591
/*data*/ d: data
@@ -737,6 +747,12 @@ export class PersistentConnection extends ServerActions {
737747
}, Math.floor(timeout)) as any;
738748
}
739749

750+
private initConnection_() {
751+
if (!this.realtime_ && this.firstConnection_) {
752+
this.scheduleConnect_(0);
753+
}
754+
}
755+
740756
private onVisible_(visible: boolean) {
741757
// NOTE: Tabbing away and back to a window will defeat our reconnect backoff, but I think that's fine.
742758
if (

0 commit comments

Comments
 (0)