tinyplex / tinybase

The reactive data store for local‑first apps.
https://tinybase.org
MIT License
3.23k stars 65 forks source link

createRemotePersister: ability to throttle/debounce autoSave #107

Open rob-gordon opened 8 months ago

rob-gordon commented 8 months ago

Is your feature request related to a problem? Please describe. It would be really helpful if the remote persister allowed throttling or debouncing the rate at which it saves.

Describe the solution you'd like It could either be an argument to createRemotePersister the same way autoLoadIntervalSeconds works, or for people who want more control it could be a function of the save function, so that people could do something like:

 createRemotePersister(store, "/api/load", "/api/save", 5, (save) => debounce(save, 2000))

Describe alternatives you've considered If my remote persister is a node server then I can just throttle/debounce database writes on the server side, but ideally it would be great if this could work with serverless functions which don't run continuously. In that case I can only see it working if it's throttled on the client-side.

Additional context Thank you for this awesome library!

jamesgpearce commented 8 months ago

Good idea! In the meantime, you could defend against craziness by wrapping multiple fast changes in a transaction. (The autoSave only updates at the end of a transaction)