Spaces:
Running
Running
| import type { BucketDataState } from '$lib/types/bucket-state'; | |
| import type { DetailsDataset } from '$lib/types/details-data'; | |
| import { adaptBucketDetails } from './adapt-details'; | |
| import { fetchBucketDetailsSnapshot } from './details-snapshot'; | |
| import { emptyDetailsDataset } from './fallback-data'; | |
| import { toPublicBucketError } from './public-error'; | |
| import { fetchBucketRankingSnapshot } from './ranking-snapshot'; | |
| let cached: { value: DetailsDataset; snapshotId: string } | undefined; | |
| let inFlight: Promise<DetailsDataset> | undefined; | |
| async function refreshDetails() { | |
| const rankingSnapshot = await fetchBucketRankingSnapshot({ refreshManifest: true }); | |
| if (cached?.snapshotId === rankingSnapshot.manifest.snapshot_id) { | |
| return cached.value; | |
| } | |
| const value = adaptBucketDetails(await fetchBucketDetailsSnapshot(rankingSnapshot)); | |
| cached = { | |
| value, | |
| snapshotId: rankingSnapshot.manifest.snapshot_id | |
| }; | |
| return value; | |
| } | |
| export async function getHfBucketDetails() { | |
| const state = await getHfBucketDetailsState(); | |
| if (state.status === 'unavailable') throw new Error(state.error.message); | |
| return state.data; | |
| } | |
| export async function getHfBucketDetailsState(): Promise<BucketDataState<DetailsDataset>> { | |
| if (!inFlight) { | |
| inFlight = refreshDetails().finally(() => { | |
| inFlight = undefined; | |
| }); | |
| } | |
| try { | |
| return { | |
| status: 'ok', | |
| source: 'remote', | |
| data: await inFlight | |
| }; | |
| } catch (error) { | |
| if (cached) { | |
| console.warn('[hf-bucket] Serving stale Details data after refresh failure.', error); | |
| return { | |
| status: 'stale', | |
| source: 'stale', | |
| data: cached.value, | |
| error: toPublicBucketError(error) | |
| }; | |
| } | |
| console.error('[hf-bucket] Details data is unavailable.', error); | |
| return { | |
| status: 'unavailable', | |
| source: 'unavailable', | |
| data: emptyDetailsDataset(), | |
| error: toPublicBucketError(error) | |
| }; | |
| } | |
| } | |