c96c24a250
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import { clearCachedFeed, loadCachedFeed, saveCachedFeed } from "@/shared/db/feed-cache-db"
|
|
import type { PGlite } from "@electric-sql/pglite"
|
|
import type { FeedItem } from "./assemble-feed"
|
|
|
|
export interface FeedCacheData {
|
|
items: FeedItem[]
|
|
updatedAt: number
|
|
}
|
|
|
|
export async function loadFeedCache(db: PGlite): Promise<FeedCacheData | null> {
|
|
return loadCachedFeed(db)
|
|
}
|
|
|
|
export async function saveFeedCache(db: PGlite, items: FeedItem[]): Promise<void> {
|
|
await saveCachedFeed(db, items)
|
|
}
|
|
|
|
export async function clearFeedCache(db: PGlite): Promise<void> {
|
|
await clearCachedFeed(db)
|
|
}
|
|
|
|
export function mergeFeedItems(cached: FeedItem[], fresh: FeedItem[]): FeedItem[] {
|
|
const map = new Map<string, FeedItem>()
|
|
for (const item of cached) map.set(item.id, item)
|
|
for (const item of fresh) map.set(item.id, item)
|
|
|
|
return Array.from(map.values()).sort((a, b) => {
|
|
if (a.date && b.date) return b.date.localeCompare(a.date)
|
|
if (!a.date && b.date) return 1
|
|
if (a.date && !b.date) return -1
|
|
return a.title.localeCompare(b.title)
|
|
})
|
|
}
|