From 5d11a5ce7b2c34340ff47580024b52fabb81cab4 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 11 May 2021 17:08:44 -0400 Subject: [PATCH 1/7] adding function unsubscribe by id --- packages/web3-core-subscriptions/src/subscription.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/web3-core-subscriptions/src/subscription.js b/packages/web3-core-subscriptions/src/subscription.js index 9303b98401d..196791278cc 100644 --- a/packages/web3-core-subscriptions/src/subscription.js +++ b/packages/web3-core-subscriptions/src/subscription.js @@ -333,4 +333,10 @@ Subscription.prototype.resubscribe = function () { this.subscribe(this.callback); }; +Subscription.prototype.unsubscribeByID = function(id) { + if (this.id == id){ + this.unsubscribe(); + } +}; + module.exports = Subscription; From 45ef1c1c29e1d66e97d6df885dda4f2d01f39db5 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 12 May 2021 09:45:24 -0400 Subject: [PATCH 2/7] adding an unsubscribe testcase --- test/eth.subscribe.ganache.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/eth.subscribe.ganache.js b/test/eth.subscribe.ganache.js index a2dce67f9d0..ddb0f8ee649 100644 --- a/test/eth.subscribe.ganache.js +++ b/test/eth.subscribe.ganache.js @@ -63,6 +63,18 @@ describe('subscription connect/reconnect', function () { assert.equal(0, web3.eth._requestManager.subscriptions.size); }); + it('unsubscribes given an id', function (done) { + subscription = web3.eth.subscribe('newBlockHeaders') + .on("connected", function(subscriptionId) { + assert.equal(1, web3.eth._requestManager.subscriptions.size); + assert.ok(web3.eth.unsubscribeByID(subscriptionId)) + }); + assert.equal(0, web3.eth._requestManager.subscriptions.size); + + + + }) + it('resubscribes to an existing subscription', function (done) { this.timeout(5000); From d4c69bc7b66f9ebe0962174b807375ddb7b054d7 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 12 May 2021 16:21:28 -0400 Subject: [PATCH 3/7] adding testcase --- test/eth.subscribe.ganache.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/eth.subscribe.ganache.js b/test/eth.subscribe.ganache.js index ddb0f8ee649..c42c7d22801 100644 --- a/test/eth.subscribe.ganache.js +++ b/test/eth.subscribe.ganache.js @@ -64,14 +64,16 @@ describe('subscription connect/reconnect', function () { }); it('unsubscribes given an id', function (done) { - subscription = web3.eth.subscribe('newBlockHeaders') - .on("connected", function(subscriptionId) { - assert.equal(1, web3.eth._requestManager.subscriptions.size); - assert.ok(web3.eth.unsubscribeByID(subscriptionId)) - }); assert.equal(0, web3.eth._requestManager.subscriptions.size); - - + subscription = web3.eth + .subscribe('newBlockHeaders') + .on('connected', function (result) { + assert(result) + assert.equal(1, web3.eth._requestManager.subscriptions.size); + subscription.unsubscribeById(subscription.id); // Stop listening.. + done(); + }); + assert.equal(0, web3.eth._requestManager.subscriptions.size); }) From 921d542d7190dad01fcebfbf740920b57c090c7b Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 13 May 2021 14:19:25 -0400 Subject: [PATCH 4/7] seperated unsubscribebyid to its own method --- .../src/subscription.js | 6 ----- packages/web3-eth/src/index.js | 2 ++ test/eth.subscribe.ganache.js | 24 +++++++++---------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/packages/web3-core-subscriptions/src/subscription.js b/packages/web3-core-subscriptions/src/subscription.js index 196791278cc..9303b98401d 100644 --- a/packages/web3-core-subscriptions/src/subscription.js +++ b/packages/web3-core-subscriptions/src/subscription.js @@ -333,10 +333,4 @@ Subscription.prototype.resubscribe = function () { this.subscribe(this.callback); }; -Subscription.prototype.unsubscribeByID = function(id) { - if (this.id == id){ - this.unsubscribe(); - } -}; - module.exports = Subscription; 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 c42c7d22801..f9a8368d150 100644 --- a/test/eth.subscribe.ganache.js +++ b/test/eth.subscribe.ganache.js @@ -63,19 +63,19 @@ describe('subscription connect/reconnect', function () { assert.equal(0, web3.eth._requestManager.subscriptions.size); }); - it('unsubscribes given an id', function (done) { - assert.equal(0, web3.eth._requestManager.subscriptions.size); - subscription = web3.eth - .subscribe('newBlockHeaders') - .on('connected', function (result) { - assert(result) - assert.equal(1, web3.eth._requestManager.subscriptions.size); - subscription.unsubscribeById(subscription.id); // Stop listening.. - done(); - }); - assert.equal(0, web3.eth._requestManager.subscriptions.size); + // it('unsubscribes given an id', function (done) { + // assert.equal(0, web3.eth._requestManager.subscriptions.size); + // subscription = web3.eth + // .subscribe('newBlockHeaders') + // .on('connected', function (result) { + // assert(result) + // assert.equal(1, web3.eth._requestManager.subscriptions.size); + // subscription.unsubscribeById(subscription.id); // Stop listening.. + // done(); + // }); + // assert.equal(0, web3.eth._requestManager.subscriptions.size); - }) + // }) it('resubscribes to an existing subscription', function (done) { this.timeout(5000); From 14ca9445de8356a47720df33528258d618a97e24 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 14 May 2021 01:08:09 -0400 Subject: [PATCH 5/7] adding testcases --- test/eth.subscribe.ganache.js | 40 ++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/test/eth.subscribe.ganache.js b/test/eth.subscribe.ganache.js index f9a8368d150..e0cbe4721bf 100644 --- a/test/eth.subscribe.ganache.js +++ b/test/eth.subscribe.ganache.js @@ -63,19 +63,35 @@ describe('subscription connect/reconnect', function () { assert.equal(0, web3.eth._requestManager.subscriptions.size); }); - // it('unsubscribes given an id', function (done) { - // assert.equal(0, web3.eth._requestManager.subscriptions.size); - // subscription = web3.eth - // .subscribe('newBlockHeaders') - // .on('connected', function (result) { - // assert(result) - // assert.equal(1, web3.eth._requestManager.subscriptions.size); - // subscription.unsubscribeById(subscription.id); // Stop listening.. - // done(); - // }); - // assert.equal(0, web3.eth._requestManager.subscriptions.size); + it('unsubscribes given an id', async function () { + assert.equal(0, web3.eth._requestManager.subscriptions.size); + subscription = web3.eth.subscribe('newBlockHeaders'); + await waitSeconds(1); + + assert.equal(1, web3.eth._requestManager.subscriptions.size); + assert(web3.eth.removeSubscriptionById(subscription.id)) + assert.equal(0, web3.eth._requestManager.subscriptions.size); + - // }) + }) + + it('unsubscribes given an id with multiple subscriptions', async function () { + assert.equal(0, web3.eth._requestManager.subscriptions.size); + + subscription = web3.eth.subscribe('newBlockHeaders'); + subscription2 = web3.eth.subscribe("logs") + + await waitSeconds(1); + + assert.equal(2, web3.eth._requestManager.subscriptions.size); + + assert(web3.eth.removeSubscriptionById(subscription.id)); + assert.equal(1, web3.eth._requestManager.subscriptions.size); + + assert(web3.eth.removeSubscriptionById(subscription2.id)) + assert.equal(0, web3.eth._requestManager.subscriptions.size); + + }) it('resubscribes to an existing subscription', function (done) { this.timeout(5000); From 174ed693a691059027eeeeda046e48aec5ffab26 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 14 May 2021 01:33:22 -0400 Subject: [PATCH 6/7] adding await --- test/eth.subscribe.ganache.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/eth.subscribe.ganache.js b/test/eth.subscribe.ganache.js index e0cbe4721bf..6573d10882d 100644 --- a/test/eth.subscribe.ganache.js +++ b/test/eth.subscribe.ganache.js @@ -79,6 +79,9 @@ describe('subscription connect/reconnect', function () { assert.equal(0, web3.eth._requestManager.subscriptions.size); subscription = web3.eth.subscribe('newBlockHeaders'); + + await waitSeconds(1); + subscription2 = web3.eth.subscribe("logs") await waitSeconds(1); From 920d17a14a119f0dc07b734cc1a89caddf960c81 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 15 May 2021 11:30:04 -0400 Subject: [PATCH 7/7] fixing testcases --- test/eth.subscribe.ganache.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/test/eth.subscribe.ganache.js b/test/eth.subscribe.ganache.js index 6573d10882d..82c47ff6fba 100644 --- a/test/eth.subscribe.ganache.js +++ b/test/eth.subscribe.ganache.js @@ -63,35 +63,29 @@ describe('subscription connect/reconnect', function () { assert.equal(0, web3.eth._requestManager.subscriptions.size); }); - it('unsubscribes given an id', async 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); - assert(web3.eth.removeSubscriptionById(subscription.id)) - assert.equal(0, 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 () { - assert.equal(0, web3.eth._requestManager.subscriptions.size); subscription = web3.eth.subscribe('newBlockHeaders'); - - await waitSeconds(1); - - subscription2 = web3.eth.subscribe("logs") - + subscription2 = web3.eth.subscribe("logs"); await waitSeconds(1); assert.equal(2, web3.eth._requestManager.subscriptions.size); - assert(web3.eth.removeSubscriptionById(subscription.id)); + web3.eth.removeSubscriptionById(subscription.id); assert.equal(1, web3.eth._requestManager.subscriptions.size); - assert(web3.eth.removeSubscriptionById(subscription2.id)) + web3.eth.removeSubscriptionById(subscription2.id); assert.equal(0, web3.eth._requestManager.subscriptions.size); })