js-libp2p
js-libp2p copied to clipboard
fix: event handlers error
For some scenarios, if a user error in a libp2p event handler is thrown, the handler execution fails silently and the user has no way of knowing about the error.
The best solution according to the EventEmitter docs is to throw all the side effects of the emit in the global context, as it happens in node.js out of the box. Several browser shims for the EventEmitter I looked into leave this to the user and do not throw in the global context. Examples:
- https://github.com/protobufjs/protobuf.js/blob/master/lib/eventemitter/index.js#L81
- https://github.com/browserify/events/blob/master/events.js#L158
As JS has a variety of shims for each building tool, it is not feasible to fix this upstream. I created proper-event-emitter that extends Event Emitter and wraps the super.emit with a try catch and throw it in the global context using setTimeout
Closes #751