Fix room subscription

This commit is contained in:
Jon Staab
2024-10-30 13:52:35 -07:00
parent 996d931f3b
commit a5b0b769d5
6 changed files with 59 additions and 33 deletions
+29 -1
View File
@@ -21,7 +21,7 @@ import {
getRelayTagValues,
} from "@welshman/util"
import type {TrustedEvent, EventTemplate, List} from "@welshman/util"
import type {SubscribeRequestWithHandlers} from "@welshman/net"
import type {SubscribeRequestWithHandlers, Subscription} from "@welshman/net"
import {PublishStatus, AuthStatus, ConnectionStatus} from "@welshman/net"
import {Nip59, makeSecret, stamp, Nip46Broker} from "@welshman/signer"
import type {Nip46Handler} from "@welshman/signer"
@@ -47,6 +47,7 @@ import {
loadRelay,
addSession,
nip46Perms,
subscribe,
} from "@welshman/app"
import {
COMMENT,
@@ -89,6 +90,33 @@ 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("close", resolve)
})
])
if (!done) {
start()
}
}
start()
return () => {
done = true
sub?.close()
}
}
// Log in
export const loginWithNip46 = async (token: string, handler: Nip46Handler) => {