From d09c330707d250fcd2a03014366116cbc2af7449 Mon Sep 17 00:00:00 2001 From: Peach Date: Tue, 13 Nov 2018 10:49:29 +0800 Subject: [PATCH] fix: throttle reset bug when reseting plugin (#189) --- src/components/InfiniteLoading.vue | 14 +++++++++----- src/utils.js | 19 +++++++++++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/components/InfiniteLoading.vue b/src/components/InfiniteLoading.vue index 080cfb9..b5c8304 100644 --- a/src/components/InfiniteLoading.vue +++ b/src/components/InfiniteLoading.vue @@ -194,12 +194,16 @@ export default { }); this.$on('$InfiniteLoading:reset', (ev) => { - this.status = STATUS.READY; - this.isFirstLoad = true; - throttleer.reset(); - scrollBarStorage.remove(this.scrollParent); this.scrollParent.addEventListener('scroll', this.scrollHandler, evt3rdArg); - setTimeout(this.scrollHandler, 1); + + // wait for list to be empty and the empty action may trigger a scroll event + setTimeout(() => { + this.status = STATUS.READY; + this.isFirstLoad = true; + throttleer.reset(); + scrollBarStorage.remove(this.scrollParent); + this.scrollHandler(); + }, 1); if (!ev || ev.target !== this) { warn(WARNINGS.IDENTIFIER); diff --git a/src/utils.js b/src/utils.js index 7e039a4..c99082a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -22,17 +22,32 @@ export function error(msg) { } export const throttleer = { + timers: {}, caches: [], throttle(fn) { if (this.caches.indexOf(fn) === -1) { + const fnIndex = this.caches.length; + + // cache current handler this.caches.push(fn); - setTimeout(() => { + + // save timer for current handler + this.timers[fnIndex] = setTimeout(() => { fn(); - this.caches.splice(this.caches.indexOf(fn), 1); + + // empty cache and timer + this.caches.splice(fnIndex, 1); }, config.system.throttleLimit); } }, reset() { + // reset all timers + Object.keys(this.timers).forEach((key) => { + clearTimeout(this.timers[key]); + delete this.timers[key]; + }); + + // empty caches this.caches = []; }, };