get-convex / convex-helpers

A collection of useful code to complement the official packages.
MIT License
93 stars 16 forks source link

Query Cache #136

Closed jamwt closed 3 months ago

jamwt commented 3 months ago

Implementation of a query cache that can dramatically speed up re-rendering of values as SPA routes change and components are unmounted and remounted.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

ianmacartney commented 3 months ago

A couple thoughts just coming to me:

  1. We could move this into the npm package, maybe namespaced under a react/cache entrypoint in a single file.
  2. I wonder if we should change the tanstack-style useQuery hook in helpers to also do this, but only if it's in the cache provider - and maybe push folks to use that one. As it stands there will be 3 options for useQuery: vanilla, this one, and tanstack-style. there's a usecase for vanilla - if you don't want to use the caching - but that implies there should be an opt-out for the tanstack-style one too. So either 2 versions (TanStack to also get caching, vanilla if you want to opt out), or 4 versions (full permutations - or an extra arg to tanstack about opting out). I kinda wish I had gone with a single object arg for the tanstack one after all ({ queryFn, queryArgs })

Was it intentional to keep it out of the npm package for now, just to point people to it?

jamwt commented 3 months ago
  1. I just don't know how to do that, but otherwise -- yeah, would love to have it in the package.
  2. Yeah, I debated the tanstack-style vs. this. Any ideas about how to support both without replicating a ton of code?
jamwt commented 3 months ago

Okay, I

  1. Refactored everything to be centered around useQueries so that the tanstack-style wrapper can use this cache.
  2. Fixed error handling
  3. Moved this into the convex-helpers package, in the react module.