Skip to content

Commit

Permalink
Small fix on stats, when user is faster than the API
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeauchesne committed Apr 2, 2021
1 parent 3e9cbaa commit 4f57689
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/js/apis/c2c/DocumentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@ DocumentService.prototype.fullDownload = function (params, limit, onProgress) {
const MAX_SIZE = 2000;
const API_MAX_LIMIT = 100;

let cancelled = false;

limit = limit || MAX_SIZE;
limit = limit > MAX_SIZE ? MAX_SIZE : limit;

return new Promise((resolve, reject) => {
class CancelablePromise extends Promise {
cancel() {
cancelled = true;
}
}

return new CancelablePromise((resolve, reject) => {
const result = [];

const download = (offset = 0) => {
if (cancelled) {
return;
}

this.getAll({ ...params, offset, limit: API_MAX_LIMIT })
.then(({ data }) => {
if (cancelled) {
return;
}

for (const document of data.documents) {
result.push(document);
}
Expand All @@ -35,6 +51,10 @@ DocumentService.prototype.fullDownload = function (params, limit, onProgress) {
}
})
.catch((error) => {
if (cancelled) {
return;
}

reject(error);
});
};
Expand Down
7 changes: 6 additions & 1 deletion src/views/portals/outings-stats/OutingsStatsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default {
data() {
return {
promise: null,
outings: {},
loadingPercentage: 0,
activeTab: 'all',
Expand All @@ -57,14 +58,18 @@ export default {
methods: {
load() {
c2c.outing.fullDownload(this.$route.query, LIST_MAX_LENGTH, this.progress).then(this.compute);
if (this.promise) {
this.promise.cancel();
}
this.promise = c2c.outing.fullDownload(this.$route.query, LIST_MAX_LENGTH, this.progress).then(this.compute);
},
progress(current, total) {
this.loadingPercentage = current / Math.min(total, LIST_MAX_LENGTH);
},
compute(outings) {
this.promise = null;
this.outings = { all: outings };
for (let activity of constants.activities) {
Expand Down

0 comments on commit 4f57689

Please sign in to comment.