Skip to content

Commit

Permalink
fix(plugin): hide progress-bar on cancelled requests (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
T0shik authored Aug 3, 2020
1 parent 669d969 commit 2061721
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ const setupProgress = (axios) => {
currentRequests--

if (Axios.isCancel(error)) {
if (currentRequests <= 0) {
currentRequests = 0
$loading().finish()
}
return
}

Expand Down
13 changes: 12 additions & 1 deletion test/fixture/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
function sleep (ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}

module.exports = {
path: '/test_api',
handler (req, res) {
async handler (req, res) {
const query = new URL(req.url, 'http://localhost:3000').query
if (query && query.delay) {
await sleep(query.delay)
}

res.end(JSON.stringify({
url: req.url,
method: req.method
Expand Down
33 changes: 33 additions & 0 deletions test/fixture/pages/cancelToken.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<template>
<div>
there should be no loading bar left over:
<button @click="test">Fake Request</button>
</div>
</template>

<script>
export default {
methods: {
test() {
const source = this.$axios.CancelToken.source();
this.$axios
.$post(
"http://localhost:3000/test_api/foo/bar?delay=1000",
{ data: "test" },
{
cancelToken: source.token,
}
)
.catch((err) => {
if (this.$axios.isCancel(err)) {
console.log("request canceled");
}
});
setTimeout(function () {
source.cancel();
}, 500);
},
},
};
</script>

0 comments on commit 2061721

Please sign in to comment.