Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

fix: dispatch peer event on adding new addresses as well as set #74

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/address-book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class PeerStoreAddressBook {
const release = await this.store.lock.writeLock()
log.trace('add got write lock')

let hasPeer
let hasPeer = false
let peer: Peer | undefined
let updatedPeer

Expand Down Expand Up @@ -291,7 +291,7 @@ export class PeerStoreAddressBook {
}))

// Notify the existence of a new peer
if (hasPeer === true) {
if (!hasPeer) {
this.dispatchEvent(new CustomEvent<PeerInfo>('peer', {
detail: {
id: peerId,
Expand Down
26 changes: 26 additions & 0 deletions test/address-book.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,32 @@ describe('addressBook', () => {
await defer.promise
})

it('emits peer event if new peer has addresses added', async () => {
const defer = pDefer()

peerStore.addEventListener('peer', () => {
defer.resolve()
})

await ab.add(await createEd25519PeerId(), [
multiaddr('/ip4/42.14.53.21/tcp/3981')
])
await defer.promise
})

it('emits peer event if new peer has addresses set', async () => {
const defer = pDefer()

peerStore.addEventListener('peer', () => {
defer.resolve()
})

await ab.set(await createEd25519PeerId(), [
multiaddr('/ip4/42.14.53.21/tcp/3981')
])
await defer.promise
})

it('adds the new content and emits change event', async () => {
const defer = pDefer()

Expand Down