Skip to content

Commit 9ba57d7

Browse files
authored
feat: allow throttling update calls (#764)
1 parent 1559ca8 commit 9ba57d7

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

packages/vue-virtual-scroller/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ When the user scrolls inside RecycleScroller, the views are mostly just moved ar
187187
- `prerender` (default: `0`): render a fixed number of items for Server-Side Rendering (SSR).
188188
- `buffer` (default: `200`): amount of pixel to add to edges of the scrolling visible area to start rendering items further away.
189189
- `emitUpdate` (default: `false`): emit a `'update'` event each time the virtual scroller content is updated (can impact performance).
190+
- `updateInterval` (default: `0`): the interval in ms at which the view will be checked for updates after scrolling. When set to `0`, checked will happen during the next animation frame.
190191
- `listClass` (default: `''`): custom classes added to the item list wrapper.
191192
- `itemClass` (default: `''`): custom classes added to each item.
192193
- `listTag` (default: `'div'`): the element to render as the list's wrapper.

packages/vue-virtual-scroller/src/components/RecycleScroller.vue

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ export default {
148148
default: false,
149149
},
150150
151+
updateInterval: {
152+
type: Number,
153+
default: 0,
154+
},
155+
151156
skipHover: {
152157
type: Boolean,
153158
default: false,
@@ -341,17 +346,29 @@ export default {
341346
handleScroll (event) {
342347
if (!this.$_scrollDirty) {
343348
this.$_scrollDirty = true
344-
requestAnimationFrame(() => {
349+
if (this.$_updateTimeout) return
350+
351+
const requestUpdate = () => requestAnimationFrame(() => {
345352
this.$_scrollDirty = false
346353
const { continuous } = this.updateVisibleItems(false, true)
347354
348355
// It seems sometimes chrome doesn't fire scroll event :/
349356
// When non continous scrolling is ending, we force a refresh
350357
if (!continuous) {
351358
clearTimeout(this.$_refreshTimout)
352-
this.$_refreshTimout = setTimeout(this.handleScroll, 100)
359+
this.$_refreshTimout = setTimeout(this.handleScroll, this.updateInterval + 100)
353360
}
354361
})
362+
363+
requestUpdate()
364+
365+
// Schedule the next update with throttling
366+
if (this.updateInterval) {
367+
this.$_updateTimeout = setTimeout(() => {
368+
this.$_updateTimeout = 0
369+
if (this.$_scrollDirty) requestUpdate();
370+
}, this.updateInterval)
371+
}
355372
}
356373
},
357374
@@ -607,7 +624,7 @@ export default {
607624
// After the user has finished scrolling
608625
// Sort views so text selection is correct
609626
clearTimeout(this.$_sortTimer)
610-
this.$_sortTimer = setTimeout(this.sortViews, 300)
627+
this.$_sortTimer = setTimeout(this.sortViews, this.updateInterval + 300)
611628
612629
return {
613630
continuous,

0 commit comments

Comments
 (0)