mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-05-01 09:12:33 +00:00
* feat: topology * feat: multicodec-topology * chore: address review Co-Authored-By: Jacob Heun <jacobheun@gmail.com> * chore: remove error from disconnect * docs: topology * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
45 lines
996 B
JavaScript
45 lines
996 B
JavaScript
'use strict'
|
|
|
|
const noop = () => {}
|
|
|
|
class Topology {
|
|
/**
|
|
* @param {Object} props
|
|
* @param {number} props.min minimum needed connections (default: 0)
|
|
* @param {number} props.max maximum needed connections (default: Infinity)
|
|
* @param {Object} [props.handlers]
|
|
* @param {function} [props.handlers.onConnect] protocol "onConnect" handler
|
|
* @param {function} [props.handlers.onDisconnect] protocol "onDisconnect" handler
|
|
* @constructor
|
|
*/
|
|
constructor ({
|
|
min = 0,
|
|
max = Infinity,
|
|
handlers = {}
|
|
}) {
|
|
this.min = min
|
|
this.max = max
|
|
|
|
// Handlers
|
|
this._onConnect = handlers.onConnect || noop
|
|
this._onDisconnect = handlers.onDisconnect || noop
|
|
|
|
this.peers = new Map()
|
|
}
|
|
|
|
set registrar (registrar) {
|
|
this._registrar = registrar
|
|
}
|
|
|
|
/**
|
|
* Notify about peer disconnected event.
|
|
* @param {PeerInfo} peerInfo
|
|
* @returns {void}
|
|
*/
|
|
disconnect (peerInfo) {
|
|
this._onDisconnect(peerInfo)
|
|
}
|
|
}
|
|
|
|
module.exports = Topology
|