2026-01-07 03:10:40 +01:00

37 lines
989 B
TypeScript

/**
* Helper for querying cached objects
* Centralizes the pattern of querying with fallback hash/ID lookup
*/
import { objectCache } from '../objectCache'
import { parseObjectId } from '../urlGenerator'
import type { ObjectType } from '../objectCache'
/**
* Get a cached object by ID with fallback to hash lookup
* Tries by hash first, then by full ID
*/
export async function getCachedObjectById<T>(
objectType: ObjectType,
id: string
): Promise<T | null> {
// Try to parse id as id format (<hash>_<index>_<version>) or use it as hash
const parsed = parseObjectId(id)
const hash = parsed.hash ?? id
// Read only from IndexedDB cache
const cached = await objectCache.get(objectType, hash)
if (cached) {
return cached as T
}
// Also try by ID if hash lookup failed
const cachedById = await objectCache.getById(objectType, id)
if (cachedById) {
return cachedById as T
}
// Not found in cache - return null (no network request)
return null
}