flotilla/src/app/feeds.ts

804 lines
22 KiB
TypeScript
Raw Normal View History

2026-08-30 00:42:07 +00:00
import {derived, get, readable, writable} from "svelte/store"
import type {Readable} from "svelte/store"
import {batch, call, int, ms, now, on, sleep, uniqBy, MONTH, YEAR} from "@welshman/lib"
import {
COMMENT,
DELETE,
EVENT_TIME,
NOTE,
addressTags,
compareEventsAsc,
getAddress,
getCommentFiltersForRoot,
2026-08-31 17:03:01 +00:00
getIdOrAddress,
getReplyFilters,
hexTags,
isReplaceableKind,
matchFilters,
tagSpec,
tagValue,
tagValues,
} from "@welshman/util"
2026-08-30 00:42:07 +00:00
import type {Maybe} from "@welshman/lib"
import type {Filter, TrustedEvent} from "@welshman/util"
2026-07-28 16:16:26 +00:00
import {mergeRepositoryUpdates} from "@welshman/net"
import type {RepositoryUpdate} from "@welshman/net"
2025-01-28 16:13:20 +00:00
import {createScroller} from "@lib/html"
2026-08-30 00:42:07 +00:00
import type {ScrollerOpts} from "@lib/html"
2025-02-06 00:26:22 +00:00
import {daysBetween} from "@lib/util"
import {EVENT_CONTEXT_KINDS, REACTION_KINDS} from "@app/content"
2026-07-28 16:16:26 +00:00
import {app, network} from "@app/core"
import {getEventsForUrl} from "@app/repository"
2024-12-11 00:38:22 +00:00
const noEvents: TrustedEvent[] = []
const mergeSorted = <T>(left: T[], right: T[], compare: (a: T, b: T) => number) => {
const merged: T[] = []
let i = 0
let j = 0
while (i < left.length && j < right.length) {
merged.push(compare(left[i], right[j]) <= 0 ? left[i++] : right[j++])
}
2026-09-21 16:48:35 +00:00
while (i < left.length) {
merged.push(left[i++])
}
while (j < right.length) {
merged.push(right[j++])
}
return merged
}
// A NIP-22 comment points at its thread root with `E`/`A`, which is the scope a reply count wants.
const getTargets = ({kind, tags}: TrustedEvent) =>
kind === COMMENT
? [...tagValues(hexTags("E"), tags), ...tagValues(addressTags("A"), tags)]
: [...tagValues(hexTags("e"), tags), ...tagValues(addressTags("a"), tags)]
// A related event may point at a replaceable one by either id or address, so both are keys
const getKeys = (event: TrustedEvent) =>
isReplaceableKind(event.kind) ? [event.id, getAddress(event)] : [event.id]
export const makeFeedContext = ({
relays,
withReplies = false,
}: {
relays: string[] | Promise<string[]>
withReplies?: boolean
}) => {
// Kind 1 notes reply with `e` tags rather than as NIP-22 comments.
const contextKinds = withReplies ? [...EVENT_CONTEXT_KINDS, NOTE] : EVENT_CONTEXT_KINDS
const requestKinds = withReplies ? [...REACTION_KINDS, NOTE] : REACTION_KINDS
const {repository, tracker} = app.get()
const controller = new AbortController()
const targets = new Set<string>()
const eventsByTarget = new Map<string, TrustedEvent[]>()
const targetsByEventId = new Map<string, string[]>()
const subscribersByTarget = new Map<string, Set<(events: TrustedEvent[]) => void>>()
2026-08-31 17:03:01 +00:00
const deletedChecks = new Map<string, Set<() => void>>()
const addEvent = (event: TrustedEvent, touched: Set<string>) => {
// An event seen before its target stays unfiled, so adding the target picks it up from the repository.
if (!targetsByEventId.has(event.id)) {
const eventTargets = getTargets(event).filter(target => targets.has(target))
if (eventTargets.length > 0) {
targetsByEventId.set(event.id, eventTargets)
for (const target of eventTargets) {
eventsByTarget.set(target, [...(eventsByTarget.get(target) || []), event])
touched.add(target)
}
}
}
}
const removeEvent = (id: string, touched: Set<string>) => {
const eventTargets = targetsByEventId.get(id)
if (eventTargets) {
targetsByEventId.delete(id)
for (const target of eventTargets) {
const events = eventsByTarget.get(target)!.filter(event => event.id !== id)
if (events.length > 0) {
eventsByTarget.set(target, events)
} else {
eventsByTarget.delete(target)
}
touched.add(target)
}
}
}
2026-08-31 17:03:01 +00:00
const notifyDeleted = (added: TrustedEvent[], removed: Set<string>) => {
if (deletedChecks.size > 0) {
if (removed.size > 0) {
for (const checks of deletedChecks.values()) {
2026-09-21 16:48:35 +00:00
for (const check of checks) {
check()
}
2026-08-31 17:03:01 +00:00
}
} else {
for (const event of added) {
for (const check of deletedChecks.get(getIdOrAddress(event)) || []) {
check()
}
}
}
}
}
const notify = (touched: Set<string>) => {
for (const target of touched) {
for (const subscriber of subscribersByTarget.get(target) || []) {
subscriber(eventsByTarget.get(target) || noEvents)
}
}
}
const loadFrom = async (urls: string[], events: TrustedEvent[]) => {
2026-09-21 17:58:14 +00:00
const context = await network.get().loadComplete({
relays: urls,
signal: controller.signal,
filters: [
...getReplyFilters(events, {kinds: requestKinds}),
...getCommentFiltersForRoot(events),
],
})
if (context.length > 0) {
2026-09-21 17:58:14 +00:00
network.get().loadLenient({
relays: urls,
signal: controller.signal,
filters: getReplyFilters(context, {kinds: [DELETE]}),
})
}
}
const loadContext = batch(100, async (events: TrustedEvent[]) => {
const touched = new Set<string>()
// Local events never come through the update listener, so file them before asking the network.
for (const event of repository.query([
...getReplyFilters(events, {kinds: contextKinds}),
...getCommentFiltersForRoot(events),
])) {
addEvent(event, touched)
}
notify(touched)
const urls = await relays
// A feed built out of other people's outboxes has no relay holding the whole conversation.
const eventsByRelay = new Map<string, TrustedEvent[]>()
for (const event of events) {
for (const url of tracker.getRelays(event.id)) {
if (!urls.includes(url)) {
eventsByRelay.set(url, [...(eventsByRelay.get(url) || []), event])
}
}
}
await Promise.all([
loadFrom(urls, events),
...Array.from(eventsByRelay, ([url, seenThere]) => loadFrom([url], seenThere)),
])
})
const unsubscribe = on(
repository,
"update",
batch(150, (updates: RepositoryUpdate[]) => {
const {added, removed} = mergeRepositoryUpdates(updates)
const touched = new Set<string>()
for (const event of added) {
if (contextKinds.includes(event.kind)) {
addEvent(event, touched)
}
}
for (const id of removed) {
removeEvent(id, touched)
}
notify(touched)
2026-08-31 17:03:01 +00:00
notifyDeleted(added, removed)
}),
)
// Track an event so its context gets requested with the rest of the batch
const add = (event: TrustedEvent) => {
if (!targets.has(event.id)) {
for (const key of getKeys(event)) {
targets.add(key)
}
loadContext(event)
}
}
const relatedForKey = (key: string) =>
readable(eventsByTarget.get(key) || noEvents, set => {
let subscribers = subscribersByTarget.get(key)
if (!subscribers) {
subscribers = new Set()
subscribersByTarget.set(key, subscribers)
}
subscribers.add(set)
set(eventsByTarget.get(key) || noEvents)
return () => {
subscribers.delete(set)
if (subscribers.size === 0) {
subscribersByTarget.delete(key)
}
}
})
return {
add,
2026-08-31 17:03:01 +00:00
deleted: (event: TrustedEvent) =>
readable(repository.isDeleted(event), set => {
const key = getIdOrAddress(event)
const check = () => set(repository.isDeleted(event))
let checks = deletedChecks.get(key)
if (!checks) {
checks = new Set()
deletedChecks.set(key, checks)
}
checks.add(check)
check()
return () => {
checks.delete(check)
if (checks.size === 0) {
deletedChecks.delete(key)
}
}
}),
related: (event: TrustedEvent): Readable<TrustedEvent[]> => {
add(event)
const [key, ...rest] = getKeys(event)
// A replaceable event collects both buckets, and something tagging it by id and address lands in both.
return rest.length > 0
? derived([relatedForKey(key), ...rest.map(relatedForKey)], buckets =>
uniqBy(event => event.id, buckets.flat()),
)
: relatedForKey(key)
},
cleanup: () => {
controller.abort()
unsubscribe()
},
}
}
export type FeedContext = ReturnType<typeof makeFeedContext>
// Keeps a feed's store in step with the repository: later arrivals, deletions, and events newly seen.
2026-08-30 00:42:07 +00:00
const syncFeed = ({
2026-06-29 23:28:11 +00:00
relays,
2025-10-06 18:23:19 +00:00
filters,
addEvents,
removeEvents,
2026-08-30 00:42:07 +00:00
requireRelay = true,
2025-01-28 16:13:20 +00:00
}: {
2026-06-29 23:28:11 +00:00
relays: string[]
2025-10-06 18:23:19 +00:00
filters: Filter[]
addEvents: (events: TrustedEvent[]) => void
removeEvents: (ids: Set<string>) => void
2026-08-30 00:42:07 +00:00
// Whether an event has to have been seen on one of `relays` to belong to this feed
requireRelay?: boolean
2025-01-28 16:13:20 +00:00
}) => {
2026-07-28 00:13:14 +00:00
const onTrackedId = batch(150, (ids: string[]) => {
const matching: TrustedEvent[] = []
for (const id of new Set(ids)) {
2026-07-28 16:16:26 +00:00
const event = app.get().repository.getEvent(id)
2026-07-28 00:13:14 +00:00
if (event && matchFilters(filters, event)) {
matching.push(event)
}
}
if (matching.length > 0) {
addEvents(matching)
2026-07-28 00:13:14 +00:00
}
})
2026-08-30 00:42:07 +00:00
return [
2026-04-10 18:09:26 +00:00
on(
2026-07-28 16:16:26 +00:00
app.get().repository,
2026-04-10 18:09:26 +00:00
"update",
batch(150, (updates: RepositoryUpdate[]) => {
const {added, removed} = mergeRepositoryUpdates(updates)
if (removed.size > 0) {
removeEvents(removed)
2026-04-10 18:09:26 +00:00
}
2026-02-13 22:51:56 +00:00
2026-04-10 18:09:26 +00:00
const matching = added.filter(
2026-06-29 23:28:11 +00:00
event =>
matchFilters(filters, event) &&
2026-08-30 00:42:07 +00:00
(!requireRelay || relays.some(url => app.get().tracker.getRelays(event.id).has(url))),
2026-04-10 18:09:26 +00:00
)
2026-04-10 18:09:26 +00:00
if (matching.length > 0) {
addEvents(matching)
2026-04-10 18:09:26 +00:00
}
}),
),
2026-07-28 16:16:26 +00:00
on(app.get().tracker, "add", (id: string, url: string) => {
2026-06-29 23:28:11 +00:00
if (relays.includes(url)) {
2026-07-28 00:13:14 +00:00
onTrackedId(id)
2026-02-13 23:18:46 +00:00
}
}),
]
2026-08-30 00:42:07 +00:00
}
// One direction of a feed. An empty span is a gap in the timeline rather than the end of it.
2026-08-30 00:42:07 +00:00
export type FeedLoadState =
| {status: "idle"}
| {status: "loading"}
| {status: "searching"}
| {status: "exhausted"}
// One span of a feed's timeline. `complete` is whether the relays answered, which a dropped request is not.
2026-09-01 17:29:14 +00:00
export type FeedSpan = {found: number; complete: boolean; exhausted: boolean}
// Relatively high because quiet relays eose first.
const SPAN_THRESHOLD = 0.8
// Short because a relay can accept a socket and never eose.
const SPAN_TIMEOUT = 3000
// Abort resolves a request with what arrived.
const spanSignal = (signal: AbortSignal) =>
AbortSignal.any([signal, AbortSignal.timeout(SPAN_TIMEOUT)])
// Enough to cross a gap, not enough to reach the end of the history in one trigger.
2026-08-30 00:42:07 +00:00
const SPANS_PER_TRIGGER = 3
// A month-wide span is thousands of events in a busy feed, so ask for a page and let relays answer from the anchor.
const PAGE_SIZE = 100
// A span that finds nothing widens the next one, so a sparse history takes fewer round trips.
const nextInterval = (interval: number, found: number) =>
found > 0 ? int(MONTH) : Math.round(interval * 1.5)
// Whether a request is in flight, which is not whether more might exist.
2026-08-30 00:42:07 +00:00
export const isFeedLoading = (state: Maybe<FeedLoadState>) =>
state?.status === "loading" || state?.status === "searching"
2026-09-01 17:29:14 +00:00
const makeFeedLoader = (load: () => Promise<FeedSpan>) => {
2026-08-30 00:42:07 +00:00
const state = writable<FeedLoadState>({status: "idle"})
let running = false
const run = async () => {
2026-09-21 16:48:35 +00:00
if (running || get(state).status === "exhausted") {
return
}
2026-08-30 00:42:07 +00:00
running = true
try {
for (let span = 0; span < SPANS_PER_TRIGGER; span++) {
state.set({status: span > 0 ? "searching" : "loading"})
2026-09-01 17:29:14 +00:00
const {found, complete, exhausted} = await load()
2026-08-30 00:42:07 +00:00
if (exhausted) {
state.set({status: "exhausted"})
return
}
// A span nobody answered for has not moved the window, so hold here rather than walking past it.
2026-09-01 17:29:14 +00:00
if (!complete) {
await sleep(ms(3))
break
2026-08-30 00:42:07 +00:00
}
2026-09-01 17:29:14 +00:00
2026-09-21 16:48:35 +00:00
if (found > 0) {
break
}
2026-08-30 00:42:07 +00:00
}
} finally {
running = false
}
}
// A run covers a few spans, so settling at the end of one blinks the spinner once per page.
const settle = () => {
if (!running && isFeedLoading(get(state))) {
state.set({status: "idle"})
}
}
return {subscribe: state.subscribe, run, settle}
2026-08-30 00:42:07 +00:00
}
// The container's orientation decides which direction `reverse` reaches, so the caller passes it.
2026-08-30 00:42:07 +00:00
export const makeScrollLoader = (
element: HTMLElement,
2026-09-01 17:29:14 +00:00
load: () => Promise<FeedSpan>,
2026-08-30 00:42:07 +00:00
options: Partial<ScrollerOpts> = {},
) => {
const loader = makeFeedLoader(load)
const scroller = createScroller({
element,
delay: 300,
threshold: 5000,
...options,
onScroll: loader.run,
onSettle: loader.settle,
2026-08-30 00:42:07 +00:00
})
return {subscribe: loader.subscribe, stop: scroller.stop}
}
// Holds what a view has loaded and knows how to ask for the next span, while the view decides when.
2026-08-30 00:42:07 +00:00
export const makeFeed = ({
relays,
filters,
onEvent,
at = now(),
}: {
relays: string[]
filters: Filter[]
onEvent?: (event: TrustedEvent) => void
at?: number
}) => {
const controller = new AbortController()
const events = writable<TrustedEvent[]>([])
const seen = new Set<string>()
// Events from further back than the feed has reached, held rather than rendered as they land.
const held = new Map<string, TrustedEvent>()
// Each direction widens its own span, since what one walks through says nothing about the other.
2026-08-30 00:42:07 +00:00
let oldest = at
let newest = at
let olderInterval = int(MONTH)
let newerInterval = int(MONTH)
2026-08-30 00:42:07 +00:00
// How far back the feed has been answered for, which decides whether an event is ready to render.
let reached = at
// A span reaches only as far as its shallowest relay, so every one of them has to have answered.
const relayCount = new Set(relays).size
2026-08-30 00:42:07 +00:00
const insertEvents = (newEvents: Iterable<TrustedEvent>) => {
const added: TrustedEvent[] = []
for (const event of newEvents) {
if (!seen.has(event.id)) {
seen.add(event.id)
held.delete(event.id)
2026-08-30 00:42:07 +00:00
added.push(event)
onEvent?.(event)
}
}
if (added.length > 0) {
added.sort(compareEventsAsc)
2026-08-30 00:42:07 +00:00
events.update($events => mergeSorted($events, added, compareEventsAsc))
2026-08-30 00:42:07 +00:00
}
}
const addEvents = (newEvents: TrustedEvent[]) => {
const ready: TrustedEvent[] = []
2025-02-04 22:06:05 +00:00
for (const event of newEvents) {
if (!seen.has(event.id) && !held.has(event.id)) {
if (event.created_at >= reached) {
ready.push(event)
} else {
held.set(event.id, event)
}
}
}
insertEvents(ready)
}
const removeEvents = (ids: Set<string>) => {
events.update($events => $events.filter(event => !ids.has(event.id)))
for (const id of ids) {
seen.delete(id)
held.delete(id)
}
}
// Take the feed back to `timestamp`, releasing everything held for the stretch it gains.
const reach = (timestamp: number) => {
if (timestamp < reached) {
reached = timestamp
const ready: TrustedEvent[] = []
for (const [id, event] of held) {
if (event.created_at >= reached) {
held.delete(id)
ready.push(event)
}
}
insertEvents(ready)
}
}
const unsubscribers = syncFeed({relays, filters, addEvents, removeEvents})
// Each relay answers a limit for itself, so a page runs out where the relay that gave least ran out.
const loadSpan = async (extension: Filter, onCover?: (timestamp: number) => void) => {
2026-09-01 17:29:14 +00:00
let complete = false
const pages = new Map<string, {count: number; lowest: number}>()
const countEvent = (event: TrustedEvent, url: string) => {
const page = pages.get(url)
if (page) {
page.count += 1
page.lowest = Math.min(page.lowest, event.created_at)
} else {
pages.set(url, {count: 1, lowest: event.created_at})
}
if (pages.size === relayCount) {
onCover?.(Math.max(...Array.from(pages.values(), page => page.lowest)))
}
}
2026-08-30 00:42:07 +00:00
const found = await network.get().request({
2026-06-29 23:28:11 +00:00
relays,
2026-02-16 21:31:43 +00:00
autoClose: true,
// The socket sends five per 100ms first come first served, so the feed on screen goes ahead of the space's background pulls.
priority: 1,
signal: spanSignal(controller.signal),
threshold: SPAN_THRESHOLD,
filters: filters.map(filter => ({...filter, ...extension})),
onEvent: countEvent,
onDuplicate: countEvent,
2026-09-01 17:29:14 +00:00
onEose: () => {
complete = true
},
2026-02-16 21:31:43 +00:00
})
return {found, complete, pages}
2026-02-16 21:31:43 +00:00
}
// The window only moves once the relays have answered, since a dropped request looks like an empty span.
2026-09-01 17:29:14 +00:00
const loadOlder = async (): Promise<FeedSpan> => {
2026-09-21 16:48:35 +00:00
if (oldest < now() - int(2, YEAR)) {
return {found: 0, complete: true, exhausted: true}
}
2026-02-16 21:31:43 +00:00
2026-08-30 00:42:07 +00:00
const until = oldest
const since = until - olderInterval
// A page comes back newest first, so a relay's lowest answer proves the stretch above it.
const {found, complete, pages} = await loadSpan({since, until, limit: PAGE_SIZE}, reach)
// A relay that answered with less than its limit has covered its whole span.
let edge: Maybe<number>
for (const page of pages.values()) {
if (page.count >= PAGE_SIZE && (edge === undefined || page.lowest > edge)) {
edge = page.lowest
}
}
2026-02-16 21:31:43 +00:00
2026-09-01 17:29:14 +00:00
if (complete) {
olderInterval = nextInterval(olderInterval, found.length)
// Stepping the edge back is what stops a page that filled up inside one span being asked for again.
oldest = edge === undefined ? since : Math.min(edge, until - 1)
2026-09-01 17:29:14 +00:00
}
2026-02-16 21:31:43 +00:00
// addEvents, not insertEvents: found can reach below oldest.
addEvents(found)
reach(oldest)
return {found: found.length, complete, exhausted: false}
2026-08-30 00:42:07 +00:00
}
2025-01-28 16:13:20 +00:00
// A limit is answered with the newest events matching it, which reaches away from an anchor in the past.
2026-09-01 17:29:14 +00:00
const loadNewer = async (): Promise<FeedSpan> => {
2026-09-21 16:48:35 +00:00
if (newest >= now()) {
return {found: 0, complete: true, exhausted: true}
}
2026-02-16 21:31:43 +00:00
2026-08-30 00:42:07 +00:00
const since = newest
const until = Math.min(now(), since + newerInterval)
const {found, complete} = await loadSpan({since, until})
2025-01-28 16:13:20 +00:00
2026-09-01 17:29:14 +00:00
if (complete) {
newerInterval = nextInterval(newerInterval, found.length)
2026-09-01 17:29:14 +00:00
newest = until
}
2026-02-13 22:51:56 +00:00
addEvents(found)
return {found: found.length, complete, exhausted: false}
2026-08-30 00:42:07 +00:00
}
2025-01-28 16:13:20 +00:00
// What the repository already holds is an answer too, so a page of it renders before any relay replies.
const stored = relays.map(url =>
Array.from(getEventsForUrl(url, filters)).sort(compareEventsAsc).slice(-PAGE_SIZE),
)
if (stored.every(page => page.length > 0)) {
reach(Math.max(...stored.map(page => page[0].created_at)))
}
addEvents(stored.flat())
2025-01-28 16:13:20 +00:00
return {
events,
2026-08-30 00:42:07 +00:00
loadOlder,
loadNewer,
2025-01-28 16:13:20 +00:00
cleanup: () => {
2025-04-11 16:27:19 +00:00
controller.abort()
2026-02-13 23:18:46 +00:00
unsubscribers.forEach(call)
2025-01-28 16:13:20 +00:00
},
}
}
// Calendar events are addressed by the days they cover, so the spans are date hashes.
2025-02-06 00:26:22 +00:00
export const makeCalendarFeed = ({
2026-06-29 23:28:11 +00:00
relays,
2025-10-06 18:23:19 +00:00
filters,
onEvent,
2025-02-06 00:26:22 +00:00
}: {
2026-06-29 23:28:11 +00:00
relays: string[]
2025-10-06 18:23:19 +00:00
filters: Filter[]
onEvent?: (event: TrustedEvent) => void
2025-02-06 00:26:22 +00:00
}) => {
2026-06-15 18:45:36 +00:00
const interval = int(5, MONTH)
2025-04-11 16:27:19 +00:00
const controller = new AbortController()
2026-06-29 23:28:11 +00:00
const seen = new Set<string>()
2026-08-30 00:42:07 +00:00
let oldest = now()
let newest = now()
2025-02-06 00:26:22 +00:00
2026-07-28 16:16:26 +00:00
const getStart = (event: TrustedEvent) => parseInt(tagValue(tagSpec("start"), event.tags) || "")
2025-02-06 01:05:41 +00:00
2026-07-28 16:16:26 +00:00
const getEnd = (event: TrustedEvent) => parseInt(tagValue(tagSpec("end"), event.tags) || "")
2025-02-06 01:05:41 +00:00
const compareByStart = (a: TrustedEvent, b: TrustedEvent) =>
getStart(a) - getStart(b) || compareEventsAsc(a, b)
2026-06-29 23:28:11 +00:00
const events = writable(
uniqBy(
e => e.id,
relays.flatMap(url => Array.from(getEventsForUrl(url, filters))),
).sort(compareByStart),
2026-06-29 23:28:11 +00:00
)
2025-02-06 01:05:41 +00:00
const insertEvents = (newEvents: TrustedEvent[]) => {
2026-06-29 23:28:11 +00:00
const valid = newEvents.filter(e => !isNaN(getStart(e)) && !isNaN(getEnd(e)) && !seen.has(e.id))
2026-09-21 16:48:35 +00:00
if (valid.length === 0) {
return
}
2025-02-06 01:05:41 +00:00
2026-07-28 00:13:14 +00:00
for (const event of valid) {
seen.add(event.id)
onEvent?.(event)
2026-07-28 00:13:14 +00:00
}
valid.sort(compareByStart)
2025-02-06 00:26:22 +00:00
2026-07-28 00:13:14 +00:00
events.update($events => {
// Calendar events are addressable, so a new version supersedes the old one
const superseded = new Set(valid.map(getAddress))
return mergeSorted(
$events.filter(e => !superseded.has(getAddress(e))),
valid,
compareByStart,
)
2025-02-06 00:26:22 +00:00
})
}
const removeEvents = (ids: Set<string>) => {
events.update($events => $events.filter(event => !ids.has(event.id)))
for (const id of ids) {
seen.delete(id)
}
}
// Calendar events are addressable and often relayed on, so this takes any match rather than its own relays'.
2026-08-30 00:42:07 +00:00
const unsubscribers = syncFeed({
relays,
filters,
addEvents: insertEvents,
removeEvents,
2026-08-30 00:42:07 +00:00
requireRelay: false,
2026-07-28 00:13:14 +00:00
})
2026-08-30 00:42:07 +00:00
const loadTimeframe = async (since: number, until: number) => {
2026-09-01 17:29:14 +00:00
let complete = false
2026-08-30 00:42:07 +00:00
const found = await network.get().request({
2026-06-29 23:28:11 +00:00
relays,
2025-04-09 22:32:18 +00:00
autoClose: true,
priority: 1,
signal: spanSignal(controller.signal),
threshold: SPAN_THRESHOLD,
2026-08-30 00:42:07 +00:00
filters: [{kinds: [EVENT_TIME], "#D": daysBetween(since, until).map(String)}],
2026-09-01 17:29:14 +00:00
onEose: () => {
complete = true
},
2025-02-06 00:26:22 +00:00
})
2026-09-01 17:29:14 +00:00
return {found: found.length, complete}
2025-02-06 01:05:41 +00:00
}
2026-09-01 17:29:14 +00:00
const loadOlder = async (): Promise<FeedSpan> => {
2026-09-21 16:48:35 +00:00
if (oldest < now() - int(2, YEAR)) {
return {found: 0, complete: true, exhausted: true}
}
2025-02-06 00:26:22 +00:00
2026-08-30 00:42:07 +00:00
const until = oldest
2026-09-01 17:29:14 +00:00
const since = until - interval
const {found, complete} = await loadTimeframe(since, until)
2025-02-06 00:26:22 +00:00
2026-09-01 17:29:14 +00:00
if (complete) {
oldest = since
}
2025-02-06 00:26:22 +00:00
2026-09-01 17:29:14 +00:00
return {found, complete, exhausted: false}
2026-08-30 00:42:07 +00:00
}
2025-02-06 00:26:22 +00:00
2026-09-01 17:29:14 +00:00
const loadNewer = async (): Promise<FeedSpan> => {
2026-09-21 16:48:35 +00:00
if (newest > now() + int(2, YEAR)) {
return {found: 0, complete: true, exhausted: true}
}
2025-02-06 00:26:22 +00:00
2026-08-30 00:42:07 +00:00
const since = newest
2026-09-01 17:29:14 +00:00
const until = since + interval
const {found, complete} = await loadTimeframe(since, until)
2026-08-30 00:42:07 +00:00
2026-09-01 17:29:14 +00:00
if (complete) {
newest = until
}
2026-08-30 00:42:07 +00:00
2026-09-01 17:29:14 +00:00
return {found, complete, exhausted: false}
2026-08-30 00:42:07 +00:00
}
2025-02-06 00:26:22 +00:00
return {
events,
2026-08-30 00:42:07 +00:00
loadOlder,
loadNewer,
// The month and week views jump to a range rather than scrolling, and wait to show progress for it.
load: loadTimeframe,
2025-02-06 00:26:22 +00:00
cleanup: () => {
2025-04-11 16:27:19 +00:00
controller.abort()
2026-02-13 23:18:46 +00:00
unsubscribers.forEach(call)
2025-02-06 00:26:22 +00:00
},
}
}