Pretty cool, but this is when you need a debounced effect on state change. This won't work if you want to debounce, say, button clicks or generally function calls.
So I took inspiration from your code and created this which would be more in line with Lodash's debounce:
@Composable
fun debounced(
ms: Long,
coroutineScope: CoroutineScope = rememberCoroutineScope(),
func: () -> Unit,
): () -> Unit {
return remember {
var job: Job? = null
{
job?.cancel()
job = coroutineScope.launch {
delay(ms.milliseconds)
func()
}
}
}
}