fix: Refactors message decoding to abort as soon as a suitable decoder found (#1414)

* Refactors message decoding to abort as soon as a suitable decoder found. #1369

* fix: return from the function

* improve readability

---------

Co-authored-by: Danish Arora <35004822+danisharora099@users.noreply.github.com>
Co-authored-by: danisharora099 <danisharora099@gmail.com>
This commit is contained in:
balag3 2023-07-28 09:39:11 +02:00 committed by GitHub
parent 89392dbfdf
commit 30fcacea84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 17 deletions

View File

@ -384,23 +384,17 @@ async function pushMessage<T extends IDecodedMessage>(
return;
}
let didDecodeMsg = false;
// We don't want to wait for decoding failure, just attempt to decode
// all messages and do the call back on the one that works
// noinspection ES6MissingAwait
for (const dec of decoders) {
if (didDecodeMsg) break;
const decoded = await dec.fromProtoObj(
pubSubTopic,
message as IProtoMessage
try {
const decodePromises = decoders.map((dec) =>
dec
.fromProtoObj(pubSubTopic, message as IProtoMessage)
.then((decoded) => decoded || Promise.reject("Decoding failed"))
);
if (!decoded) {
log("Not able to decode message");
continue;
}
// This is just to prevent more decoding attempt
// TODO: Could be better if we were to abort promises
didDecodeMsg = Boolean(decoded);
await callback(decoded);
const decodedMessage = await Promise.any(decodePromises);
await callback(decodedMessage);
} catch (e) {
log("Error decoding message", e);
}
}