Skip to content

Commit

Permalink
perf($Axios): disable request cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Feb 8, 2020
1 parent 1666824 commit f992025
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
18 changes: 9 additions & 9 deletions src/plugins/axios/axios-for-inter-communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* @date 1/2/20 9:15 AM
*/
// eslint-disable-next-line no-unused-vars
import Axios, { AxiosRequestConfig, AxiosResponse, Canceler, ResponseType } from 'axios'
import * as Cancellation from '@/plugins/axios/cancellation'
import Axios, { AxiosRequestConfig, AxiosResponse, ResponseType } from 'axios'
// import * as Cancellation from '@/plugins/axios/cancellation'
import { HttpStatus } from '@/constants/http-status'
import { AxiosUtil } from '@/utils/axios-util'
// import { AxiosUtil } from '@/utils/axios-util'

/**
CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxied request.
Expand Down Expand Up @@ -66,13 +66,13 @@ export const service = Axios.create({
service.interceptors.request.use(
async (axiosRequestConfig: AxiosRequestConfig) => {
// Cancel and remove same request before sending upcoming request.
Cancellation.cancelAndRemoveSamePendingRequest(axiosRequestConfig)
// Cancellation.cancelAndRemoveSamePendingRequest(axiosRequestConfig)
// Configure cancelToken for request
axiosRequestConfig.cancelToken = new Cancellation.CancelToken((cancel: Canceler) => {
const requestToken = AxiosUtil.getRequestToken(axiosRequestConfig)
const pendingRequest = new Cancellation.PendingRequest(requestToken, cancel)
Cancellation.pendingRequestList.push(pendingRequest)
})
// axiosRequestConfig.cancelToken = new Cancellation.CancelToken((cancel: Canceler) => {
// const requestToken = AxiosUtil.getRequestToken(axiosRequestConfig)
// const pendingRequest = new Cancellation.PendingRequest(requestToken, cancel)
// Cancellation.pendingRequestList.push(pendingRequest)
// })
return axiosRequestConfig
},
(error: any) => {
Expand Down
18 changes: 9 additions & 9 deletions src/plugins/axios/axios-for-intra-communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* @date 1/2/20 9:15 AM
*/
// eslint-disable-next-line no-unused-vars
import Axios, { AxiosRequestConfig, AxiosResponse, Canceler, ResponseType } from 'axios'
import Axios, { AxiosRequestConfig, AxiosResponse, ResponseType } from 'axios'
import { validate } from 'class-validator'
import { ClassValidationUtil } from '@/utils/class-validation-util'
import * as Cancellation from '@/plugins/axios/cancellation'
// import * as Cancellation from '@/plugins/axios/cancellation'
import { HttpStatus } from '@/constants/http-status'
import { ResponseBody } from '@/plugins/axios/response-body'
import { AxiosUtil } from '@/utils/axios-util'
// import { AxiosUtil } from '@/utils/axios-util'

// 1. Create an axios instance.
export const service = Axios.create({
Expand Down Expand Up @@ -42,13 +42,13 @@ service.interceptors.request.use(
}
}
// Cancel and remove same request before sending upcoming request.
Cancellation.cancelAndRemoveSamePendingRequest(axiosRequestConfig)
// Cancellation.cancelAndRemoveSamePendingRequest(axiosRequestConfig)
// Configure cancelToken for request
axiosRequestConfig.cancelToken = new Cancellation.CancelToken((cancel: Canceler) => {
const requestToken = AxiosUtil.getRequestToken(axiosRequestConfig)
const pendingRequest = new Cancellation.PendingRequest(requestToken, cancel)
Cancellation.pendingRequestList.push(pendingRequest)
})
// axiosRequestConfig.cancelToken = new Cancellation.CancelToken((cancel: Canceler) => {
// const requestToken = AxiosUtil.getRequestToken(axiosRequestConfig)
// const pendingRequest = new Cancellation.PendingRequest(requestToken, cancel)
// Cancellation.pendingRequestList.push(pendingRequest)
// })
return axiosRequestConfig
},
(error: any) => {
Expand Down
10 changes: 5 additions & 5 deletions src/utils/axios-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export class AxiosUtil {
* @return {String} Request token.
*/
static getRequestToken = (axiosRequestConfig: AxiosRequestConfig): string => {
let params
if (!axiosRequestConfig.params) {
params = JSON.stringify(axiosRequestConfig.params)
let params = ''
if (axiosRequestConfig.params) {
params += JSON.stringify(axiosRequestConfig.params)
}
if (!axiosRequestConfig.data) {
params = JSON.stringify(axiosRequestConfig.data)
if (axiosRequestConfig.data) {
params += JSON.stringify(axiosRequestConfig.data)
}
return `${axiosRequestConfig?.url?.split('?')[0]}::${axiosRequestConfig.method}::${params}`
}
Expand Down

0 comments on commit f992025

Please sign in to comment.