Switch to makeOutboxLoader for relay selection in loaders
This commit is contained in:
@@ -3,7 +3,7 @@ import {TrustedEvent, PublishedList} from "@welshman/util"
|
|||||||
import {deriveEventsMapped} from "@welshman/store"
|
import {deriveEventsMapped} from "@welshman/store"
|
||||||
import {repository} from "./core.js"
|
import {repository} from "./core.js"
|
||||||
import {collection} from "./collection.js"
|
import {collection} from "./collection.js"
|
||||||
import {loadWithAsapMetaRelayUrls} from "./relaySelections.js"
|
import {makeOutboxLoader} from "./relaySelections.js"
|
||||||
|
|
||||||
export const follows = deriveEventsMapped<PublishedList>(repository, {
|
export const follows = deriveEventsMapped<PublishedList>(repository, {
|
||||||
filters: [{kinds: [FOLLOWS]}],
|
filters: [{kinds: [FOLLOWS]}],
|
||||||
@@ -19,6 +19,5 @@ export const {
|
|||||||
name: "follows",
|
name: "follows",
|
||||||
store: follows,
|
store: follows,
|
||||||
getKey: follows => follows.event.pubkey,
|
getKey: follows => follows.event.pubkey,
|
||||||
load: (pubkey: string, relays: string[]) =>
|
load: makeOutboxLoader([FOLLOWS])
|
||||||
loadWithAsapMetaRelayUrls(pubkey, relays, [{kinds: [FOLLOWS], authors: [pubkey]}]),
|
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {deriveEventsMapped} from "@welshman/store"
|
|||||||
import {repository} from "./core.js"
|
import {repository} from "./core.js"
|
||||||
import {collection} from "./collection.js"
|
import {collection} from "./collection.js"
|
||||||
import {ensurePlaintext} from "./plaintext.js"
|
import {ensurePlaintext} from "./plaintext.js"
|
||||||
import {loadWithAsapMetaRelayUrls} from "./relaySelections.js"
|
import {makeOutboxLoader} from "./relaySelections.js"
|
||||||
|
|
||||||
export const mutes = deriveEventsMapped<PublishedList>(repository, {
|
export const mutes = deriveEventsMapped<PublishedList>(repository, {
|
||||||
filters: [{kinds: [MUTES]}],
|
filters: [{kinds: [MUTES]}],
|
||||||
@@ -25,6 +25,5 @@ export const {
|
|||||||
name: "mutes",
|
name: "mutes",
|
||||||
store: mutes,
|
store: mutes,
|
||||||
getKey: mute => mute.event.pubkey,
|
getKey: mute => mute.event.pubkey,
|
||||||
load: (pubkey: string, relays: string[]) =>
|
load: makeOutboxLoader([MUTES])
|
||||||
loadWithAsapMetaRelayUrls(pubkey, relays, [{kinds: [MUTES], authors: [pubkey]}]),
|
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import {TrustedEvent, PublishedList} from "@welshman/util"
|
|||||||
import {deriveEventsMapped} from "@welshman/store"
|
import {deriveEventsMapped} from "@welshman/store"
|
||||||
import {repository} from "./core.js"
|
import {repository} from "./core.js"
|
||||||
import {collection} from "./collection.js"
|
import {collection} from "./collection.js"
|
||||||
import {loadWithAsapMetaRelayUrls} from "./relaySelections.js"
|
import {makeOutboxLoader} from "./relaySelections.js"
|
||||||
|
|
||||||
export const pins = deriveEventsMapped<PublishedList>(repository, {
|
export const pins = deriveEventsMapped<PublishedList>(repository, {
|
||||||
filters: [{kinds: [PINS]}],
|
filters: [{kinds: [PINS]}],
|
||||||
@@ -19,6 +19,5 @@ export const {
|
|||||||
name: "pins",
|
name: "pins",
|
||||||
store: pins,
|
store: pins,
|
||||||
getKey: pins => pins.event.pubkey,
|
getKey: pins => pins.event.pubkey,
|
||||||
load: (pubkey: string, relays: string[]) =>
|
load: makeOutboxLoader([PINS])
|
||||||
loadWithAsapMetaRelayUrls(pubkey, relays, [{kinds: [PINS], authors: [pubkey]}]),
|
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {PublishedProfile} from "@welshman/util"
|
|||||||
import {deriveEventsMapped, withGetter} from "@welshman/store"
|
import {deriveEventsMapped, withGetter} from "@welshman/store"
|
||||||
import {repository} from "./core.js"
|
import {repository} from "./core.js"
|
||||||
import {collection} from "./collection.js"
|
import {collection} from "./collection.js"
|
||||||
import {loadWithAsapMetaRelayUrls} from "./relaySelections.js"
|
import {makeOutboxLoader} from "./relaySelections.js"
|
||||||
|
|
||||||
export const profiles = withGetter(
|
export const profiles = withGetter(
|
||||||
deriveEventsMapped<PublishedProfile>(repository, {
|
deriveEventsMapped<PublishedProfile>(repository, {
|
||||||
@@ -22,8 +22,7 @@ export const {
|
|||||||
name: "profiles",
|
name: "profiles",
|
||||||
store: profiles,
|
store: profiles,
|
||||||
getKey: profile => profile.event.pubkey,
|
getKey: profile => profile.event.pubkey,
|
||||||
load: (pubkey: string, relays: string[]) =>
|
load: makeOutboxLoader([PROFILE]),
|
||||||
loadWithAsapMetaRelayUrls(pubkey, relays, [{kinds: [PROFILE], authors: [pubkey]}]),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
export const displayProfileByPubkey = (pubkey: string | undefined) =>
|
export const displayProfileByPubkey = (pubkey: string | undefined) =>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {uniq} from "@welshman/lib"
|
import {uniq, batcher, always} from "@welshman/lib"
|
||||||
import {
|
import {
|
||||||
INBOX_RELAYS,
|
INBOX_RELAYS,
|
||||||
RELAYS,
|
RELAYS,
|
||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
getRelayTagValues,
|
getRelayTagValues,
|
||||||
} from "@welshman/util"
|
} from "@welshman/util"
|
||||||
import {TrustedEvent, Filter, PublishedList, List} 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 {deriveEventsMapped} from "@welshman/store"
|
||||||
import {repository} from "./core.js"
|
import {repository} from "./core.js"
|
||||||
import {Router} from "./router.js"
|
import {Router} from "./router.js"
|
||||||
@@ -33,6 +33,39 @@ export const getWriteRelayUrls = (list?: List): string[] =>
|
|||||||
.map((t: string[]) => normalizeRelayUrl(t[1])),
|
.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, {
|
export const relaySelections = deriveEventsMapped<PublishedList>(repository, {
|
||||||
filters: [{kinds: [RELAYS]}],
|
filters: [{kinds: [RELAYS]}],
|
||||||
itemToEvent: item => item.event,
|
itemToEvent: item => item.event,
|
||||||
@@ -47,41 +80,9 @@ export const {
|
|||||||
name: "relaySelections",
|
name: "relaySelections",
|
||||||
store: relaySelections,
|
store: relaySelections,
|
||||||
getKey: relaySelections => relaySelections.event.pubkey,
|
getKey: relaySelections => relaySelections.event.pubkey,
|
||||||
load: async (pubkey: string, relays: string[]) => {
|
load: makeOutboxLoader([RELAYS]),
|
||||||
const router = Router.get()
|
|
||||||
|
|
||||||
await load({
|
|
||||||
relays: router
|
|
||||||
.merge([router.Index(), router.FromRelays(relays), router.FromPubkey(pubkey)])
|
|
||||||
.getUrls(),
|
|
||||||
filters: [{kinds: [RELAYS], authors: [pubkey]}],
|
|
||||||
})
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
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, {
|
export const inboxRelaySelections = deriveEventsMapped<PublishedList>(repository, {
|
||||||
filters: [{kinds: [INBOX_RELAYS]}],
|
filters: [{kinds: [INBOX_RELAYS]}],
|
||||||
itemToEvent: item => item.event,
|
itemToEvent: item => item.event,
|
||||||
@@ -96,6 +97,5 @@ export const {
|
|||||||
name: "inboxRelaySelections",
|
name: "inboxRelaySelections",
|
||||||
store: inboxRelaySelections,
|
store: inboxRelaySelections,
|
||||||
getKey: inboxRelaySelections => inboxRelaySelections.event.pubkey,
|
getKey: inboxRelaySelections => inboxRelaySelections.event.pubkey,
|
||||||
load: (pubkey: string, relays: string[]) =>
|
load: makeOutboxLoader([INBOX_RELAYS]),
|
||||||
loadWithAsapMetaRelayUrls(pubkey, relays, [{kinds: [INBOX_RELAYS], authors: [pubkey]}]),
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user