turn .publish() into a normal async function returning a promise.

this simplifies the code and makes the API more intuitive.

we used to need the event emitter thing because we were subscribing to the same relay
to check if the event had been published, but that is not necessary now that we assume
an OK response will always come.

closes https://github.com/nbd-wtf/nostr-tools/issues/262
This commit is contained in:
fiatjaf
2023-07-30 12:19:11 -03:00
committed by fiatjaf_
parent 17590cce91
commit 9d690814ca
4 changed files with 64 additions and 101 deletions

View File

@@ -17,7 +17,9 @@ export const authenticate = async ({
}: {
challenge: string
relay: Relay
sign: <K extends number = number>(e: EventTemplate<K>) => Promise<Event<K>> | Event<K>
sign: <K extends number = number>(
e: EventTemplate<K>
) => Promise<Event<K>> | Event<K>
}): Promise<void> => {
const e: EventTemplate = {
kind: Kind.ClientAuth,
@@ -28,15 +30,5 @@ export const authenticate = async ({
],
content: ''
}
const pub = relay.auth(await sign(e))
return new Promise((resolve, reject) => {
pub.on('ok', function ok() {
pub.off('ok', ok)
resolve()
})
pub.on('failed', function fail(reason: string) {
pub.off('failed', fail)
reject(reason)
})
})
return relay.auth(await sign(e))
}