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

clearSubscriptions method in AbstractSocketProvider fixed #3007

Merged
merged 6 commits into from
Aug 5, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- stripHexPrefix fixed (#2989)
- BatchRequest error handling fixed for callbacks (#2993)
- ``reconnected`` event and reconnection timeout option added to WebsocketProvider (#2994)
- ``clearSubscriptions`` fixed (#3007)
2 changes: 0 additions & 2 deletions packages/web3-core-method/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ export class AbstractMethod {
afterExecution(response: any): any;

execute(): Promise<any> | PromiEvent<any> | string;

clearSubscriptions(unsubscribeMethod: string): Promise<boolean | Error>;
}

export class AbstractMethodFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,3 @@ abstractMethod.afterExecution('response');

// $ExpectType string | PromiEvent<any> | Promise<any>
abstractMethod.execute();

// $ExpectType Promise<boolean | Error>
abstractMethod.clearSubscriptions('eth_unsubscribe');
9 changes: 4 additions & 5 deletions packages/web3-core/src/AbstractWeb3Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class AbstractWeb3Module {
* @constructor
*/
constructor(provider, options = {}, methodFactory = null, nodeNet = null) {
// ProviderDetector and ProviderResolver are created in the constructor for providing a simpler Web3 Module API.
// TODO: Move the ProviderResolver dependency injection to the planned public_api layer.
this.providerResolver = new ProviderResolver();
this.givenProvider = ProviderDetector.detect();

Expand Down Expand Up @@ -257,6 +257,8 @@ export default class AbstractWeb3Module {
}

/**
* TODO: setProvider has to be asynchronous because of the clearSubscriptions method.
*
* Sets the currentProvider and provider property
*
* @method setProvider
Expand Down Expand Up @@ -309,10 +311,7 @@ export default class AbstractWeb3Module {
* @returns {Promise<Boolean|Error>}
*/
clearSubscriptions(unsubscribeMethod) {
if (
typeof this.currentProvider.clearSubscriptions !== 'undefined' &&
this.currentProvider.subscriptions.length > 0
) {
if (this.currentProvider.supportsSubscriptions()) {
return this.currentProvider.clearSubscriptions(unsubscribeMethod);
}

Expand Down
48 changes: 30 additions & 18 deletions packages/web3-providers/lib/providers/AbstractSocketProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export default class AbstractSocketProvider extends EventEmitter {
*
* @method registerEventListeners
*/
registerEventListeners() {}
registerEventListeners() {
}

/**
* Removes all socket listeners
Expand All @@ -90,7 +91,8 @@ export default class AbstractSocketProvider extends EventEmitter {
* @param {Number} code
* @param {String} reason
*/
disconnect(code, reason) {}
disconnect(code, reason) {
}

/**
* Returns true if the socket is connected
Expand All @@ -99,7 +101,8 @@ export default class AbstractSocketProvider extends EventEmitter {
*
* @returns {Boolean}
*/
get connected() {}
get connected() {
}

/**
* Creates the JSON-RPC payload and sends it to the node.
Expand Down Expand Up @@ -260,7 +263,7 @@ export default class AbstractSocketProvider extends EventEmitter {
*
* @returns {Promise<String|Error>}
*/
subscribe(subscribeMethod = 'eth_subscribe', subscriptionMethod, parameters) {
subscribe(subscribeMethod, subscriptionMethod, parameters) {
parameters.unshift(subscriptionMethod);

return this.send(subscribeMethod, parameters)
Expand Down Expand Up @@ -288,7 +291,7 @@ export default class AbstractSocketProvider extends EventEmitter {
*
* @returns {Promise<Boolean|Error>}
*/
unsubscribe(subscriptionId, unsubscribeMethod = 'eth_unsubscribe') {
unsubscribe(subscriptionId, unsubscribeMethod) {
if (this.hasSubscription(subscriptionId)) {
return this.send(unsubscribeMethod, [subscriptionId]).then((response) => {
if (response) {
Expand All @@ -313,22 +316,31 @@ export default class AbstractSocketProvider extends EventEmitter {
*
* @returns {Promise<Boolean|Error>}
*/
clearSubscriptions(unsubscribeMethod = 'eth_unsubscribe') {
let unsubscribePromises = [];

this.subscriptions.forEach((value, key) => {
this.removeAllListeners(key);
clearSubscriptions(unsubscribeMethod = '') {
if (this.subscriptions.size > 0) {
let unsubscribePromises = [];
const type = unsubscribeMethod.slice(0, 3);

this.subscriptions.forEach((value) => {
if (type === '') {
unsubscribePromises.push(
this.unsubscribe(value.id, `${value.subscribeMethod.slice(0, 3)}_unsubscribe`)
);
} else if (type === value.subscribeMethod.slice(0, 3)) {
unsubscribePromises.push(this.unsubscribe(value.id, unsubscribeMethod));
}
});

unsubscribePromises.push(this.unsubscribe(value.id, unsubscribeMethod));
});
return Promise.all(unsubscribePromises).then((results) => {
if (results.includes(false)) {
throw new Error(`Could not unsubscribe all subscriptions: ${JSON.stringify(results)}`);
}

return Promise.all(unsubscribePromises).then((results) => {
if (results.includes(false)) {
throw new Error(`Could not unsubscribe all subscriptions: ${JSON.stringify(results)}`);
}
return true;
});
}

return true;
});
return Promise.resolve(true);
}

/**
Expand Down
Loading