refactor: Use same code for store, lightpush and filter

This commit is contained in:
Franck Royer 2022-07-25 12:51:41 +10:00 committed by fryorcraken.eth
parent 0a505659b7
commit 2265a0099f
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 12 additions and 33 deletions

View File

@ -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);