LCOV - code coverage report
Current view: top level - src/utils - pagedList.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 86.3 % 73 63
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { computed, onScopeDispose, ref, toValue } from 'vue'
       2              : import type { MaybeRef } from 'vue'
       3              : 
       4              : export interface PagedList<T> {
       5              :   hasMore: Readonly<MaybeRef<boolean>>
       6              :   invalidate: (items?: string[]) => Promise<void>
       7              :   isLoading: Readonly<MaybeRef<boolean>>
       8              :   items: Readonly<MaybeRef<readonly T[]>>
       9              :   /** Returns whether pagination advanced. Page-walking callers must stop on false. */
      10              :   loadMore: () => Promise<boolean>
      11              :   loadNew: () => Promise<void>
      12              : }
      13              : 
      14              : export type MaybePaged<T> = readonly T[] | PagedList<T>
      15         2788 : export function isPaged<T>(list: MaybePaged<T>): list is PagedList<T> {
      16         2788 :   return !Array.isArray(list)
      17              : }
      18         2584 : export function pagedItems<T>(list: MaybePaged<T>): readonly T[] {
      19         2584 :   return isPaged(list) ? toValue(list.items) : list
      20              : }
      21              : 
      22         2356 : export class WrappedList<T, U> implements PagedList<U> {
      23              :   readonly items: MaybeRef<readonly U[]>
      24              :   constructor(
      25         5522 :     private readonly childList: PagedList<T>,
      26         5522 :     private readonly transform: (items: readonly T[]) => readonly U[]
      27              :   ) {
      28         5522 :     this.items = computed(() => this.transform(toValue(this.childList.items)))
      29              :   }
      30              :   get hasMore() {
      31          292 :     return this.childList.hasMore
      32              :   }
      33              :   async invalidate(stale?: string[]) {
      34            1 :     await this.childList.invalidate(stale)
      35              :   }
      36              :   get isLoading() {
      37           48 :     return this.childList.isLoading
      38              :   }
      39              :   loadMore() {
      40           10 :     return this.childList.loadMore()
      41              :   }
      42              :   async loadNew() {
      43            3 :     await this.childList.loadNew()
      44              :   }
      45              : }
      46              : 
      47              : interface CacheEntry<T> {
      48              :   list: PagedList<T>
      49              :   refCount: number
      50              : }
      51              : type Cache<T> = Map<string, CacheEntry<T>>
      52              : 
      53              : export interface SharedPagedListState<TParams, TItem> {
      54              :   readonly cache: Cache<TItem>
      55              :   readonly factory: (params: TParams) => PagedList<TItem>
      56              :   readonly paramKeyFn: (params: TParams) => string
      57              :   readonly itemKeyFn: (item: TItem) => unknown
      58              : }
      59              : 
      60           60 : export function getPagedList<TParams, TItem>(
      61              :   params: TParams,
      62              :   state: SharedPagedListState<TParams, TItem>
      63              : ): PagedList<TItem> {
      64           60 :   const key = state.paramKeyFn(params)
      65           60 :   const entry = state.cache.get(key) ?? {
      66              :     list: state.factory(params),
      67              :     refCount: 0
      68              :   }
      69           60 :   state.cache.set(key, entry)
      70           60 :   entry.refCount++
      71              : 
      72           60 :   onScopeDispose(() => --entry.refCount || state.cache.delete(key))
      73           60 :   return new SharedPagedList(entry.list, state.cache, state.itemKeyFn)
      74              : }
      75              : 
      76         2356 : class SharedPagedList<T> implements PagedList<T> {
      77              :   constructor(
      78           60 :     private readonly childList: PagedList<T>,
      79           60 :     private readonly cache: Cache<T>,
      80           60 :     private readonly itemKeyFn: (item: T) => unknown
      81              :   ) {}
      82              :   overlapping(): PagedList<T>[] {
      83            0 :     const snapshot = toValue(this.childList.items)
      84            0 :     if (snapshot.length === 0) return [this.childList]
      85              : 
      86            0 :     const staleKeys = new Set(snapshot.map(this.itemKeyFn))
      87            0 :     return [...this.cache.values()]
      88              :       .filter((e) =>
      89              :         toValue(e.list.items).some((item) =>
      90              :           staleKeys.has(this.itemKeyFn(item))
      91              :         )
      92              :       )
      93              :       .map((e) => e.list)
      94              :   }
      95              : 
      96              :   get hasMore() {
      97           60 :     return this.childList.hasMore
      98              :   }
      99              :   async invalidate(stale?: string[]) {
     100            0 :     const toInvalidate = stale
     101              :       ? [...this.cache.values()].map((l) => l.list)
     102              :       : this.overlapping()
     103            0 :     await Promise.all(toInvalidate.map((l) => l.invalidate(stale)))
     104              :   }
     105              :   get isLoading() {
     106           60 :     return this.childList.isLoading
     107              :   }
     108              :   get items() {
     109           60 :     return this.childList.items
     110              :   }
     111              :   loadMore() {
     112            5 :     return this.childList.loadMore()
     113              :   }
     114              :   async loadNew() {
     115            0 :     await Promise.all(this.overlapping().map((l) => l.loadNew()))
     116              :   }
     117              : }
     118              : 
     119              : type Runner = (signal: AbortSignal) => Promise<void>
     120         2356 : const PREEMPT_KIND: unique symbol = Symbol()
     121              : type Kind = string | typeof PREEMPT_KIND
     122              : interface Task {
     123              :   kind: Kind
     124              :   promise: Promise<void>
     125              :   resolve: () => void
     126              :   start: () => void
     127              : }
     128              : 
     129           75 : export function usePreemptableQueue() {
     130           75 :   const queue: Task[] = []
     131           75 :   const running = ref(false)
     132           75 :   let controller = new AbortController()
     133              : 
     134           97 :   function makeTask(kind: Kind, runner: Runner): Task {
     135           97 :     let resolve!: () => void
     136           97 :     const promise = new Promise<void>((r) => (resolve = r))
     137              : 
     138           97 :     const start = () =>
     139              :       runner(controller.signal)
     140              :         .catch(() => undefined)
     141              :         .then(resolve)
     142           97 :     return { kind, promise, resolve, start }
     143              :   }
     144              : 
     145           76 :   function enqueue(kind: Kind, runner: Runner): Promise<void> {
     146           76 :     if (queue[0]?.kind === PREEMPT_KIND) return queue[0].promise
     147              : 
     148           76 :     const existing = queue.find((task) => task.kind === kind)
     149           76 :     if (existing) return existing.promise
     150              : 
     151           76 :     const task = makeTask(kind, runner)
     152           76 :     queue.push(task)
     153           76 :     if (queue.length === 1) void startQueue()
     154           76 :     return task.promise
     155              :   }
     156              : 
     157           21 :   async function preempt(runner: () => Promise<void>): Promise<void> {
     158           21 :     if (queue[0]?.kind === PREEMPT_KIND) return queue[0].promise
     159              : 
     160           21 :     const task = makeTask(PREEMPT_KIND, runner)
     161           21 :     controller.abort()
     162           21 :     controller = new AbortController()
     163           21 :     const active = queue.at(0)
     164           21 :     const existing = queue.splice(0, queue.length, task)
     165           21 :     if (active) {
     166            0 :       await active.promise
     167            0 :       queue.push(task)
     168            0 :       for (const task of existing) task.resolve()
     169              :     } else {
     170           21 :       void startQueue()
     171              :     }
     172           21 :     await task.promise
     173              :   }
     174              : 
     175           93 :   async function startQueue() {
     176           93 :     running.value = true
     177           93 :     while (queue.length) {
     178           97 :       await queue[0].start()
     179           97 :       queue.shift()
     180              :     }
     181           93 :     running.value = false
     182              :   }
     183              : 
     184           75 :   return { enqueue, preempt, running }
     185              : }
        

Generated by: LCOV version 2.0-1