Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Jul 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions packages/core/src/lib/filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}