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

WebsocketProvider improvements #2855

Merged
merged 14 commits into from
Jun 4, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default class TransactionObserver {
this.getTransactionReceiptMethod.parameters = [transactionHash];

const receipt = await this.getTransactionReceiptMethod.execute();

// on parity nodes you can get the receipt without it being mined
// so the receipt may not have a block number at this point
if (receipt && receipt.blockNumber) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ export default class AbstractSubscription extends EventEmitter {
*
* @param {AbstractWeb3Module} moduleInstance
*/
beforeSubscription(moduleInstance) {
}
beforeSubscription(moduleInstance) {}

/**
* This method will be executed on each new subscription item.
Expand Down Expand Up @@ -97,6 +96,8 @@ export default class AbstractSubscription extends EventEmitter {

this.moduleInstance.currentProvider.on('error', this.errorListener.bind(this));
this.moduleInstance.currentProvider.on(this.id, this.subscriptionListener.bind(this));

return;
})
.catch((error) => {
if (this.callback) {
Expand All @@ -107,6 +108,8 @@ export default class AbstractSubscription extends EventEmitter {

this.emit('error', error);
this.removeAllListeners();

return;
});

return this;
Expand Down Expand Up @@ -164,7 +167,7 @@ export default class AbstractSubscription extends EventEmitter {
.then((response) => {
if (!response) {
const error = new Error('Error on unsubscribe!');
if (isFunction(callback)) {
if (callback) {
callback(error, null);
}

Expand All @@ -177,7 +180,7 @@ export default class AbstractSubscription extends EventEmitter {
this.id = null;
this.removeAllListeners();

if (isFunction(callback)) {
if (callback) {
callback(false, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ export default class AbstractSocketProvider extends EventEmitter {
onError(error) {
this.emit(this.ERROR, error);
this.emit(this.SOCKET_ERROR, error);
this.removeAllSocketListeners();
this.removeAllListeners();
}

/**
Expand Down
8 changes: 7 additions & 1 deletion packages/web3-providers/src/providers/IpcProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,17 @@ export default class IpcProvider extends AbstractSocketProvider {
id = payload.id;
}

this.once(id, resolve);
this.once(id, (response) => {
resolve(response);

this.removeListener('error', reject);
});

return;
}

this.removeListener('error', reject);

return reject(new Error("Connection error: Couldn't write on the socket with Socket.write(payload)"));
});
}
Expand Down
28 changes: 17 additions & 11 deletions packages/web3-providers/src/providers/WebsocketProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,17 @@ export default class WebsocketProvider extends AbstractSocketProvider {
let timeout, id;

if (this.connection.readyState !== this.connection.OPEN) {
this.removeListener('error', reject);

return reject(new Error('Connection error: Connection is not open on send()'));
}

try {
this.connection.send(JSON.stringify(payload));
} catch (error) {
reject(error);
this.removeListener('error', reject);

return reject(error);
}

if (this.timeout) {
Expand All @@ -230,6 +234,8 @@ export default class WebsocketProvider extends AbstractSocketProvider {
clearTimeout(timeout);
}

this.removeListener('error', reject);

return resolve(response);
});

Expand All @@ -238,17 +244,17 @@ export default class WebsocketProvider extends AbstractSocketProvider {

this.once('connect', () => {
this.sendPayload(payload)
.then(resolve)
.catch(reject);
.then((response) => {
this.removeListener('error', reject);

return resolve(response);
})
.catch((error) => {
this.removeListener('error', reject);

return reject(error);
});
});
}).then((response) => {
this.removeListener('error', reject);

return response;
}).catch((error) => {
this.removeListener('error', reject);

throw error;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ describe('AbstractSocketProviderTest', () => {
abstractSocketProvider.removeAllListeners = jest.fn();

abstractSocketProvider.onError('not ready!');

expect(abstractSocketProvider.removeAllListeners).toHaveBeenCalled();
});

it('calls onClose and close event will be emitted', (done) => {
Expand Down