mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-17 14:57:19 +00:00
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 type { Peer, PeerStore } from "@libp2p/interface-peer-store";
|
||||||
import { Libp2p } from "libp2p";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a pseudo-random peer that supports the given protocol.
|
* 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.
|
* Returns the list of peers that supports the given protocol.
|
||||||
*/
|
*/
|
||||||
export async function getPeersForProtocol(
|
export async function getPeersForProtocol(
|
||||||
libp2p: Libp2p,
|
peerStore: PeerStore,
|
||||||
protocols: string[]
|
protocols: string[]
|
||||||
): Promise<Peer[]> {
|
): Promise<Peer[]> {
|
||||||
const peers: Peer[] = [];
|
const peers: Peer[] = [];
|
||||||
await libp2p.peerStore.forEach((peer) => {
|
await peerStore.forEach((peer) => {
|
||||||
for (let i = 0; i < protocols.length; i++) {
|
for (let i = 0; i < protocols.length; i++) {
|
||||||
if (peer.protocols.includes(protocols[i])) {
|
if (peer.protocols.includes(protocols[i])) {
|
||||||
peers.push(peer);
|
peers.push(peer);
|
||||||
|
@ -272,7 +272,7 @@ export class WakuFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async peers(): Promise<Peer[]> {
|
async peers(): Promise<Peer[]> {
|
||||||
return getPeersForProtocol(this.libp2p, [FilterCodec]);
|
return getPeersForProtocol(this.libp2p.peerStore, [FilterCodec]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async randomPeer(): Promise<Peer | undefined> {
|
async randomPeer(): Promise<Peer | undefined> {
|
||||||
|
@ -109,7 +109,7 @@ export class WakuLightPush {
|
|||||||
* peers.
|
* peers.
|
||||||
*/
|
*/
|
||||||
async peers(): Promise<Peer[]> {
|
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);
|
codecs.push(codec);
|
||||||
}
|
}
|
||||||
|
|
||||||
return getPeersForProtocol(this.libp2p, codecs);
|
return getPeersForProtocol(this.libp2p.peerStore, codecs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user