From bace9832d9ab07b158068eca3104a24f4c494851 Mon Sep 17 00:00:00 2001 From: Martin Guillon Date: Sat, 15 Jan 2022 11:36:06 +0100 Subject: [PATCH 1/2] feat: added method `cancelRequest`. You need to set the `tag` in request options first --- src/https.android.ts | 2 +- src/https.common.ts | 1 + src/https.d.ts | 1 + src/https.ios.ts | 23 +++++++++++++++++++++-- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/https.android.ts b/src/https.android.ts index f129536..5d91fd6 100644 --- a/src/https.android.ts +++ b/src/https.android.ts @@ -456,7 +456,7 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque } request[methods[opts.method]](okHttpBody); } - const tag = `okhttp_request_${CALL_ID++}`; + const tag = opts.tag || `okhttp_request_${CALL_ID++}`; const call = client.newCall(request.tag(tag).build()); // We have to allow networking on the main thread because larger responses will crash the app with an NetworkOnMainThreadException. diff --git a/src/https.common.ts b/src/https.common.ts index d5aa975..f951fc0 100644 --- a/src/https.common.ts +++ b/src/https.common.ts @@ -28,6 +28,7 @@ export interface HttpsRequestObject { export type CachePolicy = 'noCache' | 'onlyCache' | 'ignoreCache'; export interface HttpsRequestOptions extends HttpRequestOptions { url: string; + tag?: string; // optional request tag to allow to cancel it method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD'; headers?: Headers; params?: HttpsRequestObject; diff --git a/src/https.d.ts b/src/https.d.ts index 8f46418..3bd3362 100644 --- a/src/https.d.ts +++ b/src/https.d.ts @@ -8,5 +8,6 @@ export function request(options: Https.HttpsRequestOptions): Promise { - opts.onProgress(progress.completedUnitCount, progress.totalUnitCount); - } + opts.onProgress(progress.completedUnitCount, progress.totalUnitCount); + } : null; let task: NSURLSessionDataTask; + const tag = opts.tag; + function clearRunningRequest() { + if (tag) { + delete runningRequests[tag]; + } + } return { get nativeRequest() { return task; @@ -364,6 +378,7 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque cancel: () => task && task.cancel(), run(resolve, reject) { const success = function (task: NSURLSessionDataTask, data?: any) { + clearRunningRequest(); // TODO: refactor this code with failure one. const content = useLegacy ? new HttpsResponse(data, opts.url) : getData(data); let getHeaders = () => ({}); @@ -394,6 +409,7 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque // } }; const failure = function (task: NSURLSessionDataTask, error: any) { + clearRunningRequest(); AFFailure(resolve, reject, task, error, useLegacy, opts.url); }; if (type.startsWith('multipart/form-data')) { @@ -451,6 +467,9 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque throw new Error('method_not_supported_multipart'); } } + if (task && tag) { + runningRequests[tag] = task; + } }, }; } From 6c80e100515edb3c9b5c41cbfb1830188efd821e Mon Sep 17 00:00:00 2001 From: Martin Guillon Date: Sat, 15 Jan 2022 11:38:56 +0100 Subject: [PATCH 2/2] v3.3.6 --- CHANGELOG.md | 11 +++++++++++ lerna.json | 2 +- plugin/CHANGELOG.md | 8 ++++++++ plugin/package.json | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e87202e..abba438 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.3.6](https://github.com/farfromrefug/nativescript-https/compare/v3.3.5...v3.3.6) (2022-01-15) + + +### Features + +* added method `cancelRequest`. You need to set the `tag` in request options first ([bace983](https://github.com/farfromrefug/nativescript-https/commit/bace9832d9ab07b158068eca3104a24f4c494851)) + + + + + ## [3.3.5](https://github.com/farfromrefug/nativescript-https/compare/v3.3.4...v3.3.5) (2022-01-14) **Note:** Version bump only for package @nativescript-community/https diff --git a/lerna.json b/lerna.json index 5d36e71..4f4fd75 100644 --- a/lerna.json +++ b/lerna.json @@ -2,7 +2,7 @@ "packages": [ "plugin" ], - "version": "3.3.5", + "version": "3.3.6", "command": { "publish": { "conventionalCommits": true diff --git a/plugin/CHANGELOG.md b/plugin/CHANGELOG.md index 1e0cd6a..3f18729 100644 --- a/plugin/CHANGELOG.md +++ b/plugin/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.3.6](https://github.com/farfromrefug/nativescript-https/compare/v3.3.5...v3.3.6) (2022-01-15) + +**Note:** Version bump only for package @nativescript-community/https + + + + + ## [3.3.5](https://github.com/farfromrefug/nativescript-https/compare/v3.3.4...v3.3.5) (2022-01-14) **Note:** Version bump only for package @nativescript-community/https diff --git a/plugin/package.json b/plugin/package.json index 7983328..8ab3ece 100644 --- a/plugin/package.json +++ b/plugin/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript-community/https", - "version": "3.3.5", + "version": "3.3.6", "description": "Nativescript plugin for gestures", "main": "https", "sideEffects": false,