-
Notifications
You must be signed in to change notification settings - Fork 427
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
refactor(core): add reusable rxSwr operator #7562
Conversation
751342b
to
c68bba8
Compare
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
This stack of pull requests is managed by Graphite. Learn more about stacking. |
75e3e83
to
21885ea
Compare
No changes to documentation |
21885ea
to
f98b189
Compare
f98b189
to
dd3efb0
Compare
Component Testing Report Updated Oct 6, 2024 10:55 PM (UTC) ✅ All Tests Passed -- expand for details
|
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.
Looks great to me!
dd3efb0
to
26026ac
Compare
⚡️ Editor Performance ReportUpdated Sun, 06 Oct 2024 23:07:41 GMT
Detailed information🏠 Reference resultThe performance result of
🧪 Experiment resultThe performance result of this branch
📚 Glossary
|
### Description This PR introduces a new utility function `createSWR` for implementing stale-while-revalidate caching in RxJS observables. The `createSwr` function lets you create an operator function that can be used with a cache key for storing the last emitted value. Once an observable with the same cache key is subscribed to, it will emit the cached value (if any) immediately while waiting for the fresh value to arrive. This PR also refactors the `listenSearchQuery` function to use this new SWR implementation, replacing the previous LRU cache approach. ### What to review - The new `rxSwr.ts` file in the `core/util` directory, which contains the `createSWR` function and related types. - The changes in `listenSearchQuery.ts`, particularly the removal of the `memoLRU` function and the integration of the new `swr` utility. To add SWR to any observable: ```typescript import {createSWR} from 'sanity' const swr = createSWR<ObservableValueType>({maxSize: 100}) // In your observable pipeline: someObservable$.pipe( swr('<some cacheKey identifying the observable>'), map(({fromCache, value}) => { // Handle cached and fresh data // `fromCache` is true if `value` was read from cache, otherwise false. }) ) ``` ### Testing Included some basic unit tests for rxSwr ### Notes for release n/a internal
Description
This PR introduces a new utility function
createSWR
for implementing stale-while-revalidate caching in RxJS observables. ThecreateSwr
function lets you create an operator function that can be used with a cache key for storing the last emitted value. Once an observable with the same cache key is subscribed to, it will emit the cached value (if any) immediately while waiting for the fresh value to arrive.This PR also refactors the
listenSearchQuery
function to use this new SWR implementation, replacing the previous LRU cache approach.What to review
rxSwr.ts
file in thecore/util
directory, which contains thecreateSWR
function and related types.listenSearchQuery.ts
, particularly the removal of thememoLRU
function and the integration of the newswr
utility.To add SWR to any observable:
Testing
Included some basic unit tests for rxSwr
Notes for release
n/a internal