Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Add unsubscribeByID #4061

Merged
merged 8 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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