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 timeout: Int? = null
private var invokeOnTimeout = false private var invokeOnTimeout = false
private var function: () -> Unit = {}
operator fun invoke(f: () -> Unit) { operator fun invoke(f: () -> Unit) {
function = f
if (timeout == null) { if (timeout == null) {
if (leading) { if (leading) {
f() function()
} else if (trailing) {
invokeOnTimeout = true
} }
timeout = self.setTimeout({ timeout = self.setTimeout({
if (invokeOnTimeout) { if (invokeOnTimeout) {
f() function()
} }
timeout = null timeout = null