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

Commit dbb4350

Browse files
spacesailor24samliorAlex
authored
Fix unsubscribe and add a test (#4058) (#4300)
* Fix unsubscribe and add a test (#4058) Co-authored-by: Alex <[email protected]> Co-authored-by: Wyatt Barnes <[email protected]> * Update CHANGELOG Co-authored-by: Samlior <[email protected]> Co-authored-by: Alex <[email protected]>
1 parent 1216049 commit dbb4350

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

CHANGELOG.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,9 @@ Released with 1.0.0-beta.37 code base.
444444
- ethers from 5.1.4 to 5.4.4 (#4231)
445445
- karma from 5.2.3 to 6.3.4 (#4231)
446446
- lerna from 3.22.1 to 4.0.0 (#4231)
447-
- Dropped build tests in CI for Node v8 and v10, and added support for Node v14
447+
- Dropped build tests in CI for Node v8 and v10, and added support for Node v14 (#4231)
448448
- Change default value for `maxPriorityFeePerGas` from `1 Gwei` to `2.5 Gwei` (#4284)
449-
- Introduced new configuration "blockHeaderTimeout" for waiting of block headers for transaction receipt (#3891)
450449
- Fixed bug in signTransaction (#4295)
451-
- Format `block.baseFeePerGas` to number (#4330)
452450

453451
## [Unreleased]
454452

@@ -465,3 +463,6 @@ Released with 1.0.0-beta.37 code base.
465463
- Not considering `tx.chainId` if `tx.common.customChain.chainId` is provided for `web3.eth.accounts.signTransaction` function (#4293)
466464
- Added missing PromiEvent handler types (#4194)
467465
- Updated README to include Webpack 5 angular support instructions (#4174)
466+
- Emit subscription id with connect event when creating a subscription (#4300)
467+
- Introduced new configuration "blockHeaderTimeout" for waiting of block headers for transaction receipt (#3891)
468+
- Format `block.baseFeePerGas` to number (#4330)

packages/web3-core-subscriptions/src/subscription.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ Subscription.prototype.subscribe = function() {
281281
if(!err && result) {
282282
_this.id = result;
283283
_this.method = payload.params[0];
284-
_this.emit('connected', result);
285284

286285
// call callback on notifications
287286
_this.options.requestManager.addSubscription(_this, function(error, result) {
@@ -310,6 +309,7 @@ Subscription.prototype.subscribe = function() {
310309
_this.emit('error', error);
311310
}
312311
});
312+
_this.emit('connected', result);
313313
} else {
314314
setTimeout(function(){
315315
_this.callback(err, false, _this);

test/eth.subscribe.ganache.js

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ describe('subscription connect/reconnect', function () {
5454
});
5555
});
5656

57+
it('unsubscribe should remove the subscription object from the subscriptions and send eth_unsubscribe to the node', function (done) {
58+
subscription = web3.eth
59+
.subscribe('newBlockHeaders')
60+
.on('connected', function () {
61+
const id = subscription.id;
62+
assert(subscription.options.requestManager.subscriptions.has(id));
63+
subscription.unsubscribe(); // Send eth_unsubscribe to the node
64+
assert(!subscription.options.requestManager.subscriptions.has(id));
65+
done();
66+
});
67+
});
68+
5769
it('clearSubscriptions', async function() {
5870
web3.eth.subscribe('newBlockHeaders');
5971
await waitSeconds(1); // Sub need a little time to set up

0 commit comments

Comments
 (0)