mirror of https://github.com/status-im/js-waku.git
improve readability of result processing in light push
This commit is contained in:
parent
6bcf283686
commit
9742d6887d
|
@ -58,12 +58,12 @@ class LightPush extends BaseProtocolSDK implements ILightPush {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error("Failed to send waku light push: pubsub topic not configured");
|
log.error("Failed to send waku light push: pubsub topic not configured");
|
||||||
return {
|
return {
|
||||||
|
successes,
|
||||||
failures: [
|
failures: [
|
||||||
{
|
{
|
||||||
error: ProtocolError.TOPIC_NOT_CONFIGURED
|
error: ProtocolError.TOPIC_NOT_CONFIGURED
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
successes: []
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,40 +79,37 @@ class LightPush extends BaseProtocolSDK implements ILightPush {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const sendPromises = peers.map((peer) =>
|
const results = await Promise.allSettled(
|
||||||
this.protocol.send(encoder, message, peer)
|
peers.map((peer) => this.protocol.send(encoder, message, peer))
|
||||||
);
|
);
|
||||||
|
|
||||||
const results = await Promise.allSettled(sendPromises);
|
|
||||||
|
|
||||||
for (const result of results) {
|
for (const result of results) {
|
||||||
if (result.status === "fulfilled") {
|
if (result.status !== "fulfilled") {
|
||||||
const { failure, success } = result.value;
|
|
||||||
if (success) {
|
|
||||||
successes.push(success);
|
|
||||||
}
|
|
||||||
if (failure) {
|
|
||||||
failures.push(failure);
|
|
||||||
if (failure.peerId) {
|
|
||||||
const peer = this.connectedPeers.find((connectedPeer) =>
|
|
||||||
connectedPeer.id.equals(failure.peerId)
|
|
||||||
);
|
|
||||||
if (peer) {
|
|
||||||
log.info(`
|
|
||||||
Failed to send message to peer ${failure.peerId}.
|
|
||||||
Retrying the message with the same peer in the background.
|
|
||||||
If this fails, the peer will be renewed.
|
|
||||||
`);
|
|
||||||
void this.reliabilityMonitor.attemptRetriesOrRenew(
|
|
||||||
failure.peerId,
|
|
||||||
() => this.protocol.send(encoder, message, peer)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.error("Failed unexpectedly while sending:", result.reason);
|
log.error("Failed unexpectedly while sending:", result.reason);
|
||||||
failures.push({ error: ProtocolError.GENERIC_FAIL });
|
failures.push({ error: ProtocolError.GENERIC_FAIL });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { failure, success } = result.value;
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
successes.push(success);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (failure) {
|
||||||
|
failures.push(failure);
|
||||||
|
|
||||||
|
const connectedPeer = this.connectedPeers.find((connectedPeer) =>
|
||||||
|
connectedPeer.id.equals(failure.peerId)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (connectedPeer) {
|
||||||
|
void this.reliabilityMonitor.attemptRetriesOrRenew(
|
||||||
|
connectedPeer.id,
|
||||||
|
() => this.protocol.send(encoder, message, connectedPeer)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue