diff --git a/src/address-book.ts b/src/address-book.ts index fd3d10ffe3..0552d8ca45 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 f743562dcc..35a7b250f0 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()