megasergM
Convex Community3y ago
17 replies
megaserg

Optimistic updates for debounced mutations

I would like to have an optimistic update where the mutation is debounced (not single-flighted, but debounced)
My usecase: When user types text in the field, the text should get saved in the database, but only after 1 sec after the user stopped typing. Meanwhile, another user looking at the same page should see the edited text as it gets updated in the database. Kind of like Google Doc.
Can I have no local useState and just have the value as
useQuery
result, and apply mutation withOptimisticUpdate? But how to introduce debouncing?
Alternatively, I could keep local useState, and have my custom debounce() wrapper which sets timeout and so on. And wrap mutation with that, and call debouncedMutation onChange. But then what about the other user? Should I Watch the query and update local state there?
Was this page helpful?