Skip to content

Commit

Permalink
feat: support cancelling http requests (#1048)
Browse files Browse the repository at this point in the history
Co-authored-by: Vishal Narkhede <vishalnarkhede.iitd@gmail.com>
  • Loading branch information
petyosi and vishalnarkhede committed Oct 10, 2022
1 parent c61f55a commit db719d1
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
insightMetrics: InsightMetrics;
defaultWSTimeoutWithFallback: number;
defaultWSTimeout: number;
private nextRequestAbortController: AbortController | null = null;

/**
* Initialize a client
Expand Down Expand Up @@ -2480,6 +2481,11 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
): AxiosRequestConfig {
const token = this._getToken();
const authorization = token ? { Authorization: token } : undefined;
let signal: AbortSignal | null = null;
if (this.nextRequestAbortController !== null) {
signal = this.nextRequestAbortController.signal;
this.nextRequestAbortController = null;
}

if (!options.headers?.['x-client-request-id']) {
options.headers = {
Expand All @@ -2501,6 +2507,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
'X-Stream-Client': this.getUserAgent(),
...options.headers,
},
...(signal ? { signal } : {}),
...options.config,
};
}
Expand Down Expand Up @@ -3030,4 +3037,11 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
async listPushProviders() {
return await this.get<APIResponse & PushProviderListResponse>(this.baseURL + `/push_providers`);
}

/**
* creates an abort controller that will be used by the next HTTP Request.
*/
createAbortControllerForNextRequest() {
return (this.nextRequestAbortController = new AbortController());
}
}

0 comments on commit db719d1

Please sign in to comment.