diff options
| author | Adam Soltys <[email protected]> | 2026-10-01 06:49:19 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-10-01 06:49:19 -0700 |
| commit | 68ff290e07c0e1786cf8b8f3a87e38872ad7f8ba (patch) | |
| tree | c85847c8f4a06b0a6b7dab8abb2d2eb54fefa226 /src/feed-cache.js | |
| parent | 4b3291fdbd0b34b4d2df91a59bea93885de29508 (diff) | |
| parent | 5ff909d37fe32ba04a841941593d95c7b3498ec3 (diff) | |
| download | coinosv3-68ff290e07c0e1786cf8b8f3a87e38872ad7f8ba.tar.gz coinosv3-68ff290e07c0e1786cf8b8f3a87e38872ad7f8ba.zip | |
Merge prod into master
Diffstat (limited to 'src/feed-cache.js')
| -rw-r--r-- | src/feed-cache.js | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/src/feed-cache.js b/src/feed-cache.js index ea562f7..2c0b153 100644 --- a/src/feed-cache.js +++ b/src/feed-cache.js @@ -1,17 +1,20 @@ // Disposable public feed cache. Budget is conservative UTF-16 storage bytes. +// The feeds read most recently keep a full page; the rest keep a few seed +// posts, so switching to any feed paints something before the relays answer. export const FEED_CACHE_POSTS = 30; -const MAX_FEEDS = 4, MAX_UNITS = 256_000, MAX_AGE = 7 * 86400_000; +export const FEED_SEED_POSTS = 5; +const FULL_FEEDS = 4, MAX_FEEDS = 12, MAX_UNITS = 256_000, MAX_AGE = 7 * 86400_000; export function createFeedCache(storage, prefix, now = Date.now) { const keyOf = (id) => id === 'following' ? prefix : prefix + ':' + id; - const valid = (e) => e?.kind === 1 && typeof e.id === 'string' && typeof e.pubkey === 'string' + const valid = (e) => (e?.kind === 1 || e?.kind === 1068) && typeof e.id === 'string' && typeof e.pubkey === 'string' && typeof e.content === 'string' && Number.isFinite(e.created_at) && Array.isArray(e.tags); - function compact(notes) { + function compact(notes, max = FEED_CACHE_POSTS) { const kept = []; let size = 0; for (const e of (Array.isArray(notes) ? notes : []).filter(valid).sort((a, b) => b.created_at - a.created_at)) { const cost = JSON.stringify(e).length; if (size + cost > MAX_UNITS - 1000) continue; kept.push(e); size += cost; - if (kept.length === FEED_CACHE_POSTS) break; + if (kept.length === max) break; } return kept; } @@ -23,19 +26,24 @@ export function createFeedCache(storage, prefix, now = Date.now) { try { const text = storage.getItem(key); const raw = JSON.parse(text); - const entry = Array.isArray(raw) ? { at: now(), notes: compact(raw) } : raw; + const entry = Array.isArray(raw) ? { at: now(), notes: raw } : raw; if (!entry || !Number.isFinite(entry.at) || now() - entry.at > MAX_AGE) entries.push({ key }); - else entries.push({ key, text, value: { at: entry.at, notes: compact(entry.notes) } }); + else entries.push({ key, text, entry }); } catch { entries.push({ key }); } } - entries.sort((a, b) => (b.key === preferred) - (a.key === preferred) || (b.value?.at || 0) - (a.value?.at || 0)); + // A feed that was read beats one only warmed in the background (seed). + entries.sort((a, b) => (b.key === preferred) - (a.key === preferred) + || (!a.entry?.seed) - (!b.entry?.seed) || (b.entry?.at || 0) - (a.entry?.at || 0)); let units = 0, count = 0; for (const e of entries) { - const json = e.value && JSON.stringify(e.value); + const max = count < FULL_FEEDS && !e.entry?.seed ? FEED_CACHE_POSTS : FEED_SEED_POSTS; + const value = e.entry && { at: e.entry.at, notes: compact(e.entry.notes, max), ...(e.entry.seed ? { seed: true } : {}) }; + const json = value && JSON.stringify(value); if (!json || count >= MAX_FEEDS || units + json.length > MAX_UNITS) storage.removeItem(e.key); else { if (e.text !== json) storage.setItem(e.key, json); units += json.length; count++; } } } + const peek = (id) => { try { return JSON.parse(storage.getItem(keyOf(id)) || 'null'); } catch { return null; } }; return { read(id) { try { @@ -49,5 +57,16 @@ export function createFeedCache(storage, prefix, now = Date.now) { save(id, notes) { try { storage.setItem(keyOf(id), JSON.stringify({ at: now(), notes: compact(notes) })); sweep(keyOf(id)); } catch {} }, + // Whether a feed already has posts on disk (without counting as a read). + has: (id) => !!peek(id)?.notes?.length, + // A few posts fetched ahead for a feed not yet opened. Never replaces + // posts a read left behind, and ranks below every feed that was read. + seed(id, notes) { + try { + if (peek(id)?.notes?.length) return; + storage.setItem(keyOf(id), JSON.stringify({ at: now(), seed: true, notes: compact(notes, FEED_SEED_POSTS) })); + sweep(null); + } catch {} + }, }; } |
