copper-leaf / ballast

Opinionated Application State Management framework for Kotlin Multiplatform
https://copper-leaf.github.io/ballast/
BSD 3-Clause "New" or "Revised" License
147 stars 10 forks source link

[ballast-saved-state] add option for a minimum delay between "save" requests #23

Closed cjbrooks12 closed 1 year ago

cjbrooks12 commented 2 years ago

For ViewModels that are updated very quickly, it may be desirable to skip some State emissions in order to not overload the disk and make sure that writes to the disk don't slow down the main ViewModel's processing.

To that end, all writes should be persisted asynchronously, and a configurable delay between emissions. By default, the delay will be 0. If there is a delay, then that much time will wait before sending the state to the adapter. If a new State is emitted before that delay completes, then the latest State value will be persisted. This needs to be more clever than a simple .mapLatest, as we don't want to reset the timer because emissions may continually prevent the adapter from ever being called.

cjbrooks12 commented 1 year ago

bufferFlow parameter added to BallastSavedStateInterceptor in 2.0.0, which allows user-specified buffering/filtering on the States as they are sent to be saved