diff --git a/src/storages/hydrate.ts b/src/storages/hydrate.ts index 7c21071..369f39f 100644 --- a/src/storages/hydrate.ts +++ b/src/storages/hydrate.ts @@ -21,33 +21,43 @@ async function hydrateEvents(opts: HydrateOpts): Promise { const cache = [...events]; - for (const event of await gatherReposts({ events, storage, signal })) { + for (const event of await gatherReposts({ events: cache, storage, signal })) { cache.push(event); } - for (const event of await gatherQuotes({ events, storage, signal })) { + for (const event of await gatherQuotes({ events: cache, storage, signal })) { cache.push(event); } - for (const event of await gatherAuthors({ events, storage, signal })) { + for (const event of await gatherAuthors({ events: cache, storage, signal })) { cache.push(event); } - for (const event of await gatherUsers({ events, storage, signal })) { + for (const event of await gatherUsers({ events: cache, storage, signal })) { cache.push(event); } + const [authorStats, eventStats] = await Promise.all([ + gatherAuthorStats(cache), + gatherEventStats(cache), + ]); + const stats = { - authors: await gatherAuthorStats(cache), - events: await gatherEventStats(cache), + authors: authorStats, + events: eventStats, }; - assembleEvents(cache, cache, stats); - assembleEvents(events, cache, stats); + // Dedupe events. + const results = [...new Map(cache.map((event) => [event.id, event])).values()]; + + // First connect all the events to each-other, then connect the connected events to the original list. + assembleEvents(results, results, stats); + assembleEvents(events, results, stats); return events; } +/** Connect the events in list `b` to the DittoEvent fields in list `a`. */ function assembleEvents( a: DittoEvent[], b: DittoEvent[], @@ -78,6 +88,7 @@ function assembleEvents( return a; } +/** Collect reposts from the events. */ function gatherReposts({ events, storage, signal }: HydrateOpts): Promise { const ids = new Set(); @@ -96,6 +107,7 @@ function gatherReposts({ events, storage, signal }: HydrateOpts): Promise { const ids = new Set(); @@ -114,6 +126,7 @@ function gatherQuotes({ events, storage, signal }: HydrateOpts): Promise { const pubkeys = new Set(events.map((event) => event.pubkey)); @@ -123,6 +136,7 @@ function gatherAuthors({ events, storage, signal }: HydrateOpts): Promise { const pubkeys = new Set(events.map((event) => event.pubkey)); @@ -132,6 +146,7 @@ function gatherUsers({ events, storage, signal }: HydrateOpts): Promise { const pubkeys = new Set( events @@ -146,6 +161,7 @@ function gatherAuthorStats(events: DittoEvent[]): Promise { const ids = new Set( events