Skip to content

feat(bridge-controller): add opt-in yield after first streamed quote - #11465

Open
bfullam wants to merge 3 commits into
mainfrom
swaps/yield-after-first-quote-a985
Open

bfullam wants to merge 3 commits into
mainfrom
swaps/yield-after-first-quote-a985

Conversation

@bfullam

@bfullam bfullam commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explanation

On mid-tier Android, the first streamed quote reaches controller state well before it reaches the screen. The stream reader processes the rest of the buffered burst (parsing, validation, fees, and a state update for each quote, roughly 75–120ms each) back to back on the JS thread. The client's Redux flush and render only run once that work is done.

This PR adds an optional getShouldYieldAfterFirstQuote constructor option. When it returns true, the quote stream handler awaits one macrotask (setTimeout(0)) right after the first valid quote is written to state, so the client can flush and render it before the rest of the burst is processed. It's a getter, like getUseAssetsControllerForRates, so clients can A/B it behind a remote flag that's read at fetch time. When the option is omitted or returns false, behavior is unchanged.

Only the first valid quote of each stream triggers a yield, so it costs at most one task tick per fetch. Grouping later quote updates into fewer state writes is out of scope.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them
Open in Web Open in Cursor 

Note

Low Risk
Opt-in constructor flag with a single macrotask pause on the first quote only; default behavior is unchanged when the option is not used.

Reviewed by Cursor Bugbot for commit 61381c5. Bugbot is set up for automated code reviews on this repo. Configure here.

Co-authored-by: bryan.fullam <bryan.fullam@consensys.net>
@bfullam
bfullam requested review from a team as code owners November 13, 2026 08:15
Co-authored-by: bryan.fullam <bryan.fullam@consensys.net>
@github-actions

Copy link
Copy Markdown
Contributor

🛑 Files requiring codeowner review

To merge this PR, you'll need to get approvals from the following owners.

@MetaMask/swaps-engineers (3 files)
  • packages/bridge-controller/CHANGELOG.md
  • packages/bridge-controller/src/bridge-controller.sse.test.ts
  • packages/bridge-controller/src/bridge-controller.ts

ℹ️ Files co-owned by @MetaMask/core-platform (expand for more)

[!NOTE]
For convenience in maintaining this repo, @MetaMask/core-platform co-owns
the following files. It is not required to seek approval from
@MetaMask/core-platform to change these files. This section is purely
informational.

  • packages/bridge-controller/CHANGELOG.md

Co-authored-by: bryan.fullam <bryan.fullam@consensys.net>

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 61381c5. Configure here.

this.#getShouldYieldAfterFirstQuote?.()
) {
await new Promise((resolve) => setTimeout(resolve, 0));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale quotes written after yield

Medium Severity

After the first quote is written, the handler yields with setTimeout and then keeps processing the rest of the buffered burst with no abort check. A newer fetch that cancels this stream during that gap can still receive the remaining old quotes in state.quotes.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 61381c5. Configure here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants