Skip to content

Conversation

@georgeweiler
Copy link
Contributor

@georgeweiler georgeweiler commented Dec 18, 2025

Explanation

Current state: The RampsController has a simple updateGeolocation method that fetches geolocation from the RampsService and stores it in state. There's no caching, deduplication, or request lifecycle management. When multiple UI components need the same data, each triggers a separate API call, leading to redundant network requests and inconsistent loading states across the app.

Solution: This PR introduces a request caching infrastructure to RampsController that:

  • Caches responses with TTL - Requests are cached by a key (method + params) with configurable time-to-live (default 15 minutes). Subsequent calls return cached data if still valid.
  • Deduplicates concurrent requests - If multiple callers request the same data simultaneously, they share a single in-flight promise rather than making duplicate API calls.
  • Supports abort - In-flight requests can be aborted by cache key, useful for cleanup on component unmount.
  • Tracks request state - Each cached request stores its status (idle/loading/success/error), data, error message, and timestamps. This state is exposed via Redux for UI consumption.
  • Evicts oldest entries - Cache is capped at a configurable max size (default 250), evicting oldest entries when full.

The updateGeolocation method now uses this infrastructure, serving as the first implementation and a pattern for future controller methods.

Non-obvious details:

  • The requests state field is marked persist: false since request cache should not survive app restarts.
  • RequestState.status uses a template literal type (`${RequestStatus}`) rather than the enum directly to satisfy the controller's StateConstraint JSON compatibility requirement.
  • Type assertions are used in #updateRequestState to work around immer's Draft type causing deep instantiation errors—this pattern exists elsewhere in the codebase.

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

Note

Introduces a request caching system with TTL, deduplication, aborting, and eviction, and updates RampsController (incl. updateGeolocation) to use it with new state and APIs.

  • RampsController:
    • Add requests cache to state (non-persisted) and metadata; reset on init.
    • New options: requestCacheTTL, requestCacheMaxSize.
    • New APIs: executeRequest (cached/deduped with TTL, error handling), abortRequest, getRequestState.
    • Cache eviction by oldest timestamp when exceeding requestCacheMaxSize.
    • Update updateGeolocation to use caching via createCacheKey and return string.
  • Caching Infrastructure (src/RequestCache.ts):
    • Add RequestStatus, RequestState, cache utils (createCacheKey, isCacheExpired, createLoadingState, createSuccessState, createErrorState), defaults (DEFAULT_REQUEST_CACHE_TTL, DEFAULT_REQUEST_CACHE_MAX_SIZE).
  • Exports (src/index.ts):
    • Re-export new controller options/APIs and caching types/utilities.
  • Tests:
    • Add comprehensive tests for controller caching behavior, abort flow, eviction, and cache utils.
  • Changelog:
    • Note addition of request caching with TTL, deduplication, and abort support.

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

@georgeweiler
Copy link
Contributor Author

cursor review

@georgeweiler
Copy link
Contributor Author

cursor review.

@georgeweiler georgeweiler changed the title feat(ramps-controller): add request caching system with deduplication… feat(ramps-controller): add request caching with TTL, deduplication, and abort support Dec 18, 2025
@georgeweiler georgeweiler force-pushed the TRAM-2925-ramps-controller-cache branch from abb2793 to a7497e4 Compare December 18, 2025 17:23
@georgeweiler
Copy link
Contributor Author

cursor review

@georgeweiler georgeweiler marked this pull request as ready for review December 18, 2025 17:24
@georgeweiler georgeweiler requested review from a team as code owners December 18, 2025 17:24
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no bugs!


@georgeweiler georgeweiler added this pull request to the merge queue Dec 19, 2025
Merged via the queue into main with commit 8b817b0 Dec 19, 2025
288 checks passed
@georgeweiler georgeweiler deleted the TRAM-2925-ramps-controller-cache branch December 19, 2025 04:57
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