Skip to content

Commit 1ad7057

Browse files
committed
remove added listeners in mapeo project after close
1 parent 5fee846 commit 1ad7057

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

src/mapeo-project.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,32 @@ export class MapeoProject extends TypedEmitter {
303303
this.#coreManager.creatorCore.replicate(peer.protomux)
304304
}
305305

306+
/**
307+
* @type {import('./local-peers.js').LocalPeersEvents['peer-add']}
308+
*/
309+
const onPeerAdd = (peer) => {
310+
this.#coreManager.creatorCore.replicate(peer.protomux)
311+
}
312+
313+
/**
314+
* @type {import('./local-peers.js').LocalPeersEvents['discovery-key']}
315+
*/
316+
const onDiscoverykey = (discoveryKey, stream) => {
317+
this.#syncApi[kHandleDiscoveryKey](discoveryKey, stream)
318+
}
319+
306320
// When a new peer is found, try to replicate (if it is not a member of the
307321
// project it will fail the capability check and be ignored)
308-
localPeers.on('peer-add', (peer) => {
309-
this.#coreManager.creatorCore.replicate(peer.protomux)
310-
})
322+
localPeers.on('peer-add', onPeerAdd)
311323

312324
// This happens whenever a peer replicates a core to the stream. SyncApi
313325
// handles replicating this core if we also have it, or requesting the key
314326
// for the core.
315-
localPeers.on('discovery-key', (discoveryKey, stream) => {
316-
this.#syncApi[kHandleDiscoveryKey](discoveryKey, stream)
327+
localPeers.on('discovery-key', onDiscoverykey)
328+
329+
this.once('close', () => {
330+
localPeers.off('peer-add', onPeerAdd)
331+
localPeers.off('discovery-key', onDiscoverykey)
317332
})
318333

319334
this.#l.log('Created project instance %h', projectKey)

0 commit comments

Comments
 (0)