Skip to content

Commit 2061721

Browse files
authoredAug 3, 2020
fix(plugin): hide progress-bar on cancelled requests (#398)
1 parent 669d969 commit 2061721

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
 

‎lib/plugin.js

+4
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ const setupProgress = (axios) => {
161161
currentRequests--
162162

163163
if (Axios.isCancel(error)) {
164+
if (currentRequests <= 0) {
165+
currentRequests = 0
166+
$loading().finish()
167+
}
164168
return
165169
}
166170

‎test/fixture/api.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
function sleep (ms) {
2+
return new Promise((resolve) => {
3+
setTimeout(resolve, ms)
4+
})
5+
}
6+
17
module.exports = {
28
path: '/test_api',
3-
handler (req, res) {
9+
async handler (req, res) {
10+
const query = new URL(req.url, 'http://localhost:3000').query
11+
if (query && query.delay) {
12+
await sleep(query.delay)
13+
}
14+
415
res.end(JSON.stringify({
516
url: req.url,
617
method: req.method

‎test/fixture/pages/cancelToken.vue

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<template>
2+
<div>
3+
there should be no loading bar left over:
4+
<button @click="test">Fake Request</button>
5+
</div>
6+
</template>
7+
8+
<script>
9+
export default {
10+
methods: {
11+
test() {
12+
const source = this.$axios.CancelToken.source();
13+
this.$axios
14+
.$post(
15+
"http://localhost:3000/test_api/foo/bar?delay=1000",
16+
{ data: "test" },
17+
{
18+
cancelToken: source.token,
19+
}
20+
)
21+
.catch((err) => {
22+
if (this.$axios.isCancel(err)) {
23+
console.log("request canceled");
24+
}
25+
});
26+
27+
setTimeout(function () {
28+
source.cancel();
29+
}, 500);
30+
},
31+
},
32+
};
33+
</script>

0 commit comments

Comments
 (0)
Please sign in to comment.