Skip to content

Instantly share code, notes, and snippets.

@Terenfear
Last active June 27, 2024 12:56
Show Gist options
  • Save Terenfear/a84863be501d3399889455f391eeefe5 to your computer and use it in GitHub Desktop.
Save Terenfear/a84863be501d3399889455f391eeefe5 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Basic debounce operators based on [the Stack Overflow answer](https://stackoverflow.com/a/55802898/5102726) from [Patrick](https://stackoverflow.com/users/751581/patrick)
*
* Usage:
* ```
* val onEmailChange: (String) -> Unit = throttleLatest(300L, viewLifecycleOwner.lifecycleScope, viewModel::onEmailChanged)
* emailTextView.onTextChanged(onEmailChange)
* ```
*
* Created by Terenfear on 26.07.2019.
*/
/**
* Constructs a function that processes input data and passes the latest data to [destinationFunction] in the end of every time window with length of [intervalMs].
*/
fun <T> throttleLatest(
intervalMs: Long = 300L,
coroutineScope: CoroutineScope,
destinationFunction: (T) -> Unit
): (T) -> Unit {
var throttleJob: Job? = null
var latestParam: T
return { param: T ->
latestParam = param
if (throttleJob?.isCompleted != false) {
throttleJob = coroutineScope.launch {
delay(intervalMs)
destinationFunction(latestParam)
}
}
}
}
/**
* Constructs a function that processes input data and passes it to [destinationFunction] only if there's no new data for at least [waitMs]
*/
fun <T> debounce(
waitMs: Long = 300L,
coroutineScope: CoroutineScope,
destinationFunction: (T) -> Unit
): (T) -> Unit {
var debounceJob: Job? = null
return { param: T ->
debounceJob?.cancel()
debounceJob = coroutineScope.launch {
delay(waitMs)
destinationFunction(param)
}
}
}
/**
* Constructs a function that processes input data and passes the first data to [destinationFunction] and skips all new data for the next [skipMs].
*/
fun <T> throttleFirst(
skipMs: Long = 300L,
coroutineScope: CoroutineScope,
destinationFunction: (T) -> Unit
): (T) -> Unit {
var throttleJob: Job? = null
return { param: T ->
if (throttleJob?.isCompleted != false) {
throttleJob = coroutineScope.launch {
destinationFunction(param)
delay(skipMs)
}
}
}
}
@Teja-Konjeti
Copy link

@Terenfear Can you add a copyright? Apache 2.0 maybe?

@Terenfear
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment