Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flow-typed/box-ui-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* eslint-disable no-unused-vars */
// NOTE: all of these imports resolve to `any`
// see https://github.com/facebook/flow/issues/7574
import type { $AxiosXHR, $AxiosError, Axios, CancelTokenSource } from 'axios';
import type { $AxiosXHR, $AxiosError, Axios, AbortController } from 'axios';
import type FolderAPI from '../src/api/Folder';
import type FileAPI from '../src/api/File';
import type WebLinkAPI from '../src/api/WebLink';
Expand Down
16 changes: 8 additions & 8 deletions src/utils/Xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Xhr {

axios: Axios;

axiosSource: CancelTokenSource;
axiosAbortController: AbortController;

clientName: ?string;

Expand Down Expand Up @@ -107,7 +107,7 @@ class Xhr {
this.version = version;

this.axios = axios.create();
this.axiosSource = axios.CancelToken.source();
this.axiosAbortController = new AbortController();
this.axios.interceptors.response.use(this.responseInterceptor, this.errorInterceptor);

if (typeof requestInterceptor === 'function') {
Expand Down Expand Up @@ -271,7 +271,7 @@ class Xhr {
}): Promise<StringAnyMap> {
return this.getHeaders(id, headers).then(hdrs =>
this.axios.get(url, {
cancelToken: this.axiosSource.token,
signal: this.axiosAbortController.signal,
params,
headers: hdrs,
parsedUrl: this.getParsedUrl(url),
Expand Down Expand Up @@ -487,7 +487,7 @@ class Xhr {
method,
headers: hdrs,
onUploadProgress: progressHandlerToUse,
cancelToken: this.axiosSource.token,
signal: this.axiosAbortController.signal,
})
.then(response => {
clearTimeout(idleTimeout);
Expand All @@ -506,13 +506,13 @@ class Xhr {
*
* @return {void}
*/
abort(): void {
abort(reason: string): void {
if (this.retryTimeout) {
clearTimeout(this.retryTimeout);
}
if (this.axiosSource) {
this.axiosSource.cancel();
this.axiosSource = axios.CancelToken.source();

if (this.axiosAbortController) {
this.axiosAbortController.abort(reason);
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/utils/__tests__/Xhr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('util/Xhr', () => {
})
.then(() => {
expect(xhrInstance.axios.get).toHaveBeenCalledWith('url', {
cancelToken: xhrInstance.axiosSource.token,
signal: xhrInstance.axiosAbortController.signal,
params: {},
headers: {},
parsedUrl: url,
Expand Down Expand Up @@ -283,14 +283,15 @@ describe('util/Xhr', () => {

describe('abort()', () => {
test('should cancel axios request', () => {
const mockSource = {
cancel: jest.fn(),
const mockAbortController = {
signal: jest.fn(),
abort: jest.fn(),
};
xhrInstance.axiosSource = mockSource;
xhrInstance.axiosAbortController = mockAbortController;

xhrInstance.abort();

expect(mockSource.cancel).toHaveBeenCalled();
expect(mockAbortController.abort).toHaveBeenCalled();
});
});

Expand Down