Auto register client plugins
This commit is contained in:
@@ -10,24 +10,18 @@ import {
|
||||
} from "@welshman/util"
|
||||
import type {Filter, TrustedEvent, PublishedList} from "@welshman/util"
|
||||
import {RepositoryCollection} from "./repositoryCollection.js"
|
||||
import {addMinimalFallbacks} from "./router.js"
|
||||
import type {Router} from "./router.js"
|
||||
import type {ClientContext} from "./client.js"
|
||||
import {Router, addMinimalFallbacks} from "./router.js"
|
||||
import {Networking} from "./networking.js"
|
||||
import type {IClient} from "./client.js"
|
||||
|
||||
/**
|
||||
* NIP-65 relay lists, keyed by pubkey. This is the routing substrate every
|
||||
* other outbox-model load depends on, so it also exposes `loadUsingOutbox` /
|
||||
* `makeOutboxLoader` for other collections to build their fetchers on.
|
||||
*
|
||||
* It depends on a `Router`, and the `Router` depends (via injected functions) on
|
||||
* this collection — a genuine domain cycle that `createApp` breaks by wiring the
|
||||
* router's `getRelaysForPubkey` to a lazily-resolved closure.
|
||||
* NIP-65 relay lists, keyed by pubkey. This is the routing substrate every other
|
||||
* outbox-model load depends on, so it also exposes `loadUsingOutbox` /
|
||||
* `makeOutboxLoader` for other collections to build their fetchers on. It and the
|
||||
* Router reference each other lazily via `ctx.use`, so the cycle never bites.
|
||||
*/
|
||||
export class RelayLists extends RepositoryCollection<PublishedList> {
|
||||
constructor(
|
||||
ctx: ClientContext,
|
||||
readonly router: Router,
|
||||
) {
|
||||
constructor(ctx: IClient) {
|
||||
super(ctx, {
|
||||
filters: [{kinds: [RELAYS]}],
|
||||
eventToItem: (event: TrustedEvent) => readList(asDecryptedEvent(event)),
|
||||
@@ -37,11 +31,12 @@ export class RelayLists extends RepositoryCollection<PublishedList> {
|
||||
|
||||
fetch(pubkey: string, relayHints: string[] = []) {
|
||||
const filters = [{kinds: [RELAYS], authors: [pubkey], limit: 1}]
|
||||
const router = this.ctx.use(Router)
|
||||
|
||||
return Promise.all([
|
||||
this.ctx.load({filters, relays: this.router.FromRelays(relayHints).getUrls()}),
|
||||
this.ctx.load({filters, relays: this.router.FromPubkey(pubkey).getUrls()}),
|
||||
this.ctx.load({filters, relays: this.router.Index().getUrls()}),
|
||||
this.ctx.use(Networking).load({filters, relays: router.FromRelays(relayHints).getUrls()}),
|
||||
this.ctx.use(Networking).load({filters, relays: router.FromPubkey(pubkey).getUrls()}),
|
||||
this.ctx.use(Networking).load({filters, relays: router.Index().getUrls()}),
|
||||
])
|
||||
}
|
||||
|
||||
@@ -53,7 +48,8 @@ export class RelayLists extends RepositoryCollection<PublishedList> {
|
||||
loadUsingOutbox = async (kind: number, pubkey: string, filter: Filter = {}) => {
|
||||
const filters: Filter[] = [{...filter, kinds: [kind], authors: [pubkey]}]
|
||||
const writeRelays = getRelaysFromList(await this.load(pubkey), RelayMode.Write)
|
||||
const allRelays = this.router
|
||||
const allRelays = this.ctx
|
||||
.use(Router)
|
||||
.FromRelays(writeRelays)
|
||||
.policy(addMinimalFallbacks)
|
||||
.limit(8)
|
||||
@@ -64,7 +60,7 @@ export class RelayLists extends RepositoryCollection<PublishedList> {
|
||||
}
|
||||
|
||||
for (const relays of chunk(2, allRelays)) {
|
||||
const events = await this.ctx.load({filters, relays})
|
||||
const events = await this.ctx.use(Networking).load({filters, relays})
|
||||
|
||||
if (events.length > 0) {
|
||||
return first(sortEventsDesc(events))
|
||||
@@ -76,10 +72,10 @@ export class RelayLists extends RepositoryCollection<PublishedList> {
|
||||
(kind: number, filter: Filter = {}) =>
|
||||
async (pubkey: string, relayHints: string[] = []) => {
|
||||
const filters: Filter[] = [{...filter, kinds: [kind], authors: [pubkey]}]
|
||||
const relays = this.router.FromRelays(relayHints).getUrls()
|
||||
const relays = this.ctx.use(Router).FromRelays(relayHints).getUrls()
|
||||
|
||||
await Promise.all([
|
||||
this.ctx.load({filters, relays}),
|
||||
this.ctx.use(Networking).load({filters, relays}),
|
||||
this.loadUsingOutbox(kind, pubkey, filter),
|
||||
])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user