Optimistic Updates - a proof of concept / demo / call for ideas #9117
Unanswered
JonLoesch
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
So I've been plugging away at this concept for a bit, and I'm curious what people think. It involves optimistic updates, or the idea of updating data in the cache of a query based of the prospective state of a mutation. Here's the link to the to the official docs section on this subject.
I'm really bad at being direct and to the point, but I'll try. Here's my three main points about how this proof of concept does things:
1 - It is done application wide once, instead of having it passed in everywhere where you run a certain mutation (this would mainly matter if you have the same mutation being used in multiple places)
2 - It uses an "immutable data mapper" (or function of type signature
(data: Immutable<T>) => Immutable<T>
) as its core abstraction. These functions are inserted into a post-fetch step in the TanStack Query process, and this means that it handles plenty of race conditions completely (two mutations at once, query while mutation is active, etc...)3 - It interfaces with
@tanstack/query-core
instead of@tanstack/react-query
. This means that the optimization logic doesn't have to worry about react hooks, or any other UI framework at all.I started this project as a TRPC plugin and I was focused on point #1. But as the idea has evolved I think it's more appropriate at the TanStack Query level, and I think actually points #2 and #3 are way more important than #1. So I'm starting a new discussion about it here.
Here's what optimistic update logic might look like in my system:
I'll stop the explanation there. If you want to see a whole lot more excruciating over-explained detail, there's a github repo I published as a POC. But there's a lot there and it's messy (And it's also incomplete and the example code doesn't line up exactly. And it works via some really hacky javascript modification of prototypes to extend functionality. Crucially though, it does actually work).
So here's my question for the TanStack community: Should I pursue this as an optional componion module for tanstack query? Or as a fork/PR? Or at all?
Beta Was this translation helpful? Give feedback.
All reactions