Easier Optimistic Updates
Hi, just wanted to share a small helper for optimistic updates.
First, here's some example usage:
The helper is the
withOptimisticQuery
function - you get the return value of all queries, and can easily just mutate the state - even if you don't know the exact args, etc.
It's all 100% TypeSafe, too, and it avoids unnecessary updates. Thanks to immer
it also does treat everything as immutable, but you can still use it as you're used to :)
For comparision, here's the mutation with "plain" convex:
Anyway, needless to say, I prefer the new version. Funnily enough, the "before" version also likely causes more re-renders. immer
doesn't create an optimistic update if the old and new title are the same. In my code, I think I'd have to check myself1 Reply
Dependencies:
immer
I might make this an npm package (needs a bit of cleanup tho), but tbh, I feel like sth like this should be part of core convex.
Also: All this code is licensed under MIT / and/or WTFPL - up to the user. As in: convex core team, feel free to play around with this.
Also, question to convex team: have you considered looking into autogenerating some optimistic updates in some way? I think there's some real opportunity there, esp. if you do symbolic execution.
The disadvantage is, of course, it becomes entirely unpredictable whether your optimistic stuff will work automagically or not, which is sad. Hm.