mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-15 18:21:22 +00:00
feat: conditionally emit errors
test: add tests for emit override
This commit is contained in:
18
src/index.js
18
src/index.js
@ -51,7 +51,10 @@ class Node extends EventEmitter {
|
||||
this._transport = [] // Transport instances/references
|
||||
this._discovery = [] // Discovery service instances/references
|
||||
|
||||
// create the switch, and listen for errors
|
||||
this._switch = new Switch(this.peerInfo, this.peerBook, _options.switch)
|
||||
this._switch.on('error', (...args) => this.emit('error', ...args))
|
||||
|
||||
this.stats = this._switch.stats
|
||||
this.connectionManager = new ConnectionManager(this, _options.connectionManager)
|
||||
|
||||
@ -162,6 +165,21 @@ class Node extends EventEmitter {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides EventEmitter.emit to conditionally emit errors
|
||||
* if there is a handler. If not, errors will be logged.
|
||||
* @param {string} eventName
|
||||
* @param {...any} args
|
||||
* @returns {void}
|
||||
*/
|
||||
emit (eventName, ...args) {
|
||||
if (eventName === 'error' && !this._events.error) {
|
||||
log.error(...args)
|
||||
} else {
|
||||
super.emit(eventName, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the libp2p node and all sub services
|
||||
*
|
||||
|
Reference in New Issue
Block a user