Skip to content

Commit abcea64

Browse files
nhquyss9romise
andauthored
fix(useInfiniteScroll): improve promise handling and add flush post to watch (#5122)
Co-authored-by: Vida Xie <[email protected]>
1 parent df9f996 commit abcea64

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

packages/core/useInfiniteScroll/index.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export function useInfiniteScroll<T extends InfiniteScrollElement>(
8585
function checkAndLoad() {
8686
state.measure()
8787

88-
if (!observedElement.value || !isElementVisible.value || !canLoad.value)
88+
if (!observedElement.value || !isElementVisible.value || !canLoad.value || promise.value)
8989
return
9090

9191
const { scrollHeight, clientHeight, scrollWidth, clientWidth } = observedElement.value as HTMLElement
@@ -94,23 +94,21 @@ export function useInfiniteScroll<T extends InfiniteScrollElement>(
9494
: scrollWidth <= clientWidth
9595

9696
if (state.arrivedState[direction] || isNarrower) {
97-
if (!promise.value) {
98-
promise.value = Promise.all([
99-
onLoadMore(state),
100-
new Promise(resolve => setTimeout(resolve, interval)),
101-
])
102-
.finally(() => {
103-
promise.value = null
104-
nextTick(() => checkAndLoad())
105-
})
106-
}
97+
promise.value = Promise.all([
98+
onLoadMore(state),
99+
new Promise(resolve => setTimeout(resolve, interval)),
100+
])
101+
.finally(() => {
102+
promise.value = null
103+
nextTick(() => checkAndLoad())
104+
})
107105
}
108106
}
109107

110108
const stop = watch(
111109
() => [state.arrivedState[direction], isElementVisible.value, canLoad.value],
112110
checkAndLoad,
113-
{ immediate: true },
111+
{ immediate: true, flush: 'post' },
114112
)
115113

116114
tryOnUnmounted(stop)

0 commit comments

Comments
 (0)