From be6e447c8960e0b74308137efa10b45d0763b922 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 30 Sep 2026 19:27:26 +0000 Subject: 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 Claude-Session: https://claude.ai/code/session_01WMMwWYJkPQxLgfMsY7hWcU --- src/features/messages.js | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'src/features') 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) { -- cgit v1.2.3