Skip to content

Commit

Permalink
fix: throttle reset bug when reseting plugin (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Nov 13, 2018
1 parent 79109fc commit d09c330
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/components/InfiniteLoading.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 17 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
},
};
Expand Down

0 comments on commit d09c330

Please sign in to comment.