diff --git a/package-lock.json b/package-lock.json index 1d2d4b7..f7cc2ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,7 +32,7 @@ "@welshman/dvm": "~0.0.10", "@welshman/feeds": "~0.0.21", "@welshman/lib": "~0.0.23", - "@welshman/net": "~0.0.30", + "@welshman/net": "~0.0.31", "@welshman/signer": "~0.0.10", "@welshman/store": "~0.0.12", "@welshman/util": "~0.0.42", @@ -3701,9 +3701,9 @@ } }, "node_modules/@welshman/net": { - "version": "0.0.30", - "resolved": "https://registry.npmjs.org/@welshman/net/-/net-0.0.30.tgz", - "integrity": "sha512-whFPG/NKW80VAA1RL/RQOvEd6PqAUgLV7gBJ/06rKzmfTtG6SUcro/Qz0c25bEB/BjPvMjTF/M0hv6qQJLRL6Q==", + "version": "0.0.31", + "resolved": "https://registry.npmjs.org/@welshman/net/-/net-0.0.31.tgz", + "integrity": "sha512-7UcEuU65XJTtphjtgGPTNO0InOHm9iqvsmsmhRk7QTNALDkIcqofL3TjnI32zPYJhq5QVIO8tXj04iXsld0OdQ==", "license": "MIT", "dependencies": { "@welshman/lib": "~0.0.23", diff --git a/package.json b/package.json index 7e2e38c..79a9ccb 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "@welshman/dvm": "~0.0.10", "@welshman/feeds": "~0.0.21", "@welshman/lib": "~0.0.23", - "@welshman/net": "~0.0.30", + "@welshman/net": "~0.0.31", "@welshman/signer": "~0.0.10", "@welshman/store": "~0.0.12", "@welshman/util": "~0.0.42", diff --git a/src/app/commands.ts b/src/app/commands.ts index cd1644c..144dc5f 100644 --- a/src/app/commands.ts +++ b/src/app/commands.ts @@ -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) => [ ...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) => { diff --git a/src/app/components/Chat.svelte b/src/app/components/Chat.svelte index 8f472d8..91853ab 100644 --- a/src/app/components/Chat.svelte +++ b/src/app/components/Chat.svelte @@ -10,7 +10,7 @@