Terminate gql subscription on server address change

Otherwise, the subscription is still active for the old server instead of connecting to the new server
This commit is contained in:
schroda
2024-08-25 22:34:01 +02:00
parent 114f9fd34f
commit 08866ace3d
4 changed files with 23 additions and 12 deletions

View File

@@ -428,6 +428,7 @@ export class RequestManager {
public reset(): void {
this.graphQLClient.client.resetStore();
this.graphQLClient.terminateSubscriptions();
this.cache.clear();
this.imageQueue.clear();
}

View File

@@ -9,14 +9,10 @@
import { AppStorage } from '@/util/AppStorage.ts';
export abstract class BaseClient<Client, ClientConfig, Fetcher> {
protected client!: Client;
protected abstract client: Client;
public abstract readonly fetcher: Fetcher;
constructor() {
this.createClient();
}
public getBaseUrl(): string {
const { hostname, port, protocol } = window.location;
@@ -25,7 +21,5 @@ export abstract class BaseClient<Client, ClientConfig, Fetcher> {
return AppStorage.local.getItemParsed('serverBaseURL', `${protocol}//${hostname}:${inferredPort}`);
}
protected abstract createClient(): void;
public abstract updateConfig(config: Partial<ClientConfig>): void;
}

View File

@@ -136,14 +136,24 @@ export class GraphQLClient extends BaseClient<
> {
readonly fetcher = null;
public declare client: ApolloClient<NormalizedCacheObject>;
public client!: ApolloClient<NormalizedCacheObject>;
private wsClient!: Client;
constructor() {
super();
this.createClient();
}
public override getBaseUrl(): string {
return `${super.getBaseUrl()}/api/graphql`;
}
public terminateSubscriptions(): void {
this.wsClient.terminate();
}
private createUploadLink() {
return createUploadLink({ uri: () => this.getBaseUrl(), credentials: 'include' });
}
@@ -182,13 +192,11 @@ export class GraphQLClient extends BaseClient<
});
const checkHeartbeatInterval = heartbeatInterval + 1000 * 10;
// for some reason "this.wsClient" is undefined in the "setInterval" callback
const { wsClient } = this;
setInterval(() => {
const isHeartbeatMissing = Date.now() - lastHeartbeat > checkHeartbeatInterval * 1.1;
if (isHeartbeatMissing) {
// force a reconnect
wsClient.terminate();
this.wsClient.terminate();
}
}, checkHeartbeatInterval);

View File

@@ -27,6 +27,8 @@ export class RestClient
extends BaseClient<typeof fetch, RequestInit, (url: string, data: any) => Promise<Response>>
implements IRestClient
{
protected client!: typeof fetch;
private config: RequestInit = {
credentials: 'include',
};
@@ -78,7 +80,13 @@ export class RestClient
return result;
};
protected override createClient(): void {
constructor() {
super();
this.createClient();
}
private createClient(): void {
this.client = fetch.bind(window);
}