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
+23 -18
View File
@@ -15,7 +15,7 @@ import {
StampedEvent,
NOSTR_CONNECT,
} from "@welshman/util"
import {MultiRequest, MultiPublish, RequestEvent, AdapterContext} from "@welshman/net"
import {MultiPublish, request, AdapterContext} from "@welshman/net"
import {ISigner, EncryptionImplementation, decrypt, hash, own} from "../util.js"
import {Nip01Signer} from "./nip01.js"
@@ -97,7 +97,7 @@ const popupManager = (() => {
})()
export class Nip46Receiver extends Emitter {
public sub?: MultiRequest
public abortController?: AbortController
constructor(
public signer: ISigner,
@@ -108,33 +108,38 @@ export class Nip46Receiver extends Emitter {
// start listening to the remote signer for incoming events
// broadcast any event returned by the remote signer
start = async () => {
if (this.sub) return
if (this.abortController) return
this.abortController = new AbortController()
const {relays, context} = this.params
const userPubkey = await this.signer.getPubkey()
const filters = [{kinds: [NOSTR_CONNECT], "#p": [userPubkey]}]
this.sub = new MultiRequest({relays, filters, context})
request({
relays,
filters,
context,
signal: this.abortController.signal,
onEvent: async (event: TrustedEvent, url: string) => {
const json = await decrypt(this.signer, event.pubkey, event.content)
const response = tryCatch(() => JSON.parse(json)) || {}
this.sub.on(RequestEvent.Event, async (event: TrustedEvent, url: string) => {
const json = await decrypt(this.signer, event.pubkey, event.content)
const response = tryCatch(() => JSON.parse(json)) || {}
// Delay errors in case there's a zombie signer out there clogging things up
if (response.error) {
await sleep(3000)
}
// Delay errors in case there's a zombie signer out there clogging things up
if (response.error) {
await sleep(3000)
}
this.emit(Nip46Event.Receive, {...response, url, event} as Nip46Response)
})
this.sub.on(RequestEvent.Close, () => {
this.sub = undefined
this.emit(Nip46Event.Receive, {...response, url, event} as Nip46Response)
},
onClose: () => {
this.abortController = undefined
},
})
}
stop = () => {
this.sub?.close()
this.abortController?.abort()
this.removeAllListeners()
}
}