Skip to content

Commit 3a014bc

Browse files
committed
feat: cancelAllRequests
1 parent 1a00cad commit 3a014bc

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/https/request.android.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,32 @@ export function cancelRequest(tag: string, client: okhttp3.OkHttpClient = runnin
405405
}
406406
}
407407

408+
export function cancelAllRequests() {
409+
Object.values(notClosedResponses).forEach((req) => req.cancel());
410+
411+
Object.values(runningClients).forEach((client) => {
412+
const dispatcher = client.dispatcher();
413+
//When you want to cancel:
414+
//A) go through the queued calls and cancel if the tag matches:
415+
if (dispatcher.queuedCallsCount() > 0) {
416+
const queuedCalls = dispatcher.queuedCalls();
417+
for (let index = 0; index < queuedCalls.size(); index++) {
418+
const call = queuedCalls.get(index);
419+
call.cancel();
420+
}
421+
}
422+
423+
//B) go through the running calls and cancel if the tag matches:
424+
if (dispatcher.runningCallsCount() > 0) {
425+
const runningCalls = dispatcher.runningCalls();
426+
for (let index = 0; index < runningCalls.size(); index++) {
427+
const call = runningCalls.get(index);
428+
call.cancel();
429+
}
430+
}
431+
});
432+
}
433+
408434
export function clearCookies() {
409435
if (cookieJar) {
410436
cookieJar = null;
@@ -478,7 +504,6 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
478504
}
479505
});
480506
okHttpBody = builder.build();
481-
482507
} else if (type === 'application/x-www-form-urlencoded') {
483508
const builder = new okhttp3.FormBody.Builder();
484509
Object.keys(opts.body).forEach((key) => {

src/https/request.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export function setCache(options?: CacheOptions);
112112
export function clearCache();
113113
export function createRequest(opts: HttpsRequestOptions): HttpsRequest;
114114
export function cancelRequest(tag: string);
115+
export function cancelAllRequests();
115116
export function clearCookies();
116117
export function addNetworkInterceptor(interceptor);
117118

src/https/request.ios.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ export function cancelRequest(tag: string) {
308308
runningRequests[tag].cancel();
309309
}
310310
}
311+
export function cancelAllRequests() {
312+
Object.values(runningRequests).forEach(request=>{
313+
request.cancel()
314+
})
315+
}
311316

312317
export function clearCookies() {
313318
const storage = NSHTTPCookieStorage.sharedHTTPCookieStorage;

0 commit comments

Comments
 (0)