-
-
Notifications
You must be signed in to change notification settings - Fork 275
refactor(ramps): streamline RampsController reset and abort logic #7818
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
base: main
Are you sure you want to change the base?
Conversation
|
cursor review |
…EYS, abort on reset, requireRegion/isRegionCurrent and mutateRequests helpers
There was a problem hiding this comment.
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 and found 1 potential issue.
| * 2. **Cache hit** – If valid, non-expired data exists in state.requests for | ||
| * this key and forceRefresh is not set, returns that data without fetching. | ||
| * | ||
| * 3. **New request** – If options.resourceType is set, aborts any other |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSDoc says executeRequest aborts other in-flight requests for the same resourceType, but I don't see that happening in the implementation. It only stores resourceType on the pending request so #abortDependentRequests can find it later. Should the doc say something more like "stores resourceType for later abort when region changes" or did you mean to add logic here to abort same-resource requests when a new one starts?
| }); | ||
| }); | ||
|
|
||
| it('does not throw when updating resource field and resource is null (defensive)', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test name says "does not throw" but the assertion expects it to throw with "Cannot set property of null". Should the name be something like "throws when state.countries is null" instead?
| } | ||
| } | ||
|
|
||
| #abortDependentRequests(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this aborts requests, it doesn't decrement #pendingResourceCount or set loading to false. I see the finally block skips cleanup when aborted is true, and resetDependentResources resets everything after, so loading won't get stuck. Just wanted to confirm that's the intended flow and we're not relying on the aborted request's finally block to clean up loading state?
Explanation
This is refactor that centralizes four areas of the RampsController: reset logic, abort handling, region validation, and "is result current" checks. The changes reduce a significant amount of duplication.
Changes
Reset flow
DEPENDENT_RESOURCE_KEYSandresetResource()so “reset one resource” is defined once and driven bygetDefaultRampsControllerState().resetDependentResources()to callgetDefaultRampsControllerState()once and loop over the keys instead of manual assignments.resetResource(state, 'paymentMethods')insetSelectedProviderandsetSelectedTokenso payment methods are fully reset (includingisLoadinganderror), fixing stale loading/error state when changing or clearing provider/token.Typing
Draft<RampsControllerState>so they can be used insideupdate()without casts and avoid TS2589.state.requestscast in a single#mutateRequests()helper;#removeRequestStateand#updateRequestStateuse it.Abort handling
#abortDependentRequests()to abort in-flight requests for dependent resources (providers, tokens, paymentMethods, quotes) and clear their request state.#cleanupState()and fromsetUserRegion()when the region changes, before clearing pending counts and resetting state, so “reset” actually cancels dependent work.DEPENDENT_RESOURCE_KEYS(and aDEPENDENT_RESOURCE_KEYS_SETfor lookup) in#clearPendingResourceCountForDependentResources()so the dependent-resource list is defined in one place.Validation and region checks
#requireRegion(): stringand use it insetSelectedProvider,setSelectedToken, andstartQuotePollingwith a single region-required error message.#isRegionCurrent(normalizedRegion: string): booleanand use it in allisResultCurrentcallbacks forgetTokens,getProviders,getPaymentMethods, andgetQuotes.Result
Single source of truth for default/reset state and for “dependent resources,” less duplicated code, correct clearing of
paymentMethods(andquotes.selected) on region or selection changes, real abort of in-flight dependent requests on reset, and no Draft casts at call sites.References
Checklist
Note
Medium Risk
Touches request aborting/caching and resource reset logic that can affect loading/error state and stale data suppression across providers/tokens/payment methods/quotes. Changes are well-covered by updated unit tests but could alter timing/behavior in edge cases around in-flight requests.
Overview
Refactors
RampsControllerto centralize reset behavior for region-dependent resources viaDEPENDENT_RESOURCE_KEYSand a sharedresetResource()helper, and uses this when clearing provider/token selection to also resetpaymentMethodsloading/error.Improves request lifecycle handling by tagging pending requests with
resourceType, adding#abortDependentRequests()(invoked on region change and controller cleanup) and tightening loading ref-counting so aborted requests don’t incorrectly clear resourceisLoading. Region validation and “is result current” checks are consolidated into#requireRegion()/#is*Current()helpers, andgetCountries()defensively stores[]when the service returns a non-array.Updates tests and changelog to cover the new reset/abort semantics and error messages (including expecting in-flight quotes to reject when aborted).
Written by Cursor Bugbot for commit 4a32b8c. This will update automatically on new commits. Configure here.