Safety mechanism to prevent loading everything

This commit is contained in:
pythongosssss
2024-11-24 11:44:26 +00:00
parent 8254e3c9cf
commit 02c13c403f

View File

@@ -163,6 +163,7 @@ const outputFilterPopup = ref(null)
const ITEMS_PER_PAGE = 8 const ITEMS_PER_PAGE = 8
const SCROLL_THRESHOLD = 100 // pixels from bottom to trigger load const SCROLL_THRESHOLD = 100 // pixels from bottom to trigger load
const MAX_LOAD_ITERATIONS = 10
const allTasks = computed(() => const allTasks = computed(() =>
isInFolderView.value isInFolderView.value
@@ -200,27 +201,28 @@ const filterTasks = (tasks: TaskItemImpl[]) =>
return true return true
}) })
const loadMoreItems = () => { const loadMoreItems = (iteration: number) => {
const currentLength = visibleTasks.value.length const currentLength = visibleTasks.value.length
const newTasks = filterTasks(allTasks.value).slice( const newTasks = filterTasks(allTasks.value).slice(
currentLength, currentLength,
currentLength + ITEMS_PER_PAGE currentLength + ITEMS_PER_PAGE
) )
visibleTasks.value.push(...newTasks) visibleTasks.value.push(...newTasks)
if (newTasks.length) { // If we've added some items, check if we need to add more
// Prevent loading everything at once in case of render update issues
if (newTasks.length && iteration < MAX_LOAD_ITERATIONS) {
nextTick(() => { nextTick(() => {
// If we've added some items, check if we need to add more checkAndLoadMore(iteration + 1)
checkAndLoadMore()
}) })
} }
} }
const checkAndLoadMore = () => { const checkAndLoadMore = (iteration: number) => {
if (!scrollContainer.value) return if (!scrollContainer.value) return
const { scrollHeight, scrollTop, clientHeight } = scrollContainer.value const { scrollHeight, scrollTop, clientHeight } = scrollContainer.value
if (scrollHeight - scrollTop - clientHeight < SCROLL_THRESHOLD) { if (scrollHeight - scrollTop - clientHeight < SCROLL_THRESHOLD) {
loadMoreItems() loadMoreItems(iteration)
} }
} }
@@ -228,7 +230,7 @@ useInfiniteScroll(
scrollContainer, scrollContainer,
() => { () => {
if (visibleTasks.value.length < allTasks.value.length) { if (visibleTasks.value.length < allTasks.value.length) {
loadMoreItems() loadMoreItems(0)
} }
}, },
{ distance: SCROLL_THRESHOLD } { distance: SCROLL_THRESHOLD }
@@ -238,7 +240,7 @@ useInfiniteScroll(
// This is necessary as the sidebar tab can change size when user drags the splitter. // This is necessary as the sidebar tab can change size when user drags the splitter.
useResizeObserver(scrollContainer, () => { useResizeObserver(scrollContainer, () => {
nextTick(() => { nextTick(() => {
checkAndLoadMore() checkAndLoadMore(0)
}) })
}) })
@@ -246,7 +248,7 @@ const updateVisibleTasks = () => {
visibleTasks.value = filterTasks(allTasks.value).slice(0, ITEMS_PER_PAGE) visibleTasks.value = filterTasks(allTasks.value).slice(0, ITEMS_PER_PAGE)
nextTick(() => { nextTick(() => {
checkAndLoadMore() checkAndLoadMore(0)
}) })
} }