Refactor swap debounce: separate user input from programmatic actions#1697
Refactor swap debounce: separate user input from programmatic actions#1697gemcoder21 merged 2 commits intomainfrom
Conversation
- Add SwapFetchTrigger with DebouncableTrigger protocol - User keyboard input triggers debounced fetch (isImmediate: false) - Button actions (percent, asset change, retry, min amount) trigger immediate fetch - Timer calls fetch() directly instead of setting trigger - Add fetchTriggerIsImmediate test covering all 5 trigger cases
Summary of ChangesHello @DRadmir, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the swap quote fetching mechanism to provide more precise control over when quote requests are made. By introducing a dedicated Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively refactors the swap quote fetching logic by separating user input-driven debounced fetches from immediate programmatic fetches. The introduction of SwapFetchTrigger and the simplification of the debouncedTask modifier are excellent improvements that enhance clarity and maintainability. The removal of SwapFetchState further streamlines the state management.
I've identified a potential concurrency issue related to the timer-based refresh and a minor bug in one of the new tests. My detailed comments provide suggestions to address these points.
| func fetch() async { | ||
| guard let currentInput else { return } | ||
| await performFetch(input: currentInput) | ||
| } |
There was a problem hiding this comment.
The onTimer in SwapScene calls this fetch() method directly. Other user actions use setFetchTrigger, which allows the .debouncedTask modifier to manage and cancel tasks. This direct call can lead to concurrent fetches (one from the timer, one from a user action), potentially causing race conditions or unnecessary network requests.
To ensure all fetches are serialized and cancellable, consider aligning the timer's behavior with the trigger-based mechanism. A possible approach is to introduce a public method like refreshQuotes() that calls setFetchTrigger(isImmediate: true), and have the timer call this new method. This would centralize fetch management within the .debouncedTask.
| #expect(model.fetchTrigger?.isImmediate == true) | ||
|
|
||
| model.fetchTrigger = nil | ||
| model.swapState.quotes = .error(SwapperError.NoQuoteAvailable) |
There was a problem hiding this comment.
The error SwapperError.NoQuoteAvailable does not seem to conform to RetryableError. This will cause buttonViewModel.buttonAction to default to .swap instead of .retryQuotes, and the assertion on line 120 will likely fail.
To correctly test the retry logic, you should use an error that conforms to RetryableError, like the local TestError struct.
| model.swapState.quotes = .error(SwapperError.NoQuoteAvailable) | |
| model.swapState.quotes = .error(TestError()) |
Replace the multiple Duration.Debounce constants with a single Duration.debounce and update related APIs and call sites. Default interval parameters for debounce and debouncedTask modifiers now use .debounce; callers updated to use .debounce (or .none where appropriate). Adjusted view models and views (SelectAssetScene, AddNodeSceneViewModel, SwapScene, ConfirmTransferScene, WalletSearchScene, NameRecordView) to the new API and removed the old Debounce struct.
Close: #1696