From 8c64aa3103daed44cef74d243bf427f56da4ba8a Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 11 Apr 2023 08:31:55 +0100 Subject: [PATCH] fix: dispatch peer event on adding new addresses as well as set The boolean check was incorrect. --- src/address-book.ts | 4 ++-- test/address-book.spec.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/address-book.ts b/src/address-book.ts index fd3d10f..0552d8c 100644 --- a/src/address-book.ts +++ b/src/address-book.ts @@ -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 @@ -291,7 +291,7 @@ export class PeerStoreAddressBook { })) // Notify the existence of a new peer - if (hasPeer === true) { + if (!hasPeer) { this.dispatchEvent(new CustomEvent('peer', { detail: { id: peerId, diff --git a/test/address-book.spec.ts b/test/address-book.spec.ts index f743562..35a7b25 100644 --- a/test/address-book.spec.ts +++ b/test/address-book.spec.ts @@ -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()