Skip to content

Commit

Permalink
correctly wait until connection to a relay is established
Browse files Browse the repository at this point in the history
  • Loading branch information
jiftechnify authored and fiatjaf committed Dec 20, 2023
1 parent 7552a36 commit b7389be
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions relay.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { test, expect, afterEach, beforeEach } from 'bun:test'
import { afterEach, expect, test } from 'bun:test'

import { finalizeEvent } from './pure.ts'
import { generateSecretKey, getPublicKey } from './pure.ts'
import { Relay } from './relay.ts'
import { finalizeEvent, generateSecretKey, getPublicKey } from './pure.ts'
import { Relay, relayConnect } from './relay.ts'

let relay = new Relay('wss://public.relaying.io')

beforeEach(() => {
relay.connect()
})

afterEach(() => {
relay.close()
})
Expand All @@ -19,7 +14,14 @@ test('connectivity', async () => {
expect(relay.connected).toBeTrue()
})

test('connectivity, with relayConnect()', async () => {
const relay = await relayConnect('wss://public.relaying.io')
expect(relay.connected).toBeTrue()
})

test('querying', async () => {
await relay.connect()

let resolve1: () => void
let resolve2: () => void

Expand Down Expand Up @@ -57,6 +59,8 @@ test('querying', async () => {
}, 10000)

test('listening and publishing and closing', async () => {
await relay.connect()

let sk = generateSecretKey()
let pk = getPublicKey(sk)
var resolve1: (_: void) => void
Expand Down
4 changes: 2 additions & 2 deletions relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { Queue, normalizeURL } from './utils.ts'
import { nip42 } from './index.ts'
import { yieldThread } from './helpers.ts'

export function relayConnect(url: string) {
export async function relayConnect(url: string) {
const relay = new Relay(url)
relay.connect()
await relay.connect()
return relay
}

Expand Down

0 comments on commit b7389be

Please sign in to comment.