Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unsubscribeByID #4061

Merged
merged 8 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions packages/web3-core-subscriptions/src/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
14 changes: 14 additions & 0 deletions test/eth.subscribe.ganache.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ 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..
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think adding unsubscribeById to an instance of the Subscription class makes sense, since unsubscribe already exists - this would just be adding a semi duplicate method to the class (bloat). Instead, if we can make unsubscribeById a static method, or similar, and we could have it unsubscribe with just the id, and not have to have an instance available to call the method from, I could see this being a useful method to add

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put this comment here to reference what I'm talking about

Instead of doing:

subscription = web3.eth.subscribe(...')
subscription.unsubscribeById(...)

It would be cool if you could find a way to do

Subscription.unsubscribeById(...)

Or maybe it would be

web3.eth.subscribe.unsubscribeById(...)

so you don't have to have access to an instance of Subscription. But maybe what you have already works like that too? Idk, can you tell me? :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can probably make use of looking through web3.eth._requestManager.subscriptions and loop through the ids! Great suggestion

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! the request manager was helpful :).
It can now be called with

web3.eth. removeSubscriptionById( id)

done();
});
assert.equal(0, web3.eth._requestManager.subscriptions.size);

})

it('resubscribes to an existing subscription', function (done) {
this.timeout(5000);

Expand Down