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

chore: use emittery #863

Closed
wants to merge 1 commit into from
Closed
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"cids": "^1.0.0",
"class-is": "^1.1.0",
"debug": "^4.1.1",
"emittery": "^0.8.1",
"err-code": "^2.0.0",
"events": "^3.1.0",
"hashlru": "^2.3.0",
Expand Down
6 changes: 3 additions & 3 deletions src/connection-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const mergeOptions = require('merge-options')
const LatencyMonitor = require('./latency-monitor')
const retimer = require('retimer')

const { EventEmitter } = require('events')
const Emittery = require('emittery')

const PeerId = require('peer-id')

Expand Down Expand Up @@ -56,7 +56,7 @@ const defaultOptions = {
* @fires ConnectionManager#peer:connect Emitted when a new peer is connected.
* @fires ConnectionManager#peer:disconnect Emitted when a peer is disconnected.
*/
class ConnectionManager extends EventEmitter {
class ConnectionManager extends Emittery {
/**
* Responsible for managing known connections.
*
Expand Down Expand Up @@ -137,7 +137,7 @@ class ConnectionManager extends EventEmitter {
async stop () {
this._autoDialTimeout && this._autoDialTimeout.clear()
this._timer && this._timer.clear()
this._latencyMonitor && this._latencyMonitor.removeListener('data', this._onLatencyMeasure)
this._latencyMonitor && this._latencyMonitor.off('data', this._onLatencyMeasure)

this._started = false
await this._close()
Expand Down
6 changes: 3 additions & 3 deletions src/connection-manager/latency-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/* global window */
const globalThis = require('ipfs-utils/src/globalthis')
const { EventEmitter } = require('events')
const Emittery = require('emittery')
const VisibilityChangeEmitter = require('./visibility-change-emitter')
const debug = require('debug')('latency-monitor:LatencyMonitor')

Expand All @@ -31,7 +31,7 @@ const debug = require('debug')('latency-monitor:LatencyMonitor')
* the asyncTestFn and timing how long it takes the callback to be called. It can also periodically emit stats about this.
* This can be disabled and stats can be pulled via setting dataEmitIntervalMs = 0.
*
* @extends {EventEmitter}
* @extends {Emittery}
*
* The default implementation is an event loop latency monitor. This works by firing periodic events into the event loop
* and timing how long it takes to get back.
Expand All @@ -44,7 +44,7 @@ const debug = require('debug')('latency-monitor:LatencyMonitor')
* const monitor = new LatencyMonitor({latencyCheckIntervalMs: 1000, dataEmitIntervalMs: 60000, asyncTestFn:ping});
* monitor.on('data', (summary) => console.log('Ping Pong Latency: %O', summary));
*/
class LatencyMonitor extends EventEmitter {
class LatencyMonitor extends Emittery {
/**
* @class
* @param {LatencyMonitorOptions} [options]
Expand Down
4 changes: 2 additions & 2 deletions src/connection-manager/visibility-change-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
'use strict'

const { EventEmitter } = require('events')
const Emittery = require('emittery')

const debug = require('debug')('latency-monitor:VisibilityChangeEmitter')

Expand All @@ -32,7 +32,7 @@ const debug = require('debug')('latency-monitor:VisibilityChangeEmitter')
* // To access the visibility state directly, call:
* console.log('Am I focused now? ' + myVisibilityEmitter.isVisible());
*/
class VisibilityChangeEmitter extends EventEmitter {
class VisibilityChangeEmitter extends Emittery {
/**
* Creates a VisibilityChangeEmitter
*
Expand Down
26 changes: 14 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const debug = require('debug')
const log = Object.assign(debug('libp2p'), {
error: debug('libp2p:err')
})
const { EventEmitter } = require('events')
const Emittery = require('emittery')
const globalThis = require('ipfs-utils/src/globalthis')

const errCode = require('err-code')
Expand Down Expand Up @@ -83,11 +83,11 @@ const IDENTIFY_PROTOCOLS = IdentifyService.multicodecs
* @typedef {Object} CreateOptions
* @property {PeerId} peerId
*
* @extends {EventEmitter}
* @extends {Emittery}
* @fires Libp2p#error Emitted when an error occurs
* @fires Libp2p#peer:discovery Emitted when a peer is discovered
*/
class Libp2p extends EventEmitter {
class Libp2p extends Emittery {
/**
* Like `new Libp2p(options)` except it will create a `PeerId`
* instance if one is not provided in options.
Expand Down Expand Up @@ -284,17 +284,19 @@ class Libp2p extends EventEmitter {
*
* @param {string} eventName
* @param {...any} args
* @returns {boolean}
* @returns {Promise<void>}
*/
emit (eventName, ...args) {
// TODO: do we still need this?
// @ts-ignore _events does not exist in libp2p
if (eventName === 'error' && !this._events.error) {
log.error(args)
return false
} else {
return super.emit(eventName, ...args)
}
return new Promise((resolve) => {
// TODO: do we still need this?
// @ts-ignore _events does not exist in libp2p
if (eventName === 'error' && !this._events.error) {
log.error(args)
resolve()
} else {
super.emit(eventName, args).then(resolve)
}
})
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/metrics/stats.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// @ts-nocheck
'use strict'

const { EventEmitter } = require('events')
const Emittery = require('emittery')
const Big = require('bignumber.js')
const MovingAverage = require('moving-average')
const retimer = require('retimer')

class Stats extends EventEmitter {
class Stats extends Emittery {
/**
* A queue based manager for stat processing
*
Expand Down
6 changes: 3 additions & 3 deletions src/peer-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const errcode = require('err-code')

const { EventEmitter } = require('events')
const Emittery = require('emittery')
const PeerId = require('peer-id')

const AddressBook = require('./address-book')
Expand All @@ -19,15 +19,15 @@ const {
*/

/**
* @extends {EventEmitter}
* @extends {Emittery}
*
* @fires PeerStore#peer Emitted when a new peer is added.
* @fires PeerStore#change:protocols Emitted when a known peer supports a different set of protocols.
* @fires PeerStore#change:multiaddrs Emitted when a known peer has a different set of multiaddrs.
* @fires PeerStore#change:pubkey Emitted emitted when a peer's public key is known.
* @fires PeerStore#change:metadata Emitted when the known metadata of a peer change.
*/
class PeerStore extends EventEmitter {
class PeerStore extends Emittery {
/**
* Peer object
*
Expand Down
2 changes: 1 addition & 1 deletion src/peer-store/persistent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class PersistentPeerStore extends PeerStore {
*/
async stop () {
log('PeerStore is stopping')
this.removeAllListeners()
this.clearListeners()
await this._commitData()
log('PeerStore stopped')
}
Expand Down