mirror of https://github.com/waku-org/js-waku.git
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:
parent
89392dbfdf
commit
30fcacea84
|
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue