Switch to makeOutboxLoader for relay selection in loaders

This commit is contained in:
Jon Staab
2025-04-09 16:26:18 -07:00
parent 859f7fa68f
commit 0c43bf199f
5 changed files with 45 additions and 49 deletions
+2 -3
View File
@@ -3,7 +3,7 @@ import {TrustedEvent, PublishedList} from "@welshman/util"
import {deriveEventsMapped} from "@welshman/store"
import {repository} from "./core.js"
import {collection} from "./collection.js"
import {loadWithAsapMetaRelayUrls} from "./relaySelections.js"
import {makeOutboxLoader} from "./relaySelections.js"
export const follows = deriveEventsMapped<PublishedList>(repository, {
filters: [{kinds: [FOLLOWS]}],
@@ -19,6 +19,5 @@ export const {
name: "follows",
store: follows,
getKey: follows => follows.event.pubkey,
load: (pubkey: string, relays: string[]) =>
loadWithAsapMetaRelayUrls(pubkey, relays, [{kinds: [FOLLOWS], authors: [pubkey]}]),
load: makeOutboxLoader([FOLLOWS])
})
+2 -3
View File
@@ -4,7 +4,7 @@ import {deriveEventsMapped} from "@welshman/store"
import {repository} from "./core.js"
import {collection} from "./collection.js"
import {ensurePlaintext} from "./plaintext.js"
import {loadWithAsapMetaRelayUrls} from "./relaySelections.js"
import {makeOutboxLoader} from "./relaySelections.js"
export const mutes = deriveEventsMapped<PublishedList>(repository, {
filters: [{kinds: [MUTES]}],
@@ -25,6 +25,5 @@ export const {
name: "mutes",
store: mutes,
getKey: mute => mute.event.pubkey,
load: (pubkey: string, relays: string[]) =>
loadWithAsapMetaRelayUrls(pubkey, relays, [{kinds: [MUTES], authors: [pubkey]}]),
load: makeOutboxLoader([MUTES])
})
+2 -3
View File
@@ -3,7 +3,7 @@ import {TrustedEvent, PublishedList} from "@welshman/util"
import {deriveEventsMapped} from "@welshman/store"
import {repository} from "./core.js"
import {collection} from "./collection.js"
import {loadWithAsapMetaRelayUrls} from "./relaySelections.js"
import {makeOutboxLoader} from "./relaySelections.js"
export const pins = deriveEventsMapped<PublishedList>(repository, {
filters: [{kinds: [PINS]}],
@@ -19,6 +19,5 @@ export const {
name: "pins",
store: pins,
getKey: pins => pins.event.pubkey,
load: (pubkey: string, relays: string[]) =>
loadWithAsapMetaRelayUrls(pubkey, relays, [{kinds: [PINS], authors: [pubkey]}]),
load: makeOutboxLoader([PINS])
})
+2 -3
View File
@@ -4,7 +4,7 @@ import {PublishedProfile} from "@welshman/util"
import {deriveEventsMapped, withGetter} from "@welshman/store"
import {repository} from "./core.js"
import {collection} from "./collection.js"
import {loadWithAsapMetaRelayUrls} from "./relaySelections.js"
import {makeOutboxLoader} from "./relaySelections.js"
export const profiles = withGetter(
deriveEventsMapped<PublishedProfile>(repository, {
@@ -22,8 +22,7 @@ export const {
name: "profiles",
store: profiles,
getKey: profile => profile.event.pubkey,
load: (pubkey: string, relays: string[]) =>
loadWithAsapMetaRelayUrls(pubkey, relays, [{kinds: [PROFILE], authors: [pubkey]}]),
load: makeOutboxLoader([PROFILE]),
})
export const displayProfileByPubkey = (pubkey: string | undefined) =>
+37 -37
View File
@@ -1,4 +1,4 @@
import {uniq} from "@welshman/lib"
import {uniq, batcher, always} from "@welshman/lib"
import {
INBOX_RELAYS,
RELAYS,
@@ -10,7 +10,7 @@ import {
getRelayTagValues,
} from "@welshman/util"
import {TrustedEvent, Filter, PublishedList, List} from "@welshman/util"
import {load} from "@welshman/net"
import {request, load, RequestEvent} from "@welshman/net"
import {deriveEventsMapped} from "@welshman/store"
import {repository} from "./core.js"
import {Router} from "./router.js"
@@ -33,6 +33,39 @@ export const getWriteRelayUrls = (list?: List): string[] =>
.map((t: string[]) => normalizeRelayUrl(t[1])),
)
export type OutboxLoaderRequest = {
pubkey: string
relays: string[]
}
export const makeOutboxLoader = (kinds: number[]) => {
const loadOutboxRequest = batcher(200, (requests: OutboxLoaderRequest[]) => {
const router = Router.get()
const authors: string[] = []
const scenarios = [router.Index()]
for (const {pubkey, relays} of requests) {
authors.push(pubkey)
scenarios.push(router.FromPubkey(pubkey), router.FromRelays(relays))
}
const filters = [{authors, kinds}]
const relays = router.merge(scenarios).getUrls()
const promise = new Promise<void>(resolve => {
const req = request({filters, relays, autoClose: true})
req.on(RequestEvent.Eose, () => resolve())
req.on(RequestEvent.Close, () => resolve())
})
return requests.map(always(promise))
})
return (pubkey: string, relays: string[]) => loadOutboxRequest({pubkey, relays})
}
export const relaySelections = deriveEventsMapped<PublishedList>(repository, {
filters: [{kinds: [RELAYS]}],
itemToEvent: item => item.event,
@@ -47,41 +80,9 @@ export const {
name: "relaySelections",
store: relaySelections,
getKey: relaySelections => relaySelections.event.pubkey,
load: async (pubkey: string, relays: string[]) => {
const router = Router.get()
await load({
relays: router
.merge([router.Index(), router.FromRelays(relays), router.FromPubkey(pubkey)])
.getUrls(),
filters: [{kinds: [RELAYS], authors: [pubkey]}],
})
},
load: makeOutboxLoader([RELAYS]),
})
export const loadWithAsapMetaRelayUrls = (pubkey: string, relays: string[], filters: Filter[]) => {
const router = Router.get()
return new Promise(resolve => {
let resolved = 0
const onLoad = (events: TrustedEvent[]) => {
if (++resolved === 2 || events.length > 0) {
resolve(events)
}
}
load({
filters,
relays: router.merge([router.Index(), router.FromRelays(relays)]).getUrls(),
}).then(onLoad)
loadRelaySelections(pubkey, relays)
.then(() => load({filters, relays: router.FromPubkey(pubkey).getUrls()}))
.then(onLoad)
})
}
export const inboxRelaySelections = deriveEventsMapped<PublishedList>(repository, {
filters: [{kinds: [INBOX_RELAYS]}],
itemToEvent: item => item.event,
@@ -96,6 +97,5 @@ export const {
name: "inboxRelaySelections",
store: inboxRelaySelections,
getKey: inboxRelaySelections => inboxRelaySelections.event.pubkey,
load: (pubkey: string, relays: string[]) =>
loadWithAsapMetaRelayUrls(pubkey, relays, [{kinds: [INBOX_RELAYS], authors: [pubkey]}]),
load: makeOutboxLoader([INBOX_RELAYS]),
})