From 4f57689a1d8f2acbc6812ab115df9a9b6c72c10e Mon Sep 17 00:00:00 2001 From: cbeauchesne Date: Fri, 2 Apr 2021 14:07:06 +0200 Subject: [PATCH] Small fix on stats, when user is faster than the API --- src/js/apis/c2c/DocumentService.js | 22 ++++++++++++++++++- .../outings-stats/OutingsStatsView.vue | 7 +++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/js/apis/c2c/DocumentService.js b/src/js/apis/c2c/DocumentService.js index 0afbd18a09..2fa5ce30d8 100644 --- a/src/js/apis/c2c/DocumentService.js +++ b/src/js/apis/c2c/DocumentService.js @@ -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); } @@ -35,6 +51,10 @@ DocumentService.prototype.fullDownload = function (params, limit, onProgress) { } }) .catch((error) => { + if (cancelled) { + return; + } + reject(error); }); }; diff --git a/src/views/portals/outings-stats/OutingsStatsView.vue b/src/views/portals/outings-stats/OutingsStatsView.vue index 54b7f80bd9..a527cca7d7 100644 --- a/src/views/portals/outings-stats/OutingsStatsView.vue +++ b/src/views/portals/outings-stats/OutingsStatsView.vue @@ -42,6 +42,7 @@ export default { data() { return { + promise: null, outings: {}, loadingPercentage: 0, activeTab: 'all', @@ -57,7 +58,10 @@ 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) { @@ -65,6 +69,7 @@ export default { }, compute(outings) { + this.promise = null; this.outings = { all: outings }; for (let activity of constants.activities) {