mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
make relay.connect() an awaitable thing.
This commit is contained in:
@@ -53,7 +53,7 @@ import {
|
||||
} from 'nostr-tools'
|
||||
|
||||
const relay = relayInit('wss://relay.example.com')
|
||||
relay.connect()
|
||||
await relay.connect()
|
||||
|
||||
relay.on('connect', () => {
|
||||
console.log(`connected to ${relay.url}`)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "nostr-tools",
|
||||
"version": "1.0.0-beta2",
|
||||
"version": "1.0.0-rc1",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
8
relay.ts
8
relay.ts
@@ -59,14 +59,17 @@ export function relayInit(url: string): Relay {
|
||||
}
|
||||
} = {}
|
||||
|
||||
function connectRelay() {
|
||||
async function connectRelay(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
ws = new WebSocket(url)
|
||||
|
||||
ws.onopen = () => {
|
||||
listeners.connect.forEach(cb => cb())
|
||||
resolve()
|
||||
}
|
||||
ws.onerror = () => {
|
||||
listeners.error.forEach(cb => cb())
|
||||
reject()
|
||||
}
|
||||
ws.onclose = async () => {
|
||||
listeners.disconnect.forEach(cb => cb())
|
||||
@@ -121,11 +124,12 @@ export function relayInit(url: string): Relay {
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function connect(): Promise<void> {
|
||||
if (ws?.readyState && ws.readyState === 1) return // ws already open
|
||||
connectRelay()
|
||||
await connectRelay()
|
||||
}
|
||||
|
||||
async function trySend(params: [string, ...any]) {
|
||||
|
||||
Reference in New Issue
Block a user