Add request utils for complex requests
This commit is contained in:
+2
-30
@@ -27,8 +27,8 @@ import {
|
||||
getRelayTagValues,
|
||||
} from "@welshman/util"
|
||||
import type {TrustedEvent, EventTemplate, List} from "@welshman/util"
|
||||
import type {SubscribeRequestWithHandlers, Subscription} from "@welshman/net"
|
||||
import {PublishStatus, AuthStatus, SocketStatus, SubscriptionEvent} from "@welshman/net"
|
||||
import type {SubscribeRequestWithHandlers} from "@welshman/net"
|
||||
import {PublishStatus, AuthStatus, SocketStatus} from "@welshman/net"
|
||||
import {Nip59, makeSecret, stamp, Nip46Broker} from "@welshman/signer"
|
||||
import {
|
||||
pubkey,
|
||||
@@ -51,7 +51,6 @@ import {
|
||||
nip44EncryptToSelf,
|
||||
loadRelay,
|
||||
addSession,
|
||||
subscribe,
|
||||
clearStorage,
|
||||
dropSession,
|
||||
} from "@welshman/app"
|
||||
@@ -97,33 +96,6 @@ export const makeIMeta = (url: string, data: Record<string, string>) => [
|
||||
...Object.entries(data).map(([k, v]) => [k, v].join(" ")),
|
||||
]
|
||||
|
||||
export const subscribePersistent = (request: SubscribeRequestWithHandlers) => {
|
||||
let sub: Subscription
|
||||
let done = false
|
||||
|
||||
const start = async () => {
|
||||
// If the subscription gets closed quickly, don't start flapping
|
||||
await Promise.all([
|
||||
sleep(30_000),
|
||||
new Promise(resolve => {
|
||||
sub = subscribe(request)
|
||||
sub.emitter.on(SubscriptionEvent.Complete, resolve)
|
||||
}),
|
||||
])
|
||||
|
||||
if (!done) {
|
||||
start()
|
||||
}
|
||||
}
|
||||
|
||||
start()
|
||||
|
||||
return () => {
|
||||
done = true
|
||||
sub?.close()
|
||||
}
|
||||
}
|
||||
|
||||
export const getThunkError = async (thunk: Thunk) => {
|
||||
const result = await thunk.result
|
||||
const [{status, message}] = Object.values(result) as any
|
||||
|
||||
Reference in New Issue
Block a user