mirror of https://github.com/waku-org/js-waku.git
refact: getPeersForProtocol only needs the peer store
This commit is contained in:
parent
0c83953e55
commit
3b05bfe988
|
@ -1,5 +1,4 @@
|
|||
import { Peer } from "@libp2p/interface-peer-store";
|
||||
import { Libp2p } from "libp2p";
|
||||
import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
|
||||
|
||||
/**
|
||||
* Returns a pseudo-random peer that supports the given protocol.
|
||||
|
@ -18,11 +17,11 @@ export async function selectRandomPeer(
|
|||
* Returns the list of peers that supports the given protocol.
|
||||
*/
|
||||
export async function getPeersForProtocol(
|
||||
libp2p: Libp2p,
|
||||
peerStore: PeerStore,
|
||||
protocols: string[]
|
||||
): Promise<Peer[]> {
|
||||
const peers: Peer[] = [];
|
||||
await libp2p.peerStore.forEach((peer) => {
|
||||
await peerStore.forEach((peer) => {
|
||||
for (let i = 0; i < protocols.length; i++) {
|
||||
if (peer.protocols.includes(protocols[i])) {
|
||||
peers.push(peer);
|
||||
|
|
|
@ -272,7 +272,7 @@ export class WakuFilter {
|
|||
}
|
||||
|
||||
async peers(): Promise<Peer[]> {
|
||||
return getPeersForProtocol(this.libp2p, [FilterCodec]);
|
||||
return getPeersForProtocol(this.libp2p.peerStore, [FilterCodec]);
|
||||
}
|
||||
|
||||
async randomPeer(): Promise<Peer | undefined> {
|
||||
|
|
|
@ -109,7 +109,7 @@ export class WakuLightPush {
|
|||
* peers.
|
||||
*/
|
||||
async peers(): Promise<Peer[]> {
|
||||
return getPeersForProtocol(this.libp2p, [LightPushCodec]);
|
||||
return getPeersForProtocol(this.libp2p.peerStore, [LightPushCodec]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -312,7 +312,7 @@ export class WakuStore {
|
|||
codecs.push(codec);
|
||||
}
|
||||
|
||||
return getPeersForProtocol(this.libp2p, codecs);
|
||||
return getPeersForProtocol(this.libp2p.peerStore, codecs);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue