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
Show file tree
Hide file tree
Changes from 2 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
37 changes: 17 additions & 20 deletions packages/core/src/lib/filter/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,23 @@ class Filter extends BaseProtocol implements IFilter {
log("Message has no content topic, skipping");
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
decoders.forEach(async (dec: IDecoder<T>) => {
if (didDecodeMsg) return;
const decoded = await dec.fromProtoObj(
pubSubTopic,
toProtoMessage(protoMessage)
);
if (!decoded) {
log("Not able to decode message");
return;
}
// This is just to prevent more decoding attempt
// TODO: Could be better if we were to abort promises
didDecodeMsg = Boolean(decoded);
await callback(decoded);
});
Promise.any(
decoders.map(async (dec) => {
dec
.fromProtoObj(pubSubTopic, toProtoMessage(protoMessage))
.then((decoded) =>
decoded
? Promise.resolve(decoded)
: Promise.reject(new Error("Decoding failed"))
);
})
)
.then(async (decodedMessage) => {
await callback(decodedMessage);
})
.catch((e) => {
log("Error decoding message", e);
});
balag3 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
38 changes: 17 additions & 21 deletions packages/core/src/lib/filter/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,25 +389,21 @@ async function pushMessage<T extends IDecodedMessage>(
log("Message has no content topic, skipping");
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
decoders.forEach(async (dec: IDecoder<T>) => {
if (didDecodeMsg) return;
const decoded = await dec.fromProtoObj(
pubSubTopic,
message as IProtoMessage
);
// const decoded = await dec.fromProtoObj(pubSubTopic, message);
if (!decoded) {
log("Not able to decode message");
return;
}
// This is just to prevent more decoding attempt
// TODO: Could be better if we were to abort promises
didDecodeMsg = Boolean(decoded);
await callback(decoded);
});
Promise.any(
decoders.map(async (dec) => {
dec
.fromProtoObj(pubSubTopic, message as IProtoMessage)
.then((decoded) =>
decoded
? Promise.resolve(decoded)
: Promise.reject(new Error("Decoding failed"))
);
})
)
.then(async (decodedMessage) => {
await callback(decodedMessage);
})
.catch((e) => {
log("Error decoding message", e);
});
}
Loading