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 myself2 Replies
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.Wow just saw this - really nice API! I've wanted to design a more ergonomic API and this one feels really good - will draw from it for sure when the time comes
In the mean time a library sounds great - or if you want to contribute a PR to
convex-helpers we can get it in there