improve readability

This commit is contained in:
danisharora099 2023-09-15 17:02:31 +05:30
parent 1bac4bd6ab
commit 59be53225e
No known key found for this signature in database
GPG Key ID: FBD2BF500037F135
2 changed files with 16 additions and 9 deletions

View File

@ -12,6 +12,7 @@ import type {
UsePeersResults,
} from "./types";
import { OrderedSet } from "./ordered_array";
import { getPeerIdsForProtocol } from "./utils";
export const usePersistentNick = (): [
string,
@ -194,15 +195,9 @@ export const usePeers = (params: UsePeersParams): UsePeersResults => {
setPeers({
allConnected: peers.map((p) => p.id),
storePeers: peers
.filter((p) => p.protocols.includes(node.store?.multicodec || ""))
.map((p) => p.id),
filterPeers: peers
.filter((p) => p.protocols.includes(node.filter?.multicodec || ""))
.map((p) => p.id), // hardcoding codec since we don't export it currently
lightPushPeers: peers
.filter((p) => p.protocols.includes(node.lightPush?.multicodec || ""))
.map((p) => p.id),
storePeers: getPeerIdsForProtocol(node.store, peers),
filterPeers: getPeerIdsForProtocol(node.filter, peers),
lightPushPeers: getPeerIdsForProtocol(node.lightPush, peers),
});
};

View File

@ -1,4 +1,5 @@
import type { Peer } from "@libp2p/interface-peer-store";
import { IFilter, ILightPush, IStore } from "@waku/interfaces";
export async function handleCatch(
promise?: Promise<Peer[]>
@ -13,3 +14,14 @@ export async function handleCatch(
return undefined;
}
}
export function getPeerIdsForProtocol(
protocol: IStore | ILightPush | IFilter | undefined,
peers: Peer[]
) {
return protocol
? peers
.filter((p) => p.protocols.includes(protocol.multicodec))
.map((p) => p.id)
: [];
}