Skip to content
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

[Discussion] useLiveQuery with cached results. #545

Open
thruflo opened this issue Feb 17, 2025 · 1 comment
Open

[Discussion] useLiveQuery with cached results. #545

thruflo opened this issue Feb 17, 2025 · 1 comment
Labels
enhancement New feature or request question Further information is requested

Comments

@thruflo
Copy link
Contributor

thruflo commented Feb 17, 2025

Building a little bit on #544, I propose an optional mechanism for useLiveQuery to re-use the previous query results when re-rendering a component.

My use case was as follows:

  • two screens in an app
  • the first screen loads some listings using a live query
  • navigate to the second screen
  • navigate back to the first

Even with a small query, if I use the IDB filesystem, I get a noticeable flash of missing content -> content when I navigate back to the first screen. Because useLiveQuery always starts undefined and then async populates the results.

What I'd like is, in this type of circumstance, is to be able to say "hey, live query, cache the query results and use the latest cache as the default when rendering". This way, the user sees content immediately when navigating back to the page.

There may be downsides when the content underneath the live query has changed, such as listings jumping around. But this can be mitigated by (1) being an optional mechanism or (2) possibly considering ways to invalidate the cache on changes.

I imagine that this could be implemented using an option, such as {cacheResults: true}. Or it could possibly be a special value for default if using option syntax to implement #544, such as {default: live.CACHE}.


I guess the other approach here is to be able to keep the liveQuery going behind the component lifecycle.

I.e.: instead of teardown on unmount, somehow mark the query as having a ttl in the background and then using the same instance of a live query when re-rendering with the same key. This could provide the best of both worlds, in that results are instant and uptodate when navigating back.

Perhaps we would need a queryKey or perhaps we can dedupe by the actual query string and params anyway? Perhaps we would need an unMountedTTL or something? If we found a way, it would perhaps be similar to the idea that shape subscriptions go on in the background unless explicitly stopped.

@thruflo thruflo added enhancement New feature or request question Further information is requested labels Feb 17, 2025
@samwillis
Copy link
Collaborator

I like this.

There is another option in this situation (that we use in linearlite) that we need to document better. You can create the liveQuery in the React Route / TanStack Route loader, then pass it to the component and useLiveQuery(myLiveQueryInstance). This is this call signature:

export function useLiveQuery<T = { [key: string]: unknown }>(
liveQuery: LiveQuery<T>,
): LiveQueryResults<T>

This ensures that the query has done its initial load before the route changes to the navigated to screen.

We should add caching though! I wander if there is a way to do it in a global way 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants