mirror of https://github.com/waku-org/js-waku.git
refactor: Use same code for store, lightpush and filter
This commit is contained in:
parent
0a505659b7
commit
2265a0099f
|
@ -53,40 +53,16 @@ export async function waitForRemotePeer(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: This can be factored in one helper function
|
|
||||||
// Probably need to add a "string" protocol to each class to make it easier
|
|
||||||
if (protocols.includes(Protocols.Store)) {
|
if (protocols.includes(Protocols.Store)) {
|
||||||
const storePromise = (async (): Promise<void> => {
|
promises.push(waitForConnectedPeer(waku.store, Object.values(StoreCodecs)));
|
||||||
const peers = await waku.store.peers();
|
|
||||||
|
|
||||||
if (peers.length) {
|
|
||||||
log("Store peer found: ", peers[0].id.toString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await new Promise<void>((resolve) => {
|
|
||||||
const cb = (evt: CustomEvent<PeerProtocolsChangeData>): void => {
|
|
||||||
for (const codec of Object.values(StoreCodecs)) {
|
|
||||||
if (evt.detail.protocols.includes(codec)) {
|
|
||||||
log("Resolving for", StoreCodecs, evt.detail.protocols);
|
|
||||||
waku.libp2p.peerStore.removeEventListener("change:protocols", cb);
|
|
||||||
resolve();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
waku.libp2p.peerStore.addEventListener("change:protocols", cb);
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
promises.push(storePromise);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (protocols.includes(Protocols.LightPush)) {
|
if (protocols.includes(Protocols.LightPush)) {
|
||||||
promises.push(waitForConnectedPeer(waku.lightPush, LightPushCodec));
|
promises.push(waitForConnectedPeer(waku.lightPush, [LightPushCodec]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (protocols.includes(Protocols.Filter)) {
|
if (protocols.includes(Protocols.Filter)) {
|
||||||
promises.push(waitForConnectedPeer(waku.lightPush, FilterCodec));
|
promises.push(waitForConnectedPeer(waku.filter, [FilterCodec]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeoutMs) {
|
if (timeoutMs) {
|
||||||
|
@ -105,21 +81,24 @@ export async function waitForRemotePeer(
|
||||||
*/
|
*/
|
||||||
async function waitForConnectedPeer(
|
async function waitForConnectedPeer(
|
||||||
waku: WakuProtocol,
|
waku: WakuProtocol,
|
||||||
codec: string
|
codecs: string[]
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const peers = await waku.peers();
|
const peers = await waku.peers();
|
||||||
|
|
||||||
if (peers.length) {
|
if (peers.length) {
|
||||||
log(`${codec} peer found: `, peers[0].id.toString());
|
log(`${codecs} peer found: `, peers[0].id.toString());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await new Promise<void>((resolve) => {
|
await new Promise<void>((resolve) => {
|
||||||
const cb = (evt: CustomEvent<PeerProtocolsChangeData>): void => {
|
const cb = (evt: CustomEvent<PeerProtocolsChangeData>): void => {
|
||||||
|
for (const codec of codecs) {
|
||||||
if (evt.detail.protocols.includes(codec)) {
|
if (evt.detail.protocols.includes(codec)) {
|
||||||
log("Resolving for", codec, evt.detail.protocols);
|
log("Resolving for", codec, evt.detail.protocols);
|
||||||
waku.libp2p.peerStore.removeEventListener("change:protocols", cb);
|
waku.libp2p.peerStore.removeEventListener("change:protocols", cb);
|
||||||
resolve();
|
resolve();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
waku.libp2p.peerStore.addEventListener("change:protocols", cb);
|
waku.libp2p.peerStore.addEventListener("change:protocols", cb);
|
||||||
|
|
Loading…
Reference in New Issue