Line data Source code
1 : import { refAutoReset, until } from '@vueuse/core'
2 : import { computed, ref } from 'vue'
3 : import { fromZodError } from 'zod-validation-error'
4 : import type { ListAssetsData } from '@comfyorg/ingest-types'
5 :
6 : import { assetResponseSchema } from '@/platform/assets/schemas/assetSchema'
7 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
8 : import { api } from '@/scripts/api'
9 : import { getPagedList, usePreemptableQueue } from '@/utils/pagedList'
10 : import type { SharedPagedListState, PagedList } from '@/utils/pagedList'
11 : import { encodeParams } from '@/utils/requestUtil'
12 :
13 : interface QueryOptions {
14 : onError?: (reason: string, error?: unknown) => void
15 : }
16 :
17 2356 : const BASE_PARAMS = {
18 : include_public: false,
19 : include_total: false,
20 : limit: 100,
21 : sort: 'created_at',
22 : tags_none: ['missing']
23 : } satisfies ListAssetsData['query']
24 :
25 60 : function assetsQueryInternal(
26 : params: ListAssetsData['query'] = {},
27 : options: QueryOptions = {}
28 : ): PagedList<AssetItem> {
29 60 : const onError = options.onError ?? console.error
30 :
31 60 : let nextCursor: string | undefined
32 60 : const seenCursors = new Set<string | undefined>()
33 60 : let loadGeneration = 0
34 60 : const morePages = ref(true)
35 60 : const backingOff = refAutoReset(false, 2000)
36 60 : const hasMore = computed(() => morePages.value && !backingOff.value)
37 60 : const items = ref<AssetItem[]>([])
38 :
39 60 : const { enqueue, preempt, running: isLoading } = usePreemptableQueue()
40 60 : let loadMorePromise: Promise<boolean> | undefined
41 60 : let invalidationQueue = Promise.resolve()
42 68 : async function doLoadMore(signal?: AbortSignal) {
43 68 : if (!hasMore.value) return
44 68 : const requestedCursor = nextCursor ?? params.after
45 68 : if (seenCursors.has(requestedCursor)) return
46 :
47 68 : const assetResponse = await doQuery(
48 : {
49 : after: requestedCursor
50 : },
51 : signal
52 : )
53 68 : if (!assetResponse) return
54 :
55 65 : const knownIds = new Set(items.value.map(({ id }) => id))
56 65 : const newItems = assetResponse.assets.filter(({ id }) => {
57 233 : if (knownIds.has(id)) return false
58 233 : knownIds.add(id)
59 233 : return true
60 : })
61 65 : seenCursors.add(requestedCursor)
62 65 : nextCursor = assetResponse.next_cursor
63 68 : morePages.value =
64 : assetResponse.has_more &&
65 : nextCursor !== undefined &&
66 : !seenCursors.has(nextCursor)
67 68 : items.value.push(...newItems)
68 68 : loadGeneration++
69 : }
70 :
71 65 : function loadMore() {
72 65 : if (!loadMorePromise) {
73 65 : const startingGeneration = loadGeneration
74 : const operation = enqueue('loadMore', doLoadMore).then(
75 : () => loadGeneration > startingGeneration
76 : )
77 65 : loadMorePromise = operation.finally(() => {
78 65 : loadMorePromise = undefined
79 : })
80 : }
81 65 : return loadMorePromise
82 : }
83 :
84 9 : function loadNew() {
85 9 : return enqueue('loadNew', async function (signal: AbortSignal) {
86 9 : const knownIds = new Set(items.value.map((item) => item.id))
87 9 : const seenIds = new Set(knownIds)
88 9 : const newItems: AssetItem[] = []
89 9 : let headCursor: string | undefined
90 9 : const seenHeadCursors = new Set<string | undefined>()
91 9 : for (;;) {
92 9 : if (seenHeadCursors.has(headCursor)) break
93 9 : seenHeadCursors.add(headCursor)
94 :
95 : const query = headCursor
96 : ? { after: headCursor }
97 : : { after: headCursor, limit: 10 }
98 9 : const assetResponse = await doQuery(query, signal)
99 9 : if (!assetResponse) return
100 :
101 9 : const { assets, has_more, next_cursor } = assetResponse
102 : const reachedKnownId = assets.some((asset) => {
103 9 : if (knownIds.has(asset.id)) return true
104 0 : if (!seenIds.has(asset.id)) {
105 0 : seenIds.add(asset.id)
106 0 : newItems.push(asset)
107 : }
108 0 : return false
109 : })
110 9 : if (reachedKnownId || !has_more || next_cursor === undefined) break
111 0 : headCursor = next_cursor
112 : }
113 9 : items.value.splice(0, 0, ...newItems)
114 : })
115 : }
116 :
117 3 : async function applyInvalidation(stale?: string[]) {
118 3 : if (stale) {
119 0 : await preempt(() => Promise.resolve())
120 0 : const ids = new Set(stale)
121 0 : items.value = items.value.filter((item) => !ids.has(item.id))
122 0 : return
123 : }
124 3 : await preempt(async () => {
125 3 : morePages.value = true
126 3 : nextCursor = undefined
127 3 : seenCursors.clear()
128 3 : items.value = []
129 3 : await until(backingOff).toBe(false)
130 3 : await doLoadMore()
131 : })
132 : }
133 :
134 3 : function invalidate(stale?: string[]) {
135 3 : const operation = invalidationQueue.then(() => applyInvalidation(stale))
136 3 : invalidationQueue = operation
137 3 : return operation
138 : }
139 :
140 77 : async function doQuery(
141 : overrideParams: ListAssetsData['query'],
142 : signal?: AbortSignal
143 : ) {
144 77 : const requestOptions = { signal }
145 77 : const query = encodeParams({ ...BASE_PARAMS, ...params, ...overrideParams })
146 77 : const resp = await api
147 : .fetchApi(`/assets?${query}`, requestOptions)
148 : .catch((e) => onError('asset fetch failed', e))
149 :
150 77 : if (!resp) {
151 0 : if (!signal?.aborted) backingOff.value = true
152 0 : return
153 : }
154 77 : if (!resp.ok) {
155 3 : onError('asset request failed', resp)
156 3 : if (resp.status === 429 || resp.status >= 500) backingOff.value = true
157 0 : else morePages.value = false
158 3 : return
159 : }
160 :
161 74 : const jsonresp = await resp
162 : .json()
163 : .catch((e) => onError('failed to decode asset json response', e))
164 74 : if (!jsonresp) {
165 0 : morePages.value = false
166 0 : return
167 : }
168 :
169 74 : const parseResult = assetResponseSchema.safeParse(jsonresp)
170 74 : if (!parseResult.success) {
171 0 : onError('Failed to parse asset response', fromZodError(parseResult.error))
172 0 : morePages.value = false
173 0 : return
174 : }
175 74 : return parseResult.data
176 : }
177 :
178 60 : void loadMore()
179 60 : return {
180 : hasMore,
181 : invalidate,
182 : isLoading,
183 : items,
184 : loadMore,
185 : loadNew
186 : }
187 : }
188 :
189 2356 : const sharedState: SharedPagedListState<ListAssetsData['query'], AssetItem> = {
190 : cache: new Map(),
191 : factory: assetsQueryInternal,
192 : paramKeyFn: encodeParams,
193 : itemKeyFn: (item) => item.id
194 : }
195 :
196 60 : export function useAssetsQuery(
197 : params: ListAssetsData['query']
198 : ): PagedList<AssetItem> {
199 60 : const list = getPagedList(params, sharedState)
200 60 : return {
201 : hasMore: list.hasMore,
202 : isLoading: list.isLoading,
203 : items: list.items,
204 : loadMore: () => list.loadMore(),
205 : invalidate: invalidateAll,
206 : loadNew: loadNewAll
207 : }
208 : }
209 :
210 10 : export async function invalidateAll(stale?: string[]) {
211 10 : await Promise.all(
212 : [...sharedState.cache.values()].map((e) => e.list.invalidate(stale))
213 : )
214 : }
215 :
216 3 : export async function loadNewAll() {
217 3 : await Promise.all(
218 : [...sharedState.cache.values()].map((e) => e.list.loadNew())
219 : )
220 : }
|