From ec8b17352a9be54ccf7683ae55c58e33ba9a2f13 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Mon, 25 Sep 2023 19:32:36 +0100 Subject: [PATCH] fix: Deregister all events to complete stop sync. --- src/sync.js | 3 ++- test/databases/replication/events.test.js | 8 +++++--- test/utils/create-helia.js | 14 +------------- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/sync.js b/src/sync.js index 80dd591..166fa42 100644 --- a/src/sync.js +++ b/src/sync.js @@ -227,7 +227,7 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => { events.emit('error', e) } } - + if (message.detail.topic === address) { queue.add(task) } @@ -256,6 +256,7 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => { if (started) { await queue.onIdle() ipfs.libp2p.services.pubsub.removeEventListener('subscription-change', handlePeerSubscribed) + ipfs.libp2p.services.pubsub.removeEventListener('message', handleUpdateMessage) await ipfs.libp2p.unhandle(headsSyncAddress) await ipfs.libp2p.services.pubsub.unsubscribe(address) peers.clear() diff --git a/test/databases/replication/events.test.js b/test/databases/replication/events.test.js index e96b9dc..c956582 100644 --- a/test/databases/replication/events.test.js +++ b/test/databases/replication/events.test.js @@ -74,10 +74,12 @@ describe('Events Database Replication', function () { if (db1) { await db1.drop() await db1.close() + db1 = null } if (db2) { await db2.drop() await db2.close() + db2 = null } }) @@ -128,9 +130,6 @@ describe('Events Database Replication', function () { }) it('loads the database after replication', async () => { - db1 = await Events()({ ipfs: ipfs1, identity: testIdentity1, address: databaseId, accessController, directory: './orbitdb1' }) - db2 = await Events()({ ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2' }) - let replicated = false let expectedEntryHash = null @@ -146,6 +145,9 @@ describe('Events Database Replication', function () { console.error(err) } + db1 = await Events()({ ipfs: ipfs1, identity: testIdentity1, address: databaseId, accessController, directory: './orbitdb1' }) + db2 = await Events()({ ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2' }) + db2.events.on('join', onConnected) db2.events.on('update', onUpdate) diff --git a/test/utils/create-helia.js b/test/utils/create-helia.js index aedd614..f3c101d 100644 --- a/test/utils/create-helia.js +++ b/test/utils/create-helia.js @@ -39,20 +39,8 @@ export default async () => { pubsub: gossipsub({ allowPublishToZeroPeers: true }) } } + const libp2p = await createLibp2p(options) - /* const libp2p = await createLibp2p({ - connectionGater: { denyDialMultiaddr: () => false }, - transports: [webSockets({ filter: all }), webRTC()], - connectionEncryption: [noise()], - streamMuxers: [mplex()], - services: { - identify: identifyService(), - pubsub: gossipsub() - }, - addresses: { - listen: [isBrowser() ? '/ip4/0.0.0.0/tcp/12345/ws/p2p-webrtc-star' : '/ip4/0.0.0.0/tcp/0/ws'] - } -}) */ return await createHelia({ libp2p }) }