Skip to content

Commit c85a32d

Browse files
Establish connection only when needed (#4894)
This delays the first connection attempt until we have information to send to the backend.
1 parent cd29426 commit c85a32d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

packages/database/src/core/PersistentConnection.ts

+18-1
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 {
@@ -506,6 +511,8 @@ export class PersistentConnection extends ServerActions {
506511
data: unknown,
507512
onComplete?: (a: string, b: string) => void
508513
) {
514+
this.initConnection_();
515+
509516
if (this.connected_) {
510517
this.sendOnDisconnect_('om', pathString, data, onComplete);
511518
} else {
@@ -521,6 +528,8 @@ export class PersistentConnection extends ServerActions {
521528
pathString: string,
522529
onComplete?: (a: string, b: string) => void
523530
) {
531+
this.initConnection_();
532+
524533
if (this.connected_) {
525534
this.sendOnDisconnect_('oc', pathString, null, onComplete);
526535
} else {
@@ -576,6 +585,8 @@ export class PersistentConnection extends ServerActions {
576585
onComplete: (a: string, b: string | null) => void,
577586
hash?: string
578587
) {
588+
this.initConnection_();
589+
579590
const request: { [k: string]: unknown } = {
580591
/*path*/ p: pathString,
581592
/*data*/ d: data
@@ -737,6 +748,12 @@ export class PersistentConnection extends ServerActions {
737748
}, Math.floor(timeout)) as any;
738749
}
739750

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

0 commit comments

Comments
 (0)