diff --git a/src/lib/wait_for_remote_peer.ts b/src/lib/wait_for_remote_peer.ts index 1c040c1902..9ccee6d542 100644 --- a/src/lib/wait_for_remote_peer.ts +++ b/src/lib/wait_for_remote_peer.ts @@ -1,5 +1,6 @@ -import { PeerProtocolsChangeData } from "@libp2p/interface-peer-store"; +import { Peer, PeerProtocolsChangeData } from "@libp2p/interface-peer-store"; import debug from "debug"; +import type { Libp2p } from "libp2p"; import { StoreCodecs } from "./constants"; import { Protocols, Waku } from "./waku"; @@ -8,6 +9,11 @@ import { LightPushCodec } from "./waku_light_push"; const log = debug("waku:wait-for-remote-peer"); +interface WakuProtocol { + libp2p: Libp2p; + peers: () => Promise; +} + /** * Wait for a remote peer to be ready given the passed protocols. * Useful when using the [[CreateOptions.bootstrap]] with [[Waku.create]]. @@ -76,49 +82,11 @@ export async function waitForRemotePeer( } if (protocols.includes(Protocols.LightPush)) { - const lightPushPromise = (async (): Promise => { - const peers = await waku.lightPush.peers(); - - if (peers.length) { - log("Light Push peer found: ", peers[0].id.toString()); - return; - } - - await new Promise((resolve) => { - const cb = (evt: CustomEvent): void => { - if (evt.detail.protocols.includes(LightPushCodec)) { - log("Resolving for", LightPushCodec, evt.detail.protocols); - waku.libp2p.peerStore.removeEventListener("change:protocols", cb); - resolve(); - } - }; - waku.libp2p.peerStore.addEventListener("change:protocols", cb); - }); - })(); - promises.push(lightPushPromise); + promises.push(waitForConnectedPeer(waku.lightPush, LightPushCodec)); } if (protocols.includes(Protocols.Filter)) { - const filterPromise = (async (): Promise => { - const peers = await waku.filter.peers(); - - if (peers.length) { - log("Filter peer found: ", peers[0].id.toString()); - return; - } - - await new Promise((resolve) => { - const cb = (evt: CustomEvent): void => { - if (evt.detail.protocols.includes(FilterCodec)) { - log("Resolving for", FilterCodec, evt.detail.protocols); - waku.libp2p.peerStore.removeEventListener("change:protocols", cb); - resolve(); - } - }; - waku.libp2p.peerStore.addEventListener("change:protocols", cb); - }); - })(); - promises.push(filterPromise); + promises.push(waitForConnectedPeer(waku.lightPush, FilterCodec)); } if (timeoutMs) { @@ -132,6 +100,32 @@ export async function waitForRemotePeer( } } +/** + * Wait for a peer with the given protocol to be connected. + */ +async function waitForConnectedPeer( + waku: WakuProtocol, + codec: string +): Promise { + const peers = await waku.peers(); + + if (peers.length) { + log(`${codec} peer found: `, peers[0].id.toString()); + return; + } + + await new Promise((resolve) => { + const cb = (evt: CustomEvent): void => { + if (evt.detail.protocols.includes(codec)) { + log("Resolving for", codec, evt.detail.protocols); + waku.libp2p.peerStore.removeEventListener("change:protocols", cb); + resolve(); + } + }; + waku.libp2p.peerStore.addEventListener("change:protocols", cb); + }); +} + const awaitTimeout = (ms: number, rejectReason: string): Promise => new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms));