Skip to content

Commit

Permalink
🩹: remove function interface
Browse files Browse the repository at this point in the history
  • Loading branch information
junerver committed Sep 11, 2024
1 parent 26c6ccd commit cb8b195
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 27 deletions.
14 changes: 0 additions & 14 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ ktor-client-content-negotiat = { group = "io.ktor", name = "ktor-client-content-
ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktorVersion" }
ktor-server-default-headers = { group = "io.ktor", name = "ktor-server-default-headers", version.ref = "ktorVersion" }


retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofitVersion" }
retrofit-converter-scalars = { group = "com.squareup.retrofit2", name = "converter-scalars", version.ref = "retrofitVersion" }
retrofit-converter-gson = { group = "com.squareup.retrofit2", name = "converter-gson", version.ref = "retrofitVersion" }
retrofit-converter-kotlinx-serialization = { group = "com.squareup.retrofit2", name = "converter-kotlinx-serialization", version.ref = "retrofitVersion" }
okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version = "4.12.0" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
jetbrainsKotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
Expand All @@ -99,13 +92,6 @@ jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "jetbrai
kotlinSerialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }

[bundles]
retrofit = [
"retrofit",
"retrofit-converter-scalars",
"retrofit-converter-gson",
"retrofit-converter-kotlinx-serialization",
]

ktor = [
"ktor-client-core",
"ktor-client-cio",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import xyz.junerver.compose.hooks.utils.currentTime
/**
* Debounce options
*
* @constructor Create empty Debounce options
* @property wait time to delay
* @property leading Specify invoking on the leading edge of the timeout.
* @property trailing Specify invoking on the trailing edge of the timeout.
* @property maxWait The maximum time func is allowed to be delayed before it’s invoked.
* @constructor Create empty Debounce options
* @property maxWait The maximum time func is allowed to be delayed before
* it’s invoked.
*/
data class DebounceOptions internal constructor(
var wait: Duration = 1.seconds,
Expand All @@ -36,7 +37,7 @@ internal class Debounce(
var fn: VoidFunction,
private val scope: CoroutineScope,
private val options: DebounceOptions = DebounceOptions(),
) : VoidFunction {
) {

private var calledCount = 0
private val jobs: MutableList<Pair<Job, Boolean>> = arrayListOf()
Expand All @@ -54,7 +55,7 @@ internal class Debounce(
}
}

override fun invoke(p1: TParams) {
fun invoke(p1: TParams) {
val (wait, leading, trailing, maxWait) = options
fun task(guarantee: Boolean, isDelay: Boolean) {
scope.launch {
Expand Down Expand Up @@ -114,7 +115,7 @@ fun useDebounceFn(
val debounced = remember {
Debounce(latestFn, scope, options)
}.apply { this.fn = latestFn }
return debounced
return { p1 -> debounced.invoke(p1) }
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import xyz.junerver.compose.hooks.utils.currentTime
/**
* Throttle options
*
* @constructor Create empty Throttle options
* @property wait time to delay
* @property leading Specify invoking on the leading edge of the timeout.
* @property trailing Specify invoking on the trailing edge of the timeout.
* @constructor Create empty Throttle options
*/
data class ThrottleOptions internal constructor(
var wait: Duration = 1.seconds,
Expand All @@ -30,7 +30,7 @@ internal class Throttle(
var fn: VoidFunction,
private val scope: CoroutineScope,
private val options: ThrottleOptions = ThrottleOptions(),
) : VoidFunction {
) {

private var calledCount = 0
private val trailingJobs: MutableList<Job> = arrayListOf()
Expand All @@ -45,7 +45,7 @@ internal class Throttle(
}
}

override fun invoke(p1: TParams) {
fun invoke(p1: TParams) {
val (wait, leading, trailing) = options
val waitTime = currentTime - latestInvokedTime

Expand Down Expand Up @@ -95,7 +95,7 @@ fun useThrottleFn(
val throttled = remember {
Throttle(latestFn, scope, options)
}.apply { this.fn = latestFn }
return throttled
return { p1 -> throttled.invoke(p1) }
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ private class DebouncePlugin<TData : Any> : Plugin<TData>() {
requestOptions.debounceOptions
)
fetch.runAsync = {
debounce(it)
debounce.invoke(it)
}
fetch.run = {
debounce(it)
debounce.invoke(it)
}
}
object : PluginLifecycle<TData>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ private class ThrottlePlugin<TData : Any> : Plugin<TData>() {
requestOptions.throttleOptions
)
fetch.run = {
throttle(it)
throttle.invoke(it)
}
fetch.runAsync = {
throttle(it)
throttle.invoke(it)
}
}
object : PluginLifecycle<TData>() {
Expand Down

0 comments on commit cb8b195

Please sign in to comment.