SUBSCRIBE NOW
avatar
I always learn something just by skimming it that makes me want to bookmark the issue now and dig deeper later
SUBSCRIBE NOW
avatar
Keep up the good work with the newsletter 💪 I really enjoy it
SUBSCRIBE NOW
avatar
Dispatch is a must read for Android devs today and my go-to for keeping up with all things Jetpack Compose
SUBSCRIBE NOW
avatar
Dispatch has been my go-to resource as it's packed with useful information while being fun at the same time
SUBSCRIBE NOW
avatar
The content is light, fun, and still useful. I especially appreciate the small tips that are in each issue
SUBSCRIBE NOW
avatar
I truly love this newsletter ❤️‍🔥 Spot on content and I know there's a lot of effort that goes behind it
SUBSCRIBE NOW
avatar
Thanks for taking the time and energy to do it so well
JetpackCompose.app's Newsletter
avatar
I always learn something just by skimming it that makes me want to bookmark the issue now and dig deeper later
JetpackCompose.app's Newsletter
avatar
Keep up the good work with the newsletter 💪 I really enjoy it
JetpackCompose.app's Newsletter
avatar
Dispatch is a must read for Android devs today and my go-to for keeping up with all things Jetpack Compose
JetpackCompose.app's Newsletter
avatar
Dispatch has been my go-to resource as it's packed with useful information while being fun at the same time
JetpackCompose.app's Newsletter
avatar
The content is light, fun, and still useful. I especially appreciate the small tips that are in each issue
JetpackCompose.app's Newsletter
avatar
I truly love this newsletter ❤️‍🔥 Spot on content and I know there's a lot of effort that goes behind it
JetpackCompose.app's Newsletter
avatar
Thanks for taking the time and energy to do it so well

Debounce Mutation State Policy

Author: Pablisco

This is an alternate method of debouncing state changes in Jetpack Compose.

Using this technique, you can leverage a custom SnapshotMutationPolicy to accomplish the same debouncing behavior. The main difference is that this approach uses the default mutableStateOf implementation with a custom mutation policy instead.

/**
* Returns a [SnapshotMutationPolicy] implementation that applies a debounce mechanism to mutation events.
*
* @param debounceTime The time window in milliseconds within which mutation events are considered to be the same.
* @return An instance of [SnapshotMutationPolicy] that applies a debounce mechanism.
*
* @param T The type of the snapshot.
*/
fun <T> debounceMutationPolicy(
debounceTime: Long = 500,
): SnapshotMutationPolicy<T> = object : SnapshotMutationPolicy<T> {
var lastUpdate = 0L
override fun equivalent(a: T, b: T): Boolean = checkEquivalence(a, b).also { updateIfNeeded() }
private fun updateIfNeeded() = apply { if (isUpdateTime()) lastUpdate = now() }
private fun checkEquivalence(a: T, b: T): Boolean = if (isUpdateTime()) a == b else CancelMutation
private fun isUpdateTime() = now() - lastUpdate > debounceTime
}
/**
* Determines whether a mutation should be canceled or not.
*
* The value of this property determines whether a mutation should be canceled or not based on the conditions defined
* in the "checkEquivalence" function.
* If CancelMutation is set to `true`, the "checkEquivalence" function will return `true` if the time between the last
* update and the current time is greater than the debounce time.
*
* Otherwise, it will return the result of the equality comparison between the two provided values.
*/
private const val CancelMutation = true
mutableStateOf(initialValue, debounceMutationPolicy())
view raw usage.kt hosted with ❤ by GitHub

It’s a great reminder that in coding, flexibility is key. Whether you choose to extend MutableState or use a custom mutation policy, you’ve got options to tailor your state management to your needs.

Have a project you'd like to submit? Fill this form, will ya!

If you like this snippet, you might also like:

Maker OS is an all-in-one productivity system for developers

I built Maker OS to track, manage & organize my life. Now you can do it too!