mirror of
https://github.com/status-im/js-waku-examples.git
synced 2025-01-12 23:14:32 +00:00
add peer:disconnection handler and improve peer counter
This commit is contained in:
parent
ffb84a8fbb
commit
a0ccd50b6a
@ -3,6 +3,7 @@ import { generate } from "server-name-generator";
|
|||||||
import { Message } from "./Message";
|
import { Message } from "./Message";
|
||||||
import { EPeersByDiscoveryEvents, LightNode, Tags } from "@waku/interfaces";
|
import { EPeersByDiscoveryEvents, LightNode, Tags } from "@waku/interfaces";
|
||||||
import type { PeerId } from "@libp2p/interface-peer-id";
|
import type { PeerId } from "@libp2p/interface-peer-id";
|
||||||
|
import { waku } from "@waku/sdk";
|
||||||
|
|
||||||
import { useFilterMessages, useStoreMessages } from "@waku/react";
|
import { useFilterMessages, useStoreMessages } from "@waku/react";
|
||||||
import type {
|
import type {
|
||||||
@ -11,7 +12,6 @@ import type {
|
|||||||
UsePeersParams,
|
UsePeersParams,
|
||||||
UsePeersResults,
|
UsePeersResults,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
import { handleCatch } from "./utils";
|
|
||||||
|
|
||||||
export const usePersistentNick = (): [
|
export const usePersistentNick = (): [
|
||||||
string,
|
string,
|
||||||
@ -106,6 +106,13 @@ export const useNodePeers = (node: undefined | LightNode) => {
|
|||||||
new Set(DISCOVERED[Tags.PEER_EXCHANGE].map((p) => p.id))
|
new Set(DISCOVERED[Tags.PEER_EXCHANGE].map((p) => p.id))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
node.libp2p.addEventListener("peer:disconnect", (evt) => {
|
||||||
|
const peerId = evt.detail;
|
||||||
|
setConnectedBootstrapPeers((peers) => {
|
||||||
|
peers.delete(peerId);
|
||||||
|
return peers;
|
||||||
|
});
|
||||||
|
});
|
||||||
node.connectionManager.addEventListener(
|
node.connectionManager.addEventListener(
|
||||||
EPeersByDiscoveryEvents.PEER_DISCOVERY_BOOTSTRAP,
|
EPeersByDiscoveryEvents.PEER_DISCOVERY_BOOTSTRAP,
|
||||||
handleDiscoveryBootstrap
|
handleDiscoveryBootstrap
|
||||||
@ -172,18 +179,26 @@ export const usePeers = (params: UsePeersParams): UsePeersResults => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const listener = async () => {
|
const listener = async () => {
|
||||||
const peers = await Promise.all([
|
// find all the peers that are connected for diff protocols
|
||||||
node?.libp2p.getPeers(),
|
const peerIds = node.libp2p.getPeers();
|
||||||
handleCatch(node?.store?.peers()),
|
const peers = await Promise.all(
|
||||||
handleCatch(node?.filter?.peers()),
|
peerIds.map((id) => node.libp2p.peerStore.get(id))
|
||||||
handleCatch(node?.lightPush?.peers()),
|
);
|
||||||
]);
|
|
||||||
|
|
||||||
setPeers({
|
setPeers({
|
||||||
allConnected: peers[0],
|
allConnected: peers.map((p) => p.id),
|
||||||
storePeers: peers[1]?.map((p) => p.id),
|
storePeers: peers
|
||||||
filterPeers: peers[2]?.map((p) => p.id),
|
.filter((p) => p.protocols.includes(waku.StoreCodec))
|
||||||
lightPushPeers: peers[3]?.map((p) => p.id),
|
.map((p) => p.id),
|
||||||
|
//TODO: use from import
|
||||||
|
filterPeers: peers
|
||||||
|
.filter((p) =>
|
||||||
|
p.protocols.includes("/vac/waku/filter-subscribe/2.0.0-beta1")
|
||||||
|
)
|
||||||
|
.map((p) => p.id), // hardcoding codec since we don't export it currently
|
||||||
|
lightPushPeers: peers
|
||||||
|
.filter((p) => p.protocols.includes(waku.LightPushCodec))
|
||||||
|
.map((p) => p.id),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user