Add "reset" functions to clients

This commit is contained in:
schroda
2025-11-07 01:25:36 +01:00
parent b062871163
commit 284c194ac3
3 changed files with 37 additions and 12 deletions

View File

@@ -473,8 +473,10 @@ export class RequestManager {
AuthManager.setAuthRequired(null); AuthManager.setAuthRequired(null);
AuthManager.setAuthInitialized(false); AuthManager.setAuthInitialized(false);
AuthManager.removeTokens(); AuthManager.removeTokens();
this.graphQLClient.client.resetStore();
this.graphQLClient.terminateSubscriptions(); this.graphQLClient.reset();
this.restClient.reset();
this.cache.clear(); this.cache.clear();
this.imageQueue.clear(); this.imageQueue.clear();
} }

View File

@@ -32,6 +32,11 @@ export abstract class BaseClient<Client, ClientConfig, Fetcher> {
protected requestQueue: QueuedRequest[] = []; protected requestQueue: QueuedRequest[] = [];
public reset(): void {
BaseClient.activeTokenRefreshPromise = null;
this.clearQueue(new Error('Client reset'));
}
public static setTokenRefreshCompleteCallback(callback: (() => void) | null): void { public static setTokenRefreshCompleteCallback(callback: (() => void) | null): void {
BaseClient.onTokenRefreshComplete = callback; BaseClient.onTokenRefreshComplete = callback;
} }

View File

@@ -211,8 +211,32 @@ export class GraphQLClient extends BaseClient<
return `${super.getBaseUrl()}/api/graphql`; return `${super.getBaseUrl()}/api/graphql`;
} }
public terminateSubscriptions(): void { override reset(): void {
super.reset();
this.client.clearStore();
this.client.stop();
this.resetWsClient(false);
this.createClient();
}
private resetWsClient(recreateClient: boolean): void {
this.wsClient.dispose();
this.wsClient.terminate(); this.wsClient.terminate();
if (!recreateClient) {
return;
}
this.createWSClient(false);
this.client.setLink(this.createLink());
this.restartAllSubscriptions();
}
private restartAllSubscriptions(): void {
this.activeConnectionSubscriptions.forEach((callback) => callback());
} }
protected override shouldQueueRequest(operationName: string | undefined): boolean { protected override shouldQueueRequest(operationName: string | undefined): boolean {
@@ -346,18 +370,12 @@ export class GraphQLClient extends BaseClient<
return; return;
} }
this.wsClient.dispose(); this.resetWsClient(true);
this.wsClient.terminate();
this.createWSClient(false);
this.client.setLink(this.createLink());
this.activeConnectionSubscriptions.forEach((callback) => callback());
}, checkHeartbeatInterval); }, checkHeartbeatInterval);
} }
protected createClient() { protected createClient(createWsClientLazily?: boolean) {
this.createWSClient(); this.createWSClient(createWsClientLazily);
this.client = new ApolloClient({ this.client = new ApolloClient({
cache: new InMemoryCache({ cache: new InMemoryCache({
// for whatever reason there is some weird TypeError complaining that // for whatever reason there is some weird TypeError complaining that