Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: add debounce props #337

Open
wants to merge 7 commits into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 src/components/DynamicScroller.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:items="itemsWithSize"
:min-item-size="minItemSize"
:direction="direction"
:debounce="debounce"
key-field="id"
v-bind="$attrs"
@resize="onScrollerResize"
Expand Down
37 changes: 24 additions & 13 deletions src/components/RecycleScroller.vue
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export default {

beforeDestroy () {
this.removeListeners()
clearTimeout(this.scrollTimeout)
},

methods: {
Expand Down Expand Up @@ -236,19 +237,29 @@ export default {
},

handleScroll (event) {
if (!this.$_scrollDirty) {
this.$_scrollDirty = true
requestAnimationFrame(() => {
this.$_scrollDirty = false
const { continuous } = this.updateVisibleItems(false)

// 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)
}
})
const job = () => {
if (!this.$_scrollDirty) {
this.$_scrollDirty = true
requestAnimationFrame(() => {
this.$_scrollDirty = false
const { continuous } = this.updateVisibleItems(false)

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

clearTimeout(this.scrollTimeout)

if (this.debounce) {
this.scrollTimeout = setTimeout(job, parseInt(this.debounce))
} else {
job()
}
},

Expand Down
5 changes: 5 additions & 0 deletions src/components/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export const props = {
default: 'vertical',
validator: (value) => ['vertical', 'horizontal'].includes(value),
},

debounce: {
type: [Number, String],
default: 0,
},
}

export function simpleArray () {
Expand Down