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