Skip to content

Commit 30fcace

Browse files
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>
1 parent 89392db commit 30fcace

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

packages/core/src/lib/filter/index.ts

+11-17
Original file line numberDiff line numberDiff line change
@@ -384,23 +384,17 @@ async function pushMessage<T extends IDecodedMessage>(
384384
return;
385385
}
386386

387-
let didDecodeMsg = false;
388-
// We don't want to wait for decoding failure, just attempt to decode
389-
// all messages and do the call back on the one that works
390-
// noinspection ES6MissingAwait
391-
for (const dec of decoders) {
392-
if (didDecodeMsg) break;
393-
const decoded = await dec.fromProtoObj(
394-
pubSubTopic,
395-
message as IProtoMessage
387+
try {
388+
const decodePromises = decoders.map((dec) =>
389+
dec
390+
.fromProtoObj(pubSubTopic, message as IProtoMessage)
391+
.then((decoded) => decoded || Promise.reject("Decoding failed"))
396392
);
397-
if (!decoded) {
398-
log("Not able to decode message");
399-
continue;
400-
}
401-
// This is just to prevent more decoding attempt
402-
// TODO: Could be better if we were to abort promises
403-
didDecodeMsg = Boolean(decoded);
404-
await callback(decoded);
393+
394+
const decodedMessage = await Promise.any(decodePromises);
395+
396+
await callback(decodedMessage);
397+
} catch (e) {
398+
log("Error decoding message", e);
405399
}
406400
}

0 commit comments

Comments
 (0)