mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
chore: use emittery
This commit is contained in:
parent
77e8273a64
commit
3d2511db7b
@ -58,6 +58,7 @@
|
|||||||
"cids": "^1.0.0",
|
"cids": "^1.0.0",
|
||||||
"class-is": "^1.1.0",
|
"class-is": "^1.1.0",
|
||||||
"debug": "^4.1.1",
|
"debug": "^4.1.1",
|
||||||
|
"emittery": "^0.8.1",
|
||||||
"err-code": "^2.0.0",
|
"err-code": "^2.0.0",
|
||||||
"events": "^3.1.0",
|
"events": "^3.1.0",
|
||||||
"hashlru": "^2.3.0",
|
"hashlru": "^2.3.0",
|
||||||
|
@ -10,7 +10,7 @@ const mergeOptions = require('merge-options')
|
|||||||
const LatencyMonitor = require('./latency-monitor')
|
const LatencyMonitor = require('./latency-monitor')
|
||||||
const retimer = require('retimer')
|
const retimer = require('retimer')
|
||||||
|
|
||||||
const { EventEmitter } = require('events')
|
const Emittery = require('emittery')
|
||||||
|
|
||||||
const PeerId = require('peer-id')
|
const PeerId = require('peer-id')
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ const defaultOptions = {
|
|||||||
* @fires ConnectionManager#peer:connect Emitted when a new peer is connected.
|
* @fires ConnectionManager#peer:connect Emitted when a new peer is connected.
|
||||||
* @fires ConnectionManager#peer:disconnect Emitted when a peer is disconnected.
|
* @fires ConnectionManager#peer:disconnect Emitted when a peer is disconnected.
|
||||||
*/
|
*/
|
||||||
class ConnectionManager extends EventEmitter {
|
class ConnectionManager extends Emittery {
|
||||||
/**
|
/**
|
||||||
* Responsible for managing known connections.
|
* Responsible for managing known connections.
|
||||||
*
|
*
|
||||||
@ -137,7 +137,7 @@ class ConnectionManager extends EventEmitter {
|
|||||||
async stop () {
|
async stop () {
|
||||||
this._autoDialTimeout && this._autoDialTimeout.clear()
|
this._autoDialTimeout && this._autoDialTimeout.clear()
|
||||||
this._timer && this._timer.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
|
this._started = false
|
||||||
await this._close()
|
await this._close()
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
/* global window */
|
/* global window */
|
||||||
const globalThis = require('ipfs-utils/src/globalthis')
|
const globalThis = require('ipfs-utils/src/globalthis')
|
||||||
const { EventEmitter } = require('events')
|
const Emittery = require('emittery')
|
||||||
const VisibilityChangeEmitter = require('./visibility-change-emitter')
|
const VisibilityChangeEmitter = require('./visibility-change-emitter')
|
||||||
const debug = require('debug')('latency-monitor:LatencyMonitor')
|
const debug = require('debug')('latency-monitor:LatencyMonitor')
|
||||||
|
|
||||||
@ -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.
|
* 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.
|
* 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
|
* 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.
|
* and timing how long it takes to get back.
|
||||||
@ -44,7 +44,7 @@ const debug = require('debug')('latency-monitor:LatencyMonitor')
|
|||||||
* const monitor = new LatencyMonitor({latencyCheckIntervalMs: 1000, dataEmitIntervalMs: 60000, asyncTestFn:ping});
|
* const monitor = new LatencyMonitor({latencyCheckIntervalMs: 1000, dataEmitIntervalMs: 60000, asyncTestFn:ping});
|
||||||
* monitor.on('data', (summary) => console.log('Ping Pong Latency: %O', summary));
|
* monitor.on('data', (summary) => console.log('Ping Pong Latency: %O', summary));
|
||||||
*/
|
*/
|
||||||
class LatencyMonitor extends EventEmitter {
|
class LatencyMonitor extends Emittery {
|
||||||
/**
|
/**
|
||||||
* @class
|
* @class
|
||||||
* @param {LatencyMonitorOptions} [options]
|
* @param {LatencyMonitorOptions} [options]
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const { EventEmitter } = require('events')
|
const Emittery = require('emittery')
|
||||||
|
|
||||||
const debug = require('debug')('latency-monitor:VisibilityChangeEmitter')
|
const debug = require('debug')('latency-monitor:VisibilityChangeEmitter')
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ const debug = require('debug')('latency-monitor:VisibilityChangeEmitter')
|
|||||||
* // To access the visibility state directly, call:
|
* // To access the visibility state directly, call:
|
||||||
* console.log('Am I focused now? ' + myVisibilityEmitter.isVisible());
|
* console.log('Am I focused now? ' + myVisibilityEmitter.isVisible());
|
||||||
*/
|
*/
|
||||||
class VisibilityChangeEmitter extends EventEmitter {
|
class VisibilityChangeEmitter extends Emittery {
|
||||||
/**
|
/**
|
||||||
* Creates a VisibilityChangeEmitter
|
* Creates a VisibilityChangeEmitter
|
||||||
*
|
*
|
||||||
|
14
src/index.js
14
src/index.js
@ -4,7 +4,7 @@ const debug = require('debug')
|
|||||||
const log = Object.assign(debug('libp2p'), {
|
const log = Object.assign(debug('libp2p'), {
|
||||||
error: debug('libp2p:err')
|
error: debug('libp2p:err')
|
||||||
})
|
})
|
||||||
const { EventEmitter } = require('events')
|
const Emittery = require('emittery')
|
||||||
const globalThis = require('ipfs-utils/src/globalthis')
|
const globalThis = require('ipfs-utils/src/globalthis')
|
||||||
|
|
||||||
const errCode = require('err-code')
|
const errCode = require('err-code')
|
||||||
@ -83,11 +83,11 @@ const IDENTIFY_PROTOCOLS = IdentifyService.multicodecs
|
|||||||
* @typedef {Object} CreateOptions
|
* @typedef {Object} CreateOptions
|
||||||
* @property {PeerId} peerId
|
* @property {PeerId} peerId
|
||||||
*
|
*
|
||||||
* @extends {EventEmitter}
|
* @extends {Emittery}
|
||||||
* @fires Libp2p#error Emitted when an error occurs
|
* @fires Libp2p#error Emitted when an error occurs
|
||||||
* @fires Libp2p#peer:discovery Emitted when a peer is discovered
|
* @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`
|
* Like `new Libp2p(options)` except it will create a `PeerId`
|
||||||
* instance if one is not provided in options.
|
* instance if one is not provided in options.
|
||||||
@ -284,17 +284,19 @@ class Libp2p extends EventEmitter {
|
|||||||
*
|
*
|
||||||
* @param {string} eventName
|
* @param {string} eventName
|
||||||
* @param {...any} args
|
* @param {...any} args
|
||||||
* @returns {boolean}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
emit (eventName, ...args) {
|
emit (eventName, ...args) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
// TODO: do we still need this?
|
// TODO: do we still need this?
|
||||||
// @ts-ignore _events does not exist in libp2p
|
// @ts-ignore _events does not exist in libp2p
|
||||||
if (eventName === 'error' && !this._events.error) {
|
if (eventName === 'error' && !this._events.error) {
|
||||||
log.error(args)
|
log.error(args)
|
||||||
return false
|
resolve()
|
||||||
} else {
|
} else {
|
||||||
return super.emit(eventName, ...args)
|
super.emit(eventName, args).then(resolve)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const { EventEmitter } = require('events')
|
const Emittery = require('emittery')
|
||||||
const Big = require('bignumber.js')
|
const Big = require('bignumber.js')
|
||||||
const MovingAverage = require('moving-average')
|
const MovingAverage = require('moving-average')
|
||||||
const retimer = require('retimer')
|
const retimer = require('retimer')
|
||||||
|
|
||||||
class Stats extends EventEmitter {
|
class Stats extends Emittery {
|
||||||
/**
|
/**
|
||||||
* A queue based manager for stat processing
|
* A queue based manager for stat processing
|
||||||
*
|
*
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const errcode = require('err-code')
|
const errcode = require('err-code')
|
||||||
|
|
||||||
const { EventEmitter } = require('events')
|
const Emittery = require('emittery')
|
||||||
const PeerId = require('peer-id')
|
const PeerId = require('peer-id')
|
||||||
|
|
||||||
const AddressBook = require('./address-book')
|
const AddressBook = require('./address-book')
|
||||||
@ -19,7 +19,7 @@ const {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @extends {EventEmitter}
|
* @extends {Emittery}
|
||||||
*
|
*
|
||||||
* @fires PeerStore#peer Emitted when a new peer is added.
|
* @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:protocols Emitted when a known peer supports a different set of protocols.
|
||||||
@ -27,7 +27,7 @@ const {
|
|||||||
* @fires PeerStore#change:pubkey Emitted emitted when a peer's public key is known.
|
* @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.
|
* @fires PeerStore#change:metadata Emitted when the known metadata of a peer change.
|
||||||
*/
|
*/
|
||||||
class PeerStore extends EventEmitter {
|
class PeerStore extends Emittery {
|
||||||
/**
|
/**
|
||||||
* Peer object
|
* Peer object
|
||||||
*
|
*
|
||||||
|
@ -91,7 +91,7 @@ class PersistentPeerStore extends PeerStore {
|
|||||||
*/
|
*/
|
||||||
async stop () {
|
async stop () {
|
||||||
log('PeerStore is stopping')
|
log('PeerStore is stopping')
|
||||||
this.removeAllListeners()
|
this.clearListeners()
|
||||||
await this._commitData()
|
await this._commitData()
|
||||||
log('PeerStore stopped')
|
log('PeerStore stopped')
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user