Skip to content

Commit 4746509

Browse files
author
Hoang Nguyen
authored
[UI] Cancel all requests api, async jobs in UI when user logs out (apache#5663)
* cancel requests in UI when user logs out * clear notification, message from UI after logout
1 parent 001f421 commit 4746509

3 files changed

Lines changed: 44 additions & 16 deletions

File tree

ui/src/api/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
import { axios } from '@/utils/request'
18+
import { axios, sourceToken } from '@/utils/request'
19+
import { message, notification } from 'ant-design-vue'
1920

2021
export function api (command, args = {}, method = 'GET', data = {}) {
2122
let params = {}
@@ -40,6 +41,8 @@ export function api (command, args = {}, method = 'GET', data = {}) {
4041
}
4142

4243
export function login (arg) {
44+
sourceToken.init()
45+
4346
const params = new URLSearchParams()
4447
params.append('command', 'login')
4548
params.append('username', arg.username || arg.email)
@@ -57,5 +60,8 @@ export function login (arg) {
5760
}
5861

5962
export function logout () {
63+
sourceToken.cancel()
64+
message.destroy()
65+
notification.destroy()
6066
return api('logout')
6167
}

ui/src/utils/plugins.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { api } from '@/api'
2121
import { message, notification } from 'ant-design-vue'
2222
import eventBus from '@/config/eventBus'
2323
import store from '@/store'
24+
import { sourceToken } from '@/utils/request'
2425

2526
export const pollJobPlugin = {
2627
install (Vue) {
@@ -177,20 +178,22 @@ export const pollJobPlugin = {
177178
}
178179
}).catch(e => {
179180
console.error(`${catchMessage} - ${e}`)
180-
let countNotify = store.getters.countNotify
181-
countNotify++
182-
store.commit('SET_COUNT_NOTIFY', countNotify)
183-
notification.error({
184-
top: '65px',
185-
message: i18n.t('label.error'),
186-
description: catchMessage,
187-
duration: 0,
188-
onClose: () => {
189-
let countNotify = store.getters.countNotify
190-
countNotify > 0 ? countNotify-- : countNotify = 0
191-
store.commit('SET_COUNT_NOTIFY', countNotify)
192-
}
193-
})
181+
if (!sourceToken.isCancel(e)) {
182+
let countNotify = store.getters.countNotify
183+
countNotify++
184+
store.commit('SET_COUNT_NOTIFY', countNotify)
185+
notification.error({
186+
top: '65px',
187+
message: i18n.t('label.error'),
188+
description: catchMessage,
189+
duration: 0,
190+
onClose: () => {
191+
let countNotify = store.getters.countNotify
192+
countNotify > 0 ? countNotify-- : countNotify = 0
193+
store.commit('SET_COUNT_NOTIFY', countNotify)
194+
}
195+
})
196+
}
194197
catchMethod && catchMethod()
195198
})
196199
}

ui/src/utils/request.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { CURRENT_PROJECT } from '@/store/mutation-types'
2424
import { i18n } from '@/locales'
2525
import store from '@/store'
2626

27+
let source
2728
const service = axios.create({
2829
timeout: 600000
2930
})
@@ -128,6 +129,8 @@ const err = (error) => {
128129

129130
// request interceptor
130131
service.interceptors.request.use(config => {
132+
source = sourceToken.getSource()
133+
config.cancelToken = source.token
131134
if (config && config.params) {
132135
config.params.response = 'json'
133136
const project = Vue.ls.get(CURRENT_PROJECT)
@@ -154,7 +157,23 @@ const installer = {
154157
}
155158
}
156159

160+
const sourceToken = {
161+
init: () => { source = axios.CancelToken.source() },
162+
isCancel: (e) => {
163+
return axios.isCancel(e)
164+
},
165+
getSource: () => {
166+
if (!source) sourceToken.init()
167+
return source
168+
},
169+
cancel: () => {
170+
if (!source) sourceToken.init()
171+
source.cancel()
172+
}
173+
}
174+
157175
export {
158176
installer as VueAxios,
159-
service as axios
177+
service as axios,
178+
sourceToken
160179
}

0 commit comments

Comments
 (0)