Skip to content

Commit

Permalink
Teardown watcher when out of focus (#4979)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde authored and ericpgreen2 committed May 30, 2024
1 parent 79c43e7 commit fb83c15
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
void fileArtifacts.init(queryClient, instanceId);
return () => {
fileWatcher.cancel();
resourceWatcher.cancel();
fileWatcher.close();
resourceWatcher.close();
stopJavascriptErrorListeners?.();
};
});
Expand Down
16 changes: 12 additions & 4 deletions web-common/src/runtime-client/watch-request-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class WatchRequestClient<Res extends WatchResponse> {
["response", []],
["reconnect", []],
]);
private closed = false;

public on<K extends keyof EventMap<Res>>(
event: K,
Expand All @@ -61,18 +62,19 @@ export class WatchRequestClient<Res extends WatchResponse> {
this.listen().catch(console.error);
}

public cancel() {
this.controller?.abort();
this.stream = this.controller = undefined;
public close() {
this.closed = true;
this.cancel();
}

public throttle() {
this.outOfFocusThrottler.throttle(() => {
this.cancel();
this.close();
});
}

public async reconnect() {
this.closed = true;
clearTimeout(this.reconnectTimeout);

if (this.outOfFocusThrottler.isThrottling()) {
Expand All @@ -94,6 +96,11 @@ export class WatchRequestClient<Res extends WatchResponse> {
this.listen(true).catch(console.error);
}

private cancel() {
this.controller?.abort();
this.stream = this.controller = undefined;
}

private async listen(reconnect = false) {
clearTimeout(this.reconnectTimeout);

Expand Down Expand Up @@ -124,6 +131,7 @@ export class WatchRequestClient<Res extends WatchResponse> {
clearTimeout(this.retryTimeout);

if (this.controller) this.cancel();
if (this.closed) return;
this.reconnect().catch((e) => {
throw new Error(e);
});
Expand Down

2 comments on commit fb83c15

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://ui.rilldata.com as production
🚀 Deployed on https://6657de863975c97ba0ea3da1--rill-ui.netlify.app

Please sign in to comment.