Skip to content

Establish connection only when needed #4894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/database/src/core/PersistentConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ export class PersistentConnection extends ServerActions {
'Auth override specified in options, but not supported on non Node.js platforms'
);
}
this.scheduleConnect_(0);

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

Expand Down Expand Up @@ -194,6 +193,8 @@ export class PersistentConnection extends ServerActions {
}

get(query: QueryContext): Promise<string> {
this.initConnection_();

const deferred = new Deferred<string>();
const request = {
p: query._path.toString(),
Expand Down Expand Up @@ -249,6 +250,8 @@ export class PersistentConnection extends ServerActions {
tag: number | null,
onComplete: (a: string, b: unknown) => void
) {
this.initConnection_();

const queryId = query._queryIdentifier;
const pathString = query._path.toString();
this.log_('Listen called for ' + pathString + ' ' + queryId);
Expand Down Expand Up @@ -490,6 +493,8 @@ export class PersistentConnection extends ServerActions {
data: unknown,
onComplete?: (a: string, b: string) => void
) {
this.initConnection_();

if (this.connected_) {
this.sendOnDisconnect_('o', pathString, data, onComplete);
} else {
Expand All @@ -506,6 +511,8 @@ export class PersistentConnection extends ServerActions {
data: unknown,
onComplete?: (a: string, b: string) => void
) {
this.initConnection_();

if (this.connected_) {
this.sendOnDisconnect_('om', pathString, data, onComplete);
} else {
Expand All @@ -521,6 +528,8 @@ export class PersistentConnection extends ServerActions {
pathString: string,
onComplete?: (a: string, b: string) => void
) {
this.initConnection_();

if (this.connected_) {
this.sendOnDisconnect_('oc', pathString, null, onComplete);
} else {
Expand Down Expand Up @@ -576,6 +585,8 @@ export class PersistentConnection extends ServerActions {
onComplete: (a: string, b: string | null) => void,
hash?: string
) {
this.initConnection_();

const request: { [k: string]: unknown } = {
/*path*/ p: pathString,
/*data*/ d: data
Expand Down Expand Up @@ -737,6 +748,12 @@ export class PersistentConnection extends ServerActions {
}, Math.floor(timeout)) as any;
}

private initConnection_() {
if (!this.realtime_ && this.firstConnection_) {
this.scheduleConnect_(0);
}
}

private onVisible_(visible: boolean) {
// NOTE: Tabbing away and back to a window will defeat our reconnect backoff, but I think that's fine.
if (
Expand Down