Skip to content

Commit 68fddd1

Browse files
karlkarlito40
authored andcommitted
feat: add debounce props
1 parent cec667b commit 68fddd1

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

src/components/DynamicScroller.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
:items="itemsWithSize"
55
:min-item-size="minItemSize"
66
:direction="direction"
7+
:debounce="debounce"
78
key-field="id"
89
v-bind="$attrs"
910
@resize="onScrollerResize"

src/components/RecycleScroller.vue

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ export default {
191191
192192
beforeDestroy () {
193193
this.removeListeners()
194+
clearTimeout(this.scrollTimeout)
194195
},
195196
196197
methods: {
@@ -236,19 +237,29 @@ export default {
236237
},
237238
238239
handleScroll (event) {
239-
if (!this.$_scrollDirty) {
240-
this.$_scrollDirty = true
241-
requestAnimationFrame(() => {
242-
this.$_scrollDirty = false
243-
const { continuous } = this.updateVisibleItems(false)
244-
245-
// It seems sometimes chrome doesn't fire scroll event :/
246-
// When non continous scrolling is ending, we force a refresh
247-
if (!continuous) {
248-
clearTimeout(this.$_refreshTimout)
249-
this.$_refreshTimout = setTimeout(this.handleScroll, 100)
250-
}
251-
})
240+
const job = () => {
241+
if (!this.$_scrollDirty) {
242+
this.$_scrollDirty = true
243+
requestAnimationFrame(() => {
244+
this.$_scrollDirty = false
245+
const { continuous } = this.updateVisibleItems(false)
246+
247+
// It seems sometimes chrome doesn't fire scroll event :/
248+
// When non continous scrolling is ending, we force a refresh
249+
if (!continuous) {
250+
clearTimeout(this.$_refreshTimeout)
251+
this.$_refreshTimeout = setTimeout(this.handleScroll, 100)
252+
}
253+
})
254+
}
255+
}
256+
257+
clearTimeout(this.scrollTimeout)
258+
259+
if (this.debounce) {
260+
this.scrollTimeout = setTimeout(job, parseInt(this.debounce))
261+
} else {
262+
job()
252263
}
253264
},
254265

src/components/common.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export const props = {
1414
default: 'vertical',
1515
validator: (value) => ['vertical', 'horizontal'].includes(value),
1616
},
17+
18+
debounce: {
19+
type: [Number, String],
20+
default: 0,
21+
},
1722
}
1823

1924
export function simpleArray () {

0 commit comments

Comments
 (0)