Skip to content

Commit

Permalink
Merge pull request #269 from waku-org/chore/use-base-multicodec
Browse files Browse the repository at this point in the history
chore: use `BaseProtocol.multicodec` instead of exported codec
  • Loading branch information
danisharora099 committed Sep 20, 2023
2 parents 14034a0 + 0b4d5c6 commit 7c24ffa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
17 changes: 4 additions & 13 deletions examples/web-chat/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { generate } from "server-name-generator";
import { Message } from "./Message";
import { EPeersByDiscoveryEvents, LightNode, Tags } from "@waku/interfaces";
import type { PeerId } from "@libp2p/interface-peer-id";
import { waku } from "@waku/sdk";

import { useFilterMessages, useStoreMessages } from "@waku/react";
import type {
Expand All @@ -13,6 +12,7 @@ import type {
UsePeersResults,
} from "./types";
import { OrderedSet } from "./ordered_array";
import { getPeerIdsForProtocol } from "./utils";

export const usePersistentNick = (): [
string,
Expand Down Expand Up @@ -195,18 +195,9 @@ export const usePeers = (params: UsePeersParams): UsePeersResults => {

setPeers({
allConnected: peers.map((p) => p.id),
storePeers: peers
.filter((p) => p.protocols.includes(waku.waku_store.StoreCodec))
.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.waku_store.StoreCodec))
.map((p) => p.id),
storePeers: getPeerIdsForProtocol(node.store, peers),
filterPeers: getPeerIdsForProtocol(node.filter, peers),
lightPushPeers: getPeerIdsForProtocol(node.lightPush, peers),
});
};

Expand Down
12 changes: 12 additions & 0 deletions examples/web-chat/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Peer } from "@libp2p/interface-peer-store";
import type { IFilter, ILightPush, IStore } from "@waku/interfaces";

export async function handleCatch(
promise?: Promise<Peer[]>
Expand All @@ -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)
: [];
}

0 comments on commit 7c24ffa

Please sign in to comment.