mirror of https://github.com/waku-org/js-waku.git
refactor: Use common code for filter and light push
This commit is contained in:
parent
811685e041
commit
4c3da7bad2
|
@ -1,5 +1,6 @@
|
||||||
import { PeerProtocolsChangeData } from "@libp2p/interface-peer-store";
|
import { Peer, PeerProtocolsChangeData } from "@libp2p/interface-peer-store";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
|
import type { Libp2p } from "libp2p";
|
||||||
|
|
||||||
import { StoreCodecs } from "./constants";
|
import { StoreCodecs } from "./constants";
|
||||||
import { Protocols, Waku } from "./waku";
|
import { Protocols, Waku } from "./waku";
|
||||||
|
@ -8,6 +9,11 @@ import { LightPushCodec } from "./waku_light_push";
|
||||||
|
|
||||||
const log = debug("waku:wait-for-remote-peer");
|
const log = debug("waku:wait-for-remote-peer");
|
||||||
|
|
||||||
|
interface WakuProtocol {
|
||||||
|
libp2p: Libp2p;
|
||||||
|
peers: () => Promise<Peer[]>;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wait for a remote peer to be ready given the passed protocols.
|
* Wait for a remote peer to be ready given the passed protocols.
|
||||||
* Useful when using the [[CreateOptions.bootstrap]] with [[Waku.create]].
|
* Useful when using the [[CreateOptions.bootstrap]] with [[Waku.create]].
|
||||||
|
@ -76,49 +82,11 @@ export async function waitForRemotePeer(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (protocols.includes(Protocols.LightPush)) {
|
if (protocols.includes(Protocols.LightPush)) {
|
||||||
const lightPushPromise = (async (): Promise<void> => {
|
promises.push(waitForConnectedPeer(waku.lightPush, LightPushCodec));
|
||||||
const peers = await waku.lightPush.peers();
|
|
||||||
|
|
||||||
if (peers.length) {
|
|
||||||
log("Light Push peer found: ", peers[0].id.toString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await new Promise<void>((resolve) => {
|
|
||||||
const cb = (evt: CustomEvent<PeerProtocolsChangeData>): 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (protocols.includes(Protocols.Filter)) {
|
if (protocols.includes(Protocols.Filter)) {
|
||||||
const filterPromise = (async (): Promise<void> => {
|
promises.push(waitForConnectedPeer(waku.lightPush, FilterCodec));
|
||||||
const peers = await waku.filter.peers();
|
|
||||||
|
|
||||||
if (peers.length) {
|
|
||||||
log("Filter peer found: ", peers[0].id.toString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await new Promise<void>((resolve) => {
|
|
||||||
const cb = (evt: CustomEvent<PeerProtocolsChangeData>): 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeoutMs) {
|
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<void> {
|
||||||
|
const peers = await waku.peers();
|
||||||
|
|
||||||
|
if (peers.length) {
|
||||||
|
log(`${codec} peer found: `, peers[0].id.toString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await new Promise<void>((resolve) => {
|
||||||
|
const cb = (evt: CustomEvent<PeerProtocolsChangeData>): 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<void> =>
|
const awaitTimeout = (ms: number, rejectReason: string): Promise<void> =>
|
||||||
new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms));
|
new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue