Add abortSignal param to RequestOptionsBase

This commit is contained in:
Rikki Gibson 2018-05-24 10:56:37 -07:00
Родитель db1e56ec43
Коммит cb1162f092
5 изменённых файлов: 34 добавлений и 17 удалений

Просмотреть файл

@ -85,8 +85,9 @@ export class AxiosHttpClient implements HttpClient {
throw new RestError("The request was aborted", "REQUEST_ABORTED_ERROR", undefined, httpRequest);
}
let abortListener: (() => void) | undefined;
const cancelToken = abortSignal && new axios.CancelToken(canceler => {
abortSignal.addEventListener("abort", () => canceler());
abortSignal.addEventListener("abort", abortListener = () => canceler());
});
const rawHeaders: { [headerName: string]: string } = httpRequest.headers.rawHeaders();
@ -115,6 +116,10 @@ export class AxiosHttpClient implements HttpClient {
const axiosErr = err as AxiosError;
throw new RestError(axiosErr.message, "REQUEST_SEND_ERROR", undefined, httpRequest);
}
} finally {
if (abortSignal && abortListener) {
abortSignal.removeEventListener("abort", abortListener);
}
}
const headers = new HttpHeaders(res.headers);

Просмотреть файл

@ -15,4 +15,9 @@ export interface OperationArguments {
* header arguments are added.
*/
customHeaders?: { [headerName: string]: string };
/**
* The signal which can be used to abort requests.
*/
abortSignal?: AbortSignal;
}

Просмотреть файл

@ -256,6 +256,10 @@ export class ServiceClient {
}
}
if (operationArguments.abortSignal) {
httpRequest.abortSignal = operationArguments.abortSignal;
}
return this.sendRequest(httpRequest);
}
}

Просмотреть файл

@ -326,5 +326,11 @@ export interface RequestOptionsBase {
* will be applied before the request is sent.
*/
customHeaders?: { [key: string]: string };
/**
* The signal which can be used to abort requests.
*/
abortSignal?: AbortSignal;
[key: string]: any;
}

Просмотреть файл

@ -5,7 +5,17 @@ import * as should from "should";
import { AxiosHttpClient } from "../../lib/axiosHttpClient";
import { baseURL } from "../testUtils";
import { WebResource } from "../../lib/webResource";
import { isNode } from "../../lib/util/utils";
function getAbortController(): AbortController {
let controller: AbortController;
if (typeof AbortController === "function") {
controller = new AbortController();
} else {
const AbortControllerPonyfill = require("abortcontroller-polyfill/dist/cjs-ponyfill").AbortController;
controller = new AbortControllerPonyfill();
}
return controller;
}
describe("axiosHttpClient", () => {
it("should send HTTP requests", async () => {
@ -89,13 +99,7 @@ describe("axiosHttpClient", () => {
// ensure that a large upload is actually cancelled
this.timeout(2000);
let controller: AbortController;
if (isNode) {
const AbortControllerPonyfill = require("abortcontroller-polyfill/dist/cjs-ponyfill").AbortController;
controller = new AbortControllerPonyfill();
} else {
controller = new AbortController();
}
const controller = getAbortController();
const request = new WebResource(`${baseURL}/fileupload`, "POST", new Uint8Array(1024*1024*100), undefined, undefined, true, controller.signal);
const client = new AxiosHttpClient();
const promise = client.sendRequest(request);
@ -117,14 +121,7 @@ describe("axiosHttpClient", () => {
// ensure that a large upload is actually cancelled
this.timeout(4000);
let controller: AbortController;
if (isNode) {
const AbortControllerPonyfill = require("abortcontroller-polyfill/dist/cjs-ponyfill").AbortController;
controller = new AbortControllerPonyfill();
} else {
controller = new AbortController();
}
const controller = getAbortController();
const buf = new Uint8Array(1024*1024*100);
const requests = [
new WebResource(`${baseURL}/fileupload`, "POST", buf, undefined, undefined, true, controller.signal),