Skip to content

Conversation

@georgeweiler
Copy link
Contributor

@georgeweiler georgeweiler commented Jan 5, 2026

Explanation

This PR adds a new createRequestSelector utility function to the @metamask/ramps-controller package. This utility enables consumers to create memoized selectors for tracking request states (loading, error, data) from RampsController methods.

Current state: Consumers who want to track request states from RampsController methods (like updateGeolocation, getCryptoCurrencies, etc.) need to manually create selectors that extract and transform the request cache state. This can lead to:

  • Inefficient re-renders if selectors aren't properly memoized
  • Boilerplate code repeated across different request selectors
  • Potential bugs from incorrect memoization logic

Solution: The createRequestSelector function provides a standardized, optimized way to create selectors for request states. It:

  • Uses reference equality on the request object itself (not deep equality), making it efficient even for large arrays/objects
  • Returns the same object reference when the underlying request state hasn't changed, so no shallowEqual is needed with React Redux's useSelector
  • Handles all request states (loading, success, error, missing) consistently
  • Works seamlessly with reselect selectors and the existing RampsController state structure

Implementation details: The selector memoizes by comparing the request object reference. When the controller updates state, if it preserves the request object reference (typical in immutable state management), the selector returns the cached result. This avoids expensive deep equality checks while maintaining correctness.

References

example usage: This selector is being used in this metamask mobile pull request

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

Note

Introduces a memoized selector utility for RampsController request states.

  • Adds selectors.ts with createRequestSelector and RequestSelectorResult for efficient data/isFetching/error selection using cache keys
  • Exposes createRequestSelector and RequestSelectorResult via src/index.ts
  • Comprehensive tests in selectors.test.ts covering loading/success/error states, memoization stability, state transitions, and cache key isolation
  • Updates CHANGELOG.md under Unreleased to document the new utility

Written by Cursor Bugbot for commit 93f3172. This will update automatically on new commits. Configure here.

@georgeweiler georgeweiler changed the title feat(ramps): adds ramps selectors feat(ramps): adds an agnostic selector for ramps request metadata Jan 6, 2026
@georgeweiler georgeweiler marked this pull request as ready for review January 6, 2026 02:54
@georgeweiler georgeweiler requested review from a team as code owners January 6, 2026 02:54
@georgeweiler georgeweiler added this pull request to the merge queue Jan 6, 2026
Merged via the queue into main with commit 9493bbc Jan 6, 2026
286 checks passed
@georgeweiler georgeweiler deleted the ramps-controller-selectors branch January 6, 2026 15:42
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.

3 participants