Skip to content

Commit

Permalink
Move debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinstardust committed Mar 1, 2024
1 parent 68c542c commit 218a904
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions library/src/main/java/com/paypal/messages/PayPalMessageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ class PayPalMessageView @JvmOverloads constructor(
debounceUpdateContent(Unit)
}

private fun <T> debounce(
delayMs: Long = 1L,
coroutineContext: CoroutineContext = Dispatchers.Main,
callback: (T) -> Unit,
): (T) -> Unit {
var debounceJob: Job? = null
return { param: T ->
if (debounceJob?.isCompleted != false) {
debounceJob = CoroutineScope(coroutineContext).launch {
delay(delayMs)
callback(param)
}
}
}
}

// This must be above the set methods to prevent errors when using XML attributes
val debounceUpdateContent = debounce<Unit> { updateMessageContent() }

/**
* DATA
*/
Expand Down Expand Up @@ -303,24 +322,6 @@ class PayPalMessageView @JvmOverloads constructor(
this.modal?.dismiss()
}

private fun <T> debounce(
delayMs: Long = 1L,
coroutineContext: CoroutineContext = Dispatchers.Main,
callback: (T) -> Unit,
): (T) -> Unit {
var debounceJob: Job? = null
return { param: T ->
if (debounceJob?.isCompleted != false) {
debounceJob = CoroutineScope(coroutineContext).launch {
delay(delayMs)
callback(param)
}
}
}
}

val debounceUpdateContent = debounce<Unit> { updateMessageContent() }

/**
* This function purpose is to update only the UI of the [PayPalMessageView] component.
* It makes use of the existing values for the message content and style
Expand Down

0 comments on commit 218a904

Please sign in to comment.