Fixed two bugs in Throttle.

This commit is contained in:
Daan Vanden Bosch 2021-05-11 19:35:53 +02:00
parent 1923be4b3f
commit 12883de487

View File

@ -16,16 +16,21 @@ class Throttle(
) {
private var timeout: Int? = null
private var invokeOnTimeout = false
private var function: () -> Unit = {}
operator fun invoke(f: () -> Unit) {
function = f
if (timeout == null) {
if (leading) {
f()
function()
} else if (trailing) {
invokeOnTimeout = true
}
timeout = self.setTimeout({
if (invokeOnTimeout) {
f()
function()
}
timeout = null