aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-09-30 19:27:26 +0000
committerAdam Soltys <[email protected]>2026-09-30 19:27:26 +0000
commitbe6e447c8960e0b74308137efa10b45d0763b922 (patch)
treee6e203918e1ce10e91caa22b10794c6862f1b985 /src
parent0879e4f55dcee9bb7af86b1e4e2a62ee9d6ef3b6 (diff)
downloadcoinosv3-be6e447c8960e0b74308137efa10b45d0763b922.tar.gz
coinosv3-be6e447c8960e0b74308137efa10b45d0763b922.zip
Switching feeds paints posts at once instead of a spinner while the relays answer.
The disk cache held four feeds, so with more chips than that (or one never opened here) a switch opened on nothing. Now: - up to twelve feeds on disk: the four read last keep a page of 30, the rest 5 seed posts (same 256 KB budget); - once the feed on screen settles, every other feed with nothing on disk has its first posts fetched ahead, one small request each, every 15 min; - a feed with nothing on disk borrows matching posts (its people, its topics) that open feeds already hold. tools/feed-switch-seed-test.js: with 8 s relays, a fetched-ahead feed and a never-opened topic feed both paint rows at once (fails on the old code). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01WMMwWYJkPQxLgfMsY7hWcU
Diffstat (limited to 'src')
-rw-r--r--src/features/messages.js41
-rw-r--r--src/feed-cache.js33
2 files changed, 65 insertions, 9 deletions
diff --git a/src/features/messages.js b/src/features/messages.js
index 2f4446e..856a4bd 100644
--- a/src/features/messages.js
+++ b/src/features/messages.js
@@ -31,7 +31,7 @@ import {
import { saveInbox } from '../dm-inbox.js';
import { mergeFeedWindow } from '../feed-window.js';
import { createThreadStore } from '../thread-cache.js';
-import { createFeedCache, FEED_CACHE_POSTS } from '../feed-cache.js';
+import { createFeedCache, FEED_CACHE_POSTS, FEED_SEED_POSTS } from '../feed-cache.js';
import { makeSearcher, resultRows, fallbackAvatar, warmSearch, punkImageUrl, punkSmallUrl } from '../recipient-search.js';
import { getNetwork } from '../api.js';
import { decodeBolt11 } from '../ark/lightning.js';
@@ -4356,6 +4356,7 @@ export function messagesFeature(ctx) {
let stored = [];
try { stored = feedDisk()?.read(curFeedId) || wallet.loadFeatureState(feedCacheKey(curFeedId), []) || []; } catch {}
if (!Array.isArray(stored)) stored = [];
+ if (!stored.length) stored = borrowedNotes(feedDef(curFeedId));
stored = stored.slice(0, FEED_STORE);
c = { id: curFeedId, status: 'loading', notes: stored, shown: FEED_PAGE, at: 0, booting: true, presentations: new Map() };
feedStates.set(curFeedId, c);
@@ -4401,10 +4402,46 @@ export function messagesFeature(ctx) {
} catch {} finally {
if (!c.stopped) {
c.status = 'ready';
- if (c === feed && ui.chatOpen && ui.msgView === 'feed') { scheduleRepaint(); watchFeed(); }
+ if (c === feed && ui.chatOpen && ui.msgView === 'feed') { scheduleRepaint(); watchFeed(); seedOtherFeeds(); }
}
}
}
+ // A feed never opened on this device (or pushed out of the disk cache)
+ // would open on a bare spinner. Posts another open feed already holds —
+ // by its people, on its topics — stand in until the relays answer.
+ function borrowedNotes(def) {
+ if (!def || def.all) return [];
+ const authors = new Set(feedAuthors(def)), topics = new Set(feedTopics(def));
+ if (!authors.size && !topics.size) return [];
+ const fits = (e) => (!authors.size || authors.has(e.pubkey))
+ && (!topics.size || (e.tags || []).some((x) => x[0] === 't' && topics.has(normTopic(x[1]))));
+ const seen = new Set(), out = [];
+ for (const o of feedStates.values()) {
+ for (const e of o.notes) if (!seen.has(e.id) && fits(e) && !hidden(e)) { seen.add(e.id); out.push(e); }
+ }
+ return out.sort((a, b) => b.created_at - a.created_at).slice(0, FEED_PAGE);
+ }
+ // And once the feed on screen has settled, the other feeds' first few
+ // posts are fetched ahead into the disk cache, one feed at a time, so
+ // switching to any of them paints at once.
+ const SEED_EVERY = 15 * 60_000;
+ let feedsSeededAt = 0;
+ async function seedOtherFeeds() {
+ const disk = feedDisk();
+ if (!disk || isVisitor() || Date.now() - feedsSeededAt < SEED_EVERY) return;
+ feedsSeededAt = Date.now();
+ for (const def of feedList()) {
+ if (def.curated || def.of || feedStates.has(def.id) || disk.has(def.id)) continue;
+ const authors = feedAuthors(def).slice(0, REQ_AUTHORS), topics = feedTopics(def);
+ if (!authors.length && !topics.length) continue;
+ try {
+ const events = await queryOn(feedRelaysOr(def, NOTE_RELAYS), { kinds: FEED_KINDS, limit: FEED_SEED_POSTS * 2,
+ ...(authors.length ? { authors } : {}), ...(topics.length ? { '#t': topics } : {}) }, 3000);
+ if (feedStates.has(def.id)) continue; // opened while we were asking
+ disk.seed(def.id, events.filter((e) => FEED_KINDS.includes(e.kind) && !isReply(e) && !hidden(e)));
+ } catch {}
+ }
+ }
// Make a feed the current one (no paint): Back/Forward through the
// history restores ui.feedId and the view catches up from here.
function selectFeed(id) {
diff --git a/src/feed-cache.js b/src/feed-cache.js
index 7394393..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 || 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 {}
+ },
};
}