From cb8b195e87a33c628227ee6b0514dbfa226920c3 Mon Sep 17 00:00:00 2001 From: Junerver Date: Wed, 11 Sep 2024 11:51:18 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9:=20remove=20function=20interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle/libs.versions.toml | 14 -------------- .../xyz/junerver/compose/hooks/useDebounce.kt | 11 ++++++----- .../xyz/junerver/compose/hooks/useThrottle.kt | 8 ++++---- .../hooks/userequest/plugins/useDebouncePlugin.kt | 4 ++-- .../hooks/userequest/plugins/useThrottlePlugin.kt | 4 ++-- 5 files changed, 14 insertions(+), 27 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 27813b9b..f5303a1d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" } @@ -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", diff --git a/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/useDebounce.kt b/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/useDebounce.kt index d0ca2c8e..6448146f 100644 --- a/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/useDebounce.kt +++ b/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/useDebounce.kt @@ -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, @@ -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> = arrayListOf() @@ -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 { @@ -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 diff --git a/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/useThrottle.kt b/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/useThrottle.kt index e5701866..dd150941 100644 --- a/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/useThrottle.kt +++ b/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/useThrottle.kt @@ -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, @@ -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 = arrayListOf() @@ -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 @@ -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 diff --git a/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/userequest/plugins/useDebouncePlugin.kt b/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/userequest/plugins/useDebouncePlugin.kt index c489cde6..47c89e3c 100644 --- a/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/userequest/plugins/useDebouncePlugin.kt +++ b/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/userequest/plugins/useDebouncePlugin.kt @@ -29,10 +29,10 @@ private class DebouncePlugin : Plugin() { requestOptions.debounceOptions ) fetch.runAsync = { - debounce(it) + debounce.invoke(it) } fetch.run = { - debounce(it) + debounce.invoke(it) } } object : PluginLifecycle() { diff --git a/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/userequest/plugins/useThrottlePlugin.kt b/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/userequest/plugins/useThrottlePlugin.kt index 1c7f3608..c6624290 100644 --- a/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/userequest/plugins/useThrottlePlugin.kt +++ b/hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/userequest/plugins/useThrottlePlugin.kt @@ -31,10 +31,10 @@ private class ThrottlePlugin : Plugin() { requestOptions.throttleOptions ) fetch.run = { - throttle(it) + throttle.invoke(it) } fetch.runAsync = { - throttle(it) + throttle.invoke(it) } } object : PluginLifecycle() {