Change requests from classes to functions

This commit is contained in:
Jon Staab
2025-04-10 10:38:47 -07:00
parent 0c43bf199f
commit 989fc74374
8 changed files with 336 additions and 342 deletions
+12 -17
View File
@@ -1,6 +1,6 @@
import {nthEq, partition, race, now} from "@welshman/lib"
import {createEvent, getPubkeyTagValues} from "@welshman/util"
import {MultiRequest, Tracker, RequestEvent, request} from "@welshman/net"
import {request, Tracker} from "@welshman/net"
import {Scope, FeedController, RequestOpts, FeedOptions, DVMOpts, Feed} from "@welshman/feeds"
import {makeDvmRequest, DVMEvent} from "@welshman/dvm"
import {makeSecret, Nip01Signer} from "@welshman/signer"
@@ -27,13 +27,17 @@ export const makeFeedRequestHandler = ({signal}: FeedRequestHandlerOptions) =>
req.on(RequestEvent.Close, resolve)
})
} else {
const requests: MultiRequest[] = []
const promises: Promise<TrustedEvent>[][] = []
const [withSearch, withoutSearch] = partition(f => Boolean(f.search), filters)
if (withSearch.length > 0) {
requests.push(
promises.push(
request({
tracker, signal, autoClose: true,
signal,
tracker,
onEvent
threshold: 0.1,
autoClose: true,
filters: withSearch,
relays: Router.get().Search().getUrls(),
}),
@@ -41,25 +45,16 @@ export const makeFeedRequestHandler = ({signal}: FeedRequestHandlerOptions) =>
}
if (withoutSearch.length > 0) {
requests.push(
...getFilterSelections(filters).flatMap(options =>
request({tracker, signal, autoClose: true, ...options}),
promises.push(
...getFilterSelections(filters).flatMap(({relays, filters}) =>
request({tracker, signal, onEvent, relays, filters, threshold: 0.8, autoClose: true}),
),
)
}
// Break out selections by relay so we can complete early after a certain number
// of requests complete for faster load times
await race(
withSearch.length > 0 ? 0.1 : 0.8,
requests.map(
req =>
new Promise(resolve => {
req.on(RequestEvent.Event, onEvent)
req.on(RequestEvent.Close, resolve)
}),
),
)
await race(withSearch.length > 0 ? 0.1 : 0.8, promises)
// Wait until after we've queried the network to access our local cache. This results in less
// snappy response times, but is necessary to prevent stale stuff that the user has already seen
+2 -5
View File
@@ -4,9 +4,8 @@ import {
push as basePush,
pull as basePull,
PublishEvent,
RequestEvent,
SinglePublish,
SingleRequest,
requestOne,
} from "@welshman/net"
import {repository} from "./core.js"
import {relaysByUrl} from "./relays.js"
@@ -35,9 +34,7 @@ export const pull = async ({relays, filters}: AppSyncOpts) => {
relays.map(async relay => {
await (hasNegentropy(relay)
? basePull({filters, events, relays: [relay]})
: new Promise<void>(resolve => {
new SingleRequest({filters, relay, autoClose: true}).on(RequestEvent.Close, resolve)
}))
: requestOne({filters, relay, autoClose: true}))
}),
)
}