Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Closed
Next Next commit
Attach ws event listeners using EventTarget API
When using `.on<event>=fn` to attach listeners, only one listener can be
set at the same time. Since multiple request managers can use the same
provider, the EventTarget API has to be used to ensure all of them
receive the events emitted from the provider.

This is needed on both the `on` and `removeListener` functions.
  • Loading branch information
gabmontes authored and cgewecke committed Nov 6, 2019
commit 57be867914fd2a4dcbd4ea9c1cd874cec242452a
18 changes: 14 additions & 4 deletions packages/web3-providers-ws/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,15 @@ WebsocketProvider.prototype.on = function (type, callback) {
break;

case 'connect':
this.connection.onopen = callback;
this.connection.addEventListener('open', callback);
break;

case 'end':
this.connection.onclose = callback;
this.connection.addEventListener('close', callback);
break;

case 'error':
this.connection.onerror = callback;
this.connection.addEventListener('error', callback);
break;

// default:
Expand Down Expand Up @@ -342,7 +342,17 @@ WebsocketProvider.prototype.removeListener = function (type, callback) {
});
break;

// TODO remvoving connect missing
case 'connect':
this.connection.removeEventListener('open', callback);
break;

case 'end':
this.connection.removeEventListener('close', callback);
break;

case 'error':
this.connection.removeEventListener('error', callback);
break;

// default:
// this.connection.removeListener(type, callback);
Expand Down