Skip to content

Commit 18c2456

Browse files
Merge branch 'release/v1.4.0'
2 parents bddbc0e + f0e1b8e commit 18c2456

File tree

11 files changed

+99
-16
lines changed

11 files changed

+99
-16
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
VUE_APP_PACKAGE_JSON=''
33

44
# TAG must be corresponding with the version tag in package.json, need to modify it when new version releases
5-
TAG=1.3.1
5+
TAG=1.4.0

.env.development

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ VUE_APP_RUN_ON_DOCKER = 'false'
88

99
# Backend server IP and port. It's required when the environment is development.
1010
# Left blank if the environment is not development.
11-
VUE_APP_BASE_URL = 'http://localhost:8080'
11+
VUE_APP_BASE_URL = 'http://localhost:8081'
1212
# Base api
1313
VUE_APP_BASE_API = '/jm-spring-boot-template-dev_loc'
1414
# Resource base api for picture, video ect.
15-
VUE_APP_RESOURCE_BASE_API = 'http://localhost:8080/jm-spring-boot-template-dev_loc'
15+
VUE_APP_RESOURCE_BASE_API = 'http://localhost:8081/jm-spring-boot-template-dev_loc'
1616

1717
# vue-cli uses the VUE_CLI_BABEL_TRANSPILE_MODULES environment variable,
1818
# to control whether the babel-plugin-dynamic-import-node plugin is enabled.

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# [1.4.0](https://github.com/johnnymillergh/vuetify-typescript-playground/compare/v1.3.1...v1.4.0) (2020-01-07)
2+
3+
4+
### Performance Improvements
5+
6+
* **$Axios:** avoid sending same request at the same time ([c7eb3b7](https://github.com/johnnymillergh/vuetify-typescript-playground/commit/c7eb3b7d5388aaa0b8fea4fcb6cd129c87fb6b5c))
7+
8+
9+
110
## [1.3.1](https://github.com/johnnymillergh/vuetify-typescript-playground/compare/v1.3.0...v1.3.1) (2020-01-03)
211

312

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuetify-typescript-playground",
3-
"version": "1.3.1",
3+
"version": "1.4.0",
44
"license": "Apache-2.0",
55
"description": "Vuetify Typescript Playground",
66
"author": {
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
const axios = require('axios')
22
const MockAdapter = require('axios-mock-adapter')
33

4-
// All requests using this instance will have a 2 seconds delay:
4+
// All requests using this instance will have a 2 seconds delay.
55
const mock = new MockAdapter(axios, { delayResponse: 2000 })
66

7-
// Mock GET request to /users when param `searchText` is 'John'
8-
// arguments for reply are (status, data, headers)
97
mock.onGet('/api/getDemo').reply(200, {
108
message: '/api/getDemo success!'
119
})
1210

13-
mock.onGet('/api/delay').reply(200, {
14-
message: 'delay message'
11+
mock.onGet('/api/cancel-request-test').reply(200, {
12+
message: '/api/cancel-request-test SUCCESS'
1513
})

src/plugins/axios/cancellation.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import Axios, { AxiosRequestConfig, Canceler } from 'axios'
3+
4+
export const CancelToken = Axios.CancelToken
5+
export const pendingRequestList: PendingRequest[] = []
6+
7+
/**
8+
* Pending Request
9+
* @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com
10+
* @date 1/6/20 2:26 PM
11+
*/
12+
export class PendingRequest {
13+
/**
14+
* Request token.
15+
*
16+
* Format: [URL]::[HTTP Method]::[Stringified Request Params]
17+
*
18+
* Sample: /api/cancel-request-test::get::undefined
19+
*/
20+
requestToken!: string
21+
22+
/**
23+
* Cancel executor
24+
*/
25+
cancelExecutor!: Canceler
26+
27+
constructor (requestToken: string, cancelExecutor: Canceler) {
28+
this.requestToken = requestToken
29+
this.cancelExecutor = cancelExecutor
30+
}
31+
}
32+
33+
export const cancelAndRemoveSamePendingRequest = (axiosRequestConfig: AxiosRequestConfig): void => {
34+
pendingRequestList.forEach((pendingRequest, index) => {
35+
const requestToken = `${axiosRequestConfig?.url?.split('?')[0]}::${axiosRequestConfig.method}::${JSON.stringify(axiosRequestConfig.params)}`
36+
if (pendingRequest.requestToken === requestToken) {
37+
// Execute cancellation of this pending request.
38+
pendingRequest.cancelExecutor(`Cancelled Axios request. Request token: ${requestToken}`)
39+
// Remove this pending request from list.
40+
pendingRequestList.splice(index, 1)
41+
}
42+
})
43+
}

src/plugins/axios/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
* @date 1/2/20 9:15 AM
44
*/
55
// eslint-disable-next-line no-unused-vars
6-
import Axios, { ResponseType } from 'axios'
6+
import Axios, { AxiosRequestConfig, Canceler, ResponseType } from 'axios'
77
import { validate } from 'class-validator'
88
import { ClassValidationUtil } from '@/utils/class-validation-util'
9+
import * as Cancellation from '@/plugins/axios/cancellation'
910

1011
// 1. Create an axios instance.
1112
export const service = Axios.create({
@@ -37,6 +38,14 @@ service.interceptors.request.use(
3738
throw new Error(`Validation failed! The 1st error: ${ClassValidationUtil.getFirstValidationError(validation)}`)
3839
}
3940
}
41+
// Cancel and remove same request before sending upcoming request.
42+
Cancellation.cancelAndRemoveSamePendingRequest(axiosRequestConfig)
43+
// Configure cancelToken for request
44+
axiosRequestConfig.cancelToken = new Cancellation.CancelToken((cancel: Canceler) => {
45+
const requestToken = `${axiosRequestConfig?.url?.split('?')[0]}::${axiosRequestConfig.method}::${JSON.stringify(axiosRequestConfig.params)}`
46+
const pendingRequest = new Cancellation.PendingRequest(requestToken, cancel)
47+
Cancellation.pendingRequestList.push(pendingRequest)
48+
})
4049
return axiosRequestConfig
4150
},
4251
error => {

src/requests/vuetify-demo/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as Axios from '@/plugins/axios'
2-
// eslint-disable-next-line no-unused-vars
2+
/* eslint-disable no-unused-vars */
33
import { GetDemoPayload } from '@/requests/vuetify-demo/payload/get-demo-payload'
4+
import { CancelRequestTestPayload } from '@/requests/vuetify-demo/payload/cancel-request-test-payload'
45

56
export const vuetifyDemoApi = {
67
getDemo: (getDemoPayload: GetDemoPayload) => Axios.get('/api/getDemo', getDemoPayload),
7-
delay: (delayTime: number) => Axios.get('/api/delay', delayTime)
8+
cancelRequestTest: (cancelRequestTestPayload: CancelRequestTestPayload) => Axios.get('/api/cancel-request-test', cancelRequestTestPayload)
89
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Allow } from 'class-validator'
2+
3+
export class CancelRequestTestPayload {
4+
/**
5+
* ID.
6+
*/
7+
@Allow()
8+
id!: number
9+
}

src/router/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const routes = [
1212
},
1313
{
1414
path: '/hello-world',
15-
name: 'hello-world',
15+
// name: 'hello-world',
1616
// route level code-splitting
1717
// this generates a separate chunk (hello-world.[hash].js) for this route
1818
// which is lazy-loaded when the route is visited.
@@ -28,7 +28,7 @@ const routes = [
2828
},
2929
{
3030
path: '/vuetify-demo',
31-
name: 'vuetify-demo',
31+
// name: 'vuetify-demo',
3232
component: Home,
3333
children: [
3434
{
@@ -39,7 +39,7 @@ const routes = [
3939
},
4040
{
4141
path: '/form-validation',
42-
name: 'form-validation',
42+
// name: 'form-validation',
4343
component: Home,
4444
children: [
4545
{

0 commit comments

Comments
 (0)