Skip to content

feat: allow throttling update calls #764

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/vue-virtual-scroller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ When the user scrolls inside RecycleScroller, the views are mostly just moved ar
- `prerender` (default: `0`): render a fixed number of items for Server-Side Rendering (SSR).
- `buffer` (default: `200`): amount of pixel to add to edges of the scrolling visible area to start rendering items further away.
- `emitUpdate` (default: `false`): emit a `'update'` event each time the virtual scroller content is updated (can impact performance).
- `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.
- `listClass` (default: `''`): custom classes added to the item list wrapper.
- `itemClass` (default: `''`): custom classes added to each item.
- `listTag` (default: `'div'`): the element to render as the list's wrapper.
Expand Down
23 changes: 20 additions & 3 deletions packages/vue-virtual-scroller/src/components/RecycleScroller.vue
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ export default {
default: false,
},

updateInterval: {
type: Number,
default: 0,
},

skipHover: {
type: Boolean,
default: false,
Expand Down Expand Up @@ -333,17 +338,29 @@ export default {
handleScroll (event) {
if (!this.$_scrollDirty) {
this.$_scrollDirty = true
requestAnimationFrame(() => {
if (this.$_updateTimeout) return

const requestUpdate = () => requestAnimationFrame(() => {
this.$_scrollDirty = false
const { continuous } = this.updateVisibleItems(false, true)

// It seems sometimes chrome doesn't fire scroll event :/
// When non continous scrolling is ending, we force a refresh
if (!continuous) {
clearTimeout(this.$_refreshTimout)
this.$_refreshTimout = setTimeout(this.handleScroll, 100)
this.$_refreshTimout = setTimeout(this.handleScroll, this.updateInterval + 100)
}
})

requestUpdate()

// Schedule the next update with throttling
if (this.updateInterval) {
this.$_updateTimeout = setTimeout(() => {
this.$_updateTimeout = 0
if (this.$_scrollDirty) requestUpdate();
}, this.updateInterval)
}
}
},

Expand Down Expand Up @@ -597,7 +614,7 @@ export default {
// After the user has finished scrolling
// Sort views so text selection is correct
clearTimeout(this.$_sortTimer)
this.$_sortTimer = setTimeout(this.sortViews, 300)
this.$_sortTimer = setTimeout(this.sortViews, this.updateInterval + 300)

return {
continuous,
Expand Down