From 6b107e42428a20d01cbf03eb192b8656ecc139bc Mon Sep 17 00:00:00 2001 From: Marcus Pousette Date: Tue, 5 Dec 2023 11:00:17 +0100 Subject: [PATCH] fix: remove event listeners first on close --- .../programs/data/shared-log/src/index.ts | 41 +++++-------------- 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/packages/programs/data/shared-log/src/index.ts b/packages/programs/data/shared-log/src/index.ts index 56c9feb7d..b091a8c05 100644 --- a/packages/programs/data/shared-log/src/index.ts +++ b/packages/programs/data/shared-log/src/index.ts @@ -393,6 +393,17 @@ export class SharedLog extends Program< private async _close() { this._closeController.abort(); + this.node.services.pubsub.removeEventListener( + "subscribe", + this._onSubscriptionFn + ); + + this._onUnsubscriptionFn = this._onUnsubscription.bind(this); + this.node.services.pubsub.removeEventListener( + "unsubscribe", + this._onUnsubscriptionFn + ); + for (const [k, v] of this._pendingDeletes) { v.clear(); v.promise.resolve(); // TODO or reject? @@ -410,17 +421,6 @@ export class SharedLog extends Program< this._gidPeersHistory = new Map(); this._sortedPeersCache = undefined; this._loadedOnce = false; - - this.node.services.pubsub.removeEventListener( - "subscribe", - this._onSubscriptionFn - ); - - this._onUnsubscriptionFn = this._onUnsubscription.bind(this); - this.node.services.pubsub.removeEventListener( - "unsubscribe", - this._onUnsubscriptionFn - ); } async close(from?: Program): Promise { const superClosed = await super.close(from); @@ -799,25 +799,6 @@ export class SharedLog extends Program< return this.findLeadersFromUniformNumber(cursor, numberOfLeaders, options); } - /* getParticipationSum(age: number) { - let sum = 0; - const t = +new Date; - let currentNode = this.getReplicatorsSorted()?.head; - while (currentNode) { - - const value = currentNode.value; - if (t - value.timestamp > age || currentNode.value.publicKey.equals(this.node.identity.publicKey)) { - sum += value.length; - } - if (sum >= 1) { - return 1; - } - currentNode = currentNode.next - } - - return sum; - } */ - private findLeadersFromUniformNumber( cursor: number, numberOfLeaders: number,