LCOV - code coverage report
Current view: top level - src/platform/workflow/templates/composables - useLazyPagination.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 67.4 % 46 31
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { computed, ref, shallowRef, watch } from 'vue'
       2              : import type { Ref } from 'vue'
       3              : 
       4              : interface LazyPaginationOptions {
       5              :   itemsPerPage?: number
       6              :   initialPage?: number
       7              : }
       8              : 
       9           26 : export function useLazyPagination<T>(
      10              :   items: Ref<T[]> | T[],
      11              :   options: LazyPaginationOptions = {}
      12              : ) {
      13           26 :   const { itemsPerPage = 12, initialPage = 1 } = options
      14              : 
      15           26 :   const currentPage = ref(initialPage)
      16           26 :   const isLoading = ref(false)
      17           26 :   const loadedPages = shallowRef(new Set<number>([]))
      18              : 
      19              :   // Get reactive items array
      20           26 :   const itemsArray = computed(() => {
      21          125 :     const itemData = 'value' in items ? items.value : items
      22          125 :     return Array.isArray(itemData) ? itemData : []
      23              :   })
      24              : 
      25              :   // Simulate pagination by slicing the items
      26           26 :   const paginatedItems = computed(() => {
      27           29 :     const itemData = itemsArray.value
      28           29 :     if (itemData.length === 0) {
      29            0 :       return []
      30              :     }
      31              : 
      32           29 :     const loadedPageNumbers = Array.from(loadedPages.value).sort(
      33              :       (a, b) => a - b
      34              :     )
      35              :     const maxLoadedPage = Math.max(...loadedPageNumbers, 0)
      36           29 :     const endIndex = maxLoadedPage * itemsPerPage
      37           29 :     return itemData.slice(0, endIndex)
      38              :   })
      39              : 
      40           26 :   const hasMoreItems = computed(() => {
      41           36 :     const itemData = itemsArray.value
      42           36 :     if (itemData.length === 0) {
      43            0 :       return false
      44              :     }
      45              : 
      46           36 :     const loadedPagesArray = Array.from(loadedPages.value)
      47              :     const maxLoadedPage = Math.max(...loadedPagesArray, 0)
      48           36 :     return maxLoadedPage * itemsPerPage < itemData.length
      49              :   })
      50              : 
      51           26 :   const totalPages = computed(() => {
      52            0 :     const itemData = itemsArray.value
      53            0 :     if (itemData.length === 0) {
      54            0 :       return 0
      55              :     }
      56            0 :     return Math.ceil(itemData.length / itemsPerPage)
      57              :   })
      58              : 
      59           26 :   const loadNextPage = async () => {
      60            0 :     if (isLoading.value || !hasMoreItems.value) return
      61              : 
      62            0 :     isLoading.value = true
      63            0 :     const loadedPagesArray = Array.from(loadedPages.value)
      64            0 :     const nextPage = Math.max(...loadedPagesArray, 0) + 1
      65              : 
      66              :     // Simulate network delay
      67              :     // await new Promise((resolve) => setTimeout(resolve, 5000))
      68              : 
      69            0 :     const newLoadedPages = new Set(loadedPages.value)
      70            0 :     newLoadedPages.add(nextPage)
      71            0 :     loadedPages.value = newLoadedPages
      72            0 :     currentPage.value = nextPage
      73            0 :     isLoading.value = false
      74              :   }
      75              : 
      76              :   // Initialize with first page
      77           26 :   watch(
      78              :     () => itemsArray.value.length,
      79              :     (length) => {
      80           53 :       if (length > 0 && loadedPages.value.size === 0) {
      81           24 :         loadedPages.value = new Set([1])
      82              :       }
      83              :     },
      84              :     { immediate: true }
      85              :   )
      86              : 
      87           26 :   const reset = () => {
      88          103 :     currentPage.value = initialPage
      89          103 :     loadedPages.value = new Set([])
      90          103 :     isLoading.value = false
      91              : 
      92              :     // Immediately load first page if we have items
      93              :     const itemData = itemsArray.value
      94          103 :     if (itemData.length > 0) {
      95           56 :       loadedPages.value = new Set([1])
      96              :     }
      97              :   }
      98              : 
      99           26 :   return {
     100              :     paginatedItems,
     101              :     isLoading,
     102              :     hasMoreItems,
     103              :     currentPage,
     104              :     totalPages,
     105              :     loadNextPage,
     106              :     reset
     107              :   }
     108              : }
        

Generated by: LCOV version 2.0-1