This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use iterables instead of Promises where possible
- Loading branch information
Showing
22 changed files
with
987 additions
and
687 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
|
||
const PeerSet = require('./peer-set') | ||
|
||
/** | ||
* Like PeerSet but with a size restriction. | ||
*/ | ||
class LimitedPeerSet extends PeerSet { | ||
/** | ||
* Create a new limited peer set. | ||
* | ||
* @param {number} limit | ||
*/ | ||
constructor (limit) { | ||
super() | ||
this.limit = limit | ||
} | ||
|
||
/** | ||
* Add a PeerInfo if it fits in the set | ||
* | ||
* @param {PeerInfo} info | ||
* @returns {bool} | ||
*/ | ||
add (info) { | ||
if (this.size < this.limit) { | ||
return super.add(info) | ||
} | ||
return false | ||
} | ||
} | ||
|
||
module.exports = LimitedPeerSet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict' | ||
|
||
/** | ||
* A set of unique peer ids. | ||
*/ | ||
class PeerIdSet { | ||
constructor () { | ||
this.peers = new Map() | ||
} | ||
|
||
/** | ||
* Add a new id. Returns `true` if it was a new one | ||
* | ||
* @param {PeerId} id | ||
* @returns {bool} | ||
*/ | ||
add (id) { | ||
if (!id) { | ||
return false | ||
} | ||
if (!this.has(id)) { | ||
this.peers.set(id.toB58String(), id) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
/** | ||
* Check if this PeerId is already in here. | ||
* | ||
* @param {PeerId} id | ||
* @returns {bool} | ||
*/ | ||
has (id) { | ||
return this.peers.has(id && id.toB58String()) | ||
} | ||
|
||
/** | ||
* Get the set as an array. | ||
* | ||
* @returns {Array<PeerId>} | ||
*/ | ||
toArray () { | ||
return [...this.peers.values()] | ||
} | ||
|
||
/** | ||
* The size of the set | ||
* | ||
* @type {number} | ||
*/ | ||
get size () { | ||
return this.peers.size | ||
} | ||
} | ||
|
||
module.exports = PeerIdSet |
Oops, something went wrong.