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

Commit 7d53335

Browse files
spacesailor24AlexAlex
committed
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 <alex.luu@mail.utoronto.ca> Co-authored-by: Alex <alex@chainsafe.io>
1 parent 8bfba23 commit 7d53335

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Diff for: packages/web3-eth/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ var Eth = function Eth() {
280280

281281
this.clearSubscriptions = _this._requestManager.clearSubscriptions.bind(_this._requestManager);
282282

283+
this.removeSubscriptionById = _this._requestManager.removeSubscription.bind(_this._requestManager);
284+
283285
// add net
284286
this.net = new Net(this);
285287
// add chain detection

Diff for: test/eth.subscribe.ganache.js

+27
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,33 @@ describe('subscription connect/reconnect', function () {
6363
assert.equal(0, web3.eth._requestManager.subscriptions.size);
6464
});
6565

66+
it('unsubscribes given an id', async function ( ) {
67+
subscription = web3.eth.subscribe('newBlockHeaders');
68+
await waitSeconds(1);
69+
70+
assert.equal(1, web3.eth._requestManager.subscriptions.size);
71+
web3.eth.removeSubscriptionById(subscription.id)
72+
73+
assert.equal(0, web3.eth._requestManager.subscriptions.size);
74+
75+
})
76+
77+
it('unsubscribes given an id with multiple subscriptions', async function () {
78+
79+
subscription = web3.eth.subscribe('newBlockHeaders');
80+
subscription2 = web3.eth.subscribe("logs");
81+
await waitSeconds(1);
82+
83+
assert.equal(2, web3.eth._requestManager.subscriptions.size);
84+
85+
web3.eth.removeSubscriptionById(subscription.id);
86+
assert.equal(1, web3.eth._requestManager.subscriptions.size);
87+
88+
web3.eth.removeSubscriptionById(subscription2.id);
89+
assert.equal(0, web3.eth._requestManager.subscriptions.size);
90+
91+
})
92+
6693
it('resubscribes to an existing subscription', function (done) {
6794
this.timeout(5000);
6895

0 commit comments

Comments
 (0)