From 5df34373d4ba5df214ce4947db0445e9900feb6f Mon Sep 17 00:00:00 2001 From: Wyatt Barnes Date: Mon, 17 May 2021 08:22:32 -0500 Subject: [PATCH] Add unsubscribeByID (#4061) * adding function unsubscribe by id * adding an unsubscribe testcase * adding testcase * seperated unsubscribebyid to its own method * adding testcases * adding await * fixing testcases Co-authored-by: Alex Co-authored-by: Alex --- packages/web3-eth/src/index.js | 2 ++ test/eth.subscribe.ganache.js | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/packages/web3-eth/src/index.js b/packages/web3-eth/src/index.js index 469c149eca8..0bd9f42fe81 100644 --- a/packages/web3-eth/src/index.js +++ b/packages/web3-eth/src/index.js @@ -280,6 +280,8 @@ var Eth = function Eth() { this.clearSubscriptions = _this._requestManager.clearSubscriptions.bind(_this._requestManager); + this.removeSubscriptionById = _this._requestManager.removeSubscription.bind(_this._requestManager); + // add net this.net = new Net(this); // add chain detection diff --git a/test/eth.subscribe.ganache.js b/test/eth.subscribe.ganache.js index a2dce67f9d0..82c47ff6fba 100644 --- a/test/eth.subscribe.ganache.js +++ b/test/eth.subscribe.ganache.js @@ -63,6 +63,33 @@ describe('subscription connect/reconnect', function () { assert.equal(0, web3.eth._requestManager.subscriptions.size); }); + it('unsubscribes given an id', async function ( ) { + subscription = web3.eth.subscribe('newBlockHeaders'); + await waitSeconds(1); + + assert.equal(1, web3.eth._requestManager.subscriptions.size); + web3.eth.removeSubscriptionById(subscription.id) + + assert.equal(0, web3.eth._requestManager.subscriptions.size); + + }) + + it('unsubscribes given an id with multiple subscriptions', async function () { + + subscription = web3.eth.subscribe('newBlockHeaders'); + subscription2 = web3.eth.subscribe("logs"); + await waitSeconds(1); + + assert.equal(2, web3.eth._requestManager.subscriptions.size); + + web3.eth.removeSubscriptionById(subscription.id); + assert.equal(1, web3.eth._requestManager.subscriptions.size); + + web3.eth.removeSubscriptionById(subscription2.id); + assert.equal(0, web3.eth._requestManager.subscriptions.size); + + }) + it('resubscribes to an existing subscription', function (done) { this.timeout(5000);