Skip to content

Commit

Permalink
Ensure Subscriptions do not yield same values (#2196)
Browse files Browse the repository at this point in the history
* fix: only yield response when it differs from previous one

* chore: changeset file

* fix: add logic to ensure we only yield values if they differ from the previous ones
  • Loading branch information
nil-amrutlal-dept committed May 27, 2024
1 parent fb8227d commit 30217ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-falcons-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kadena/graph": patch
---

Added validations to ensure that we do not yield same events on subscriptions
14 changes: 12 additions & 2 deletions packages/apps/graph/src/graph/subscription/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ async function* iteratorFn(
context: IContext,
chainId?: string,
): AsyncGenerator<Transaction | undefined, void, unknown> {
let previousTransaction: Transaction | undefined;
let eventYielded: boolean = false;

while (!context.req.socket.destroyed) {
const transaction = await prismaClient.transaction.findFirst({
where: {
Expand All @@ -57,10 +60,17 @@ async function* iteratorFn(
chainId,
);

if (mempoolResponse) {
if (
mempoolResponse &&
JSON.stringify(mempoolResponse) !== JSON.stringify(previousTransaction)
) {
previousTransaction = { ...mempoolResponse };
yield mempoolResponse;
} else {
yield undefined;
if (!eventYielded) {
yield undefined;
eventYielded = true;
}
}
}

Expand Down

0 comments on commit 30217ff

Please sign in to comment.