Skip to content

Commit

Permalink
Add unsubscribeByID (#4061)
Browse files Browse the repository at this point in the history
* 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 <alex.luu@mail.utoronto.ca>
Co-authored-by: Alex <alex@chainsafe.io>
  • Loading branch information
3 people committed May 20, 2021
1 parent 8bfba23 commit 7d53335
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/web3-eth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/eth.subscribe.ganache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 7d53335

Please sign in to comment.