search-state.ts (1639B)
1 import type { ListingView } from './listing-view.ts'; 2 import type { ChronologicalProgress } from './continuation.ts'; 3 import type { 4 publicRunSnapshot, 5 publicRequestScopeSnapshot 6 } from '../nostr/request-scope.ts'; 7 import type { HeadResolution } from './resolve-head.ts'; 8 export type SearchScope = ReturnType<typeof publicRequestScopeSnapshot>; 9 export type SearchRefresh = 10 | 'idle' 11 | 'refreshing' 12 | 'bounded-eose' 13 | 'partial' 14 | 'cancelled' 15 | 'deadline' 16 | 'limit' 17 | 'error' 18 | 'unavailable'; 19 export type SearchSnapshot<Generation> = Readonly<{ 20 generation: Generation; 21 query: string; 22 refresh: SearchRefresh; 23 available: boolean; 24 rows: readonly HeadResolution[]; 25 listings: readonly ListingView[]; 26 hasMore: boolean; 27 continuation: ChronologicalProgress; 28 scopes: readonly SearchScope[]; 29 run: ReturnType<typeof publicRunSnapshot> | undefined; 30 definitiveAbsence: false; 31 }>; 32 // A completed bounded set never proves global completeness or absence. 33 export function searchRefreshState( 34 available: boolean, 35 run: SearchSnapshot<unknown>['run'], 36 scopes: readonly SearchScope[], 37 cancelled: boolean, 38 failed: boolean, 39 capped: boolean 40 ): SearchRefresh { 41 if (!available) return 'unavailable'; 42 if (capped || run?.ingress.stopped) return 'limit'; 43 if (cancelled) return 'cancelled'; 44 if (failed) return 'error'; 45 if (run?.activeRequests) return 'refreshing'; 46 if (scopes.some((scope) => scope.state === 'deadline')) return 'deadline'; 47 if (scopes.some((scope) => scope.state !== 'eose')) return 'partial'; 48 if (scopes.length > 0) return 'bounded-eose'; 49 return run?.active ? 'idle' : 'partial'; 50 }