Skip to content

Commit

Permalink
⚡️: deprecated hooks are marked as private
Browse files Browse the repository at this point in the history
  • Loading branch information
junerver committed Nov 8, 2024
1 parent 8489e75 commit 190fb9f
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 217 deletions.
38 changes: 17 additions & 21 deletions hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/declare.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package xyz.junerver.compose.hooks

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import xyz.junerver.compose.hooks.useredux.useDispatch
Expand Down Expand Up @@ -37,9 +36,9 @@ inline fun <reified T, R> rememberSelector(alias: String? = null, crossinline bl
@Composable
fun <TData : Any> rememberRequest(
requestFn: SuspendNormalFunction<TData>,
options: RequestOptions<TData> = RequestOptions(),
optionsOf: RequestOptions<TData>.() -> Unit = {},
plugins: Array<@Composable (RequestOptions<TData>) -> Plugin<TData>> = emptyArray(),
) = useRequest(requestFn, options, plugins)
) = useRequest(requestFn, optionsOf, plugins)

//region useAsync
@Composable
Expand Down Expand Up @@ -68,24 +67,24 @@ fun rememberClipboard() = useClipboard()
fun <T> rememberContext(context: ReactContext<T>) = useContext(context)

@Composable
fun rememberCountdown(options: CountdownOptions) = useCountdown(options)
fun rememberCountdown(optionsOf: CountdownOptions.() -> Unit) = useCountdown(optionsOf)

@Composable
fun rememberCounter(initialValue: Int = 0, options: CounterOptions) = useCounter(initialValue, options)
fun rememberCounter(initialValue: Int = 0, optionsOf: CounterOptions.() -> Unit) = useCounter(initialValue, optionsOf)

@Composable
fun <T> rememberCreation(vararg keys: Any?, factory: () -> T) = useCreation(*keys, factory = factory)

//region useDebounce
@Composable
fun <S> rememberDebounce(value: S, options: DebounceOptions = remember { DebounceOptions() }) = useDebounce(value, options)
fun <S> rememberDebounce(value: S, optionsOf: DebounceOptions.() -> Unit = {}) = useDebounce(value, optionsOf)

@Composable
fun rememberDebounceFn(fn: VoidFunction, options: DebounceOptions = remember { DebounceOptions() }) = useDebounceFn(fn, options)
fun rememberDebounceFn(fn: VoidFunction, optionsOf: DebounceOptions.() -> Unit = {}) = useDebounceFn(fn, optionsOf)

@Composable
fun LaunchedDebounceEffect(vararg keys: Any?, options: DebounceOptions = remember { DebounceOptions() }, block: SuspendAsyncFn) =
useDebounceEffect(*keys, options = options, block = block)
fun LaunchedDebounceEffect(vararg keys: Any?, optionsOf: DebounceOptions.() -> Unit = {}, block: SuspendAsyncFn) =
useDebounceEffect(*keys, optionsOf = optionsOf, block = block)
//endregion

//region useEvent
Expand All @@ -109,11 +108,10 @@ fun <T> _rememberGetState(default: T) = _useGetState(default = default)
//endregion

@Composable
fun rememberInterval(options: IntervalOptions = remember { IntervalOptions() }, block: () -> Unit) = useInterval(options, block)
fun rememberInterval(optionsOf: IntervalOptions.() -> Unit = {}, block: () -> Unit) = useInterval(optionsOf, block)

@Composable
fun rememberInterval(options: IntervalOptions = remember { IntervalOptions() }, ready: Boolean, block: () -> Unit) =
useInterval(options, ready, block)
fun rememberInterval(optionsOf: IntervalOptions.() -> Unit = {}, ready: Boolean, block: () -> Unit) = useInterval(optionsOf, ready, block)

@Composable
fun <T> rememberLatestRef(value: T) = useLatestRef(value)
Expand All @@ -138,7 +136,7 @@ fun <K, V> rememberMap(pairs: Iterable<Pair<K, V>>) = useMap(pairs)
fun rememberMount(fn: SuspendAsyncFn) = useMount(fn)

@Composable
fun rememberNow(options: UseNowOptions = remember { UseNowOptions() }) = useNow(options)
fun rememberNow(optionsOf: UseNowOptions.() -> Unit = {}) = useNow(optionsOf)

//region useNumber
@Composable
Expand Down Expand Up @@ -178,26 +176,24 @@ fun <T> _rememberState(default: T) = _useState(default)

//region useThrottle
@Composable
fun <S> rememberThrottle(value: S, options: ThrottleOptions = remember { ThrottleOptions() }) = useThrottle(value, options)
fun <S> rememberThrottle(value: S, optionsOf: ThrottleOptions.() -> Unit = {}) = useThrottle(value, optionsOf)

@Composable
fun rememberThrottleFn(fn: VoidFunction, options: ThrottleOptions = remember { ThrottleOptions() }) = useThrottleFn(fn, options)
fun rememberThrottleFn(fn: VoidFunction, optionsOf: ThrottleOptions.() -> Unit = {}) = useThrottleFn(fn, optionsOf)

@Composable
fun LaunchedThrottleEffect(vararg keys: Any?, options: ThrottleOptions = remember { ThrottleOptions() }, block: SuspendAsyncFn) =
useThrottleEffect(*keys, options = options, block = block)
fun LaunchedThrottleEffect(vararg keys: Any?, optionsOf: ThrottleOptions.() -> Unit = {}, block: SuspendAsyncFn) =
useThrottleEffect(*keys, optionsOf = optionsOf, block = block)
//endregion

@Composable
fun rememberTimeout(delay: Duration = 1.seconds, block: () -> Unit) = useTimeout(delay, block)

@Composable
fun rememberTimestamp(options: TimestampOptions = remember { TimestampOptions() }, autoResume: Boolean = true) =
useTimestamp(options, autoResume)
fun rememberTimestamp(optionsOf: TimestampOptions.() -> Unit = {}, autoResume: Boolean = true) = useTimestamp(optionsOf, autoResume)

@Composable
fun rememberTimestampRef(options: TimestampOptions = remember { TimestampOptions() }, autoResume: Boolean = true) =
useTimestampRef(options, autoResume)
fun rememberTimestampRef(optionsOf: TimestampOptions.() -> Unit = {}, autoResume: Boolean = true) = useTimestampRef(optionsOf, autoResume)

//region useToggle
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class Options<T>(val creator: () -> T) {
*
*/
@Deprecated("This function is not recommended, please use the specific `XxxOptions.optionOf{}` instead")
inline fun <reified T> optionsOf(noinline opt: T.() -> Unit): T = when (T::class) {
private inline fun <reified T> optionsOf(noinline opt: T.() -> Unit): T = when (T::class) {
CountdownOptions::class -> CountdownOptions.optionOf(opt as CountdownOptions.() -> Unit)
CounterOptions::class -> CounterOptions.optionOf(opt as CounterOptions.() -> Unit)
DebounceOptions::class -> DebounceOptions.optionOf(opt as DebounceOptions.() -> Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ data class CountdownOptions internal constructor(
companion object : Options<CountdownOptions>(::CountdownOptions)
}

@Deprecated(
"Please use the performance-optimized version. Do not pass the Options instance directly. You can simply switch by adding `=` after the `optionsOf` function. If you need to use an older version, you need to explicitly declare the parameters as `options`"
)
@Composable
fun useCountdown(options: CountdownOptions): CountdownHolder {
private fun useCountdown(options: CountdownOptions): CountdownHolder {
val (leftTime, targetDate, interval, onEnd) = options
require(leftTime.asBoolean() || targetDate.asBoolean()) {
"'leftTime' or 'targetDate' must be set"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ data class CounterOptions internal constructor(
* }.right())
* ```
*/
@Deprecated(
"Please use the performance-optimized version. Do not pass the Options instance directly. You can simply switch by adding `=` after the `optionsOf` function. If you need to use an older version, you need to explicitly declare the parameters as `options`"
)
@Composable
fun useCounter(initialValue: Int = 0, options: CounterOptions): CounterHolder {
private fun useCounter(initialValue: Int = 0, options: CounterOptions): CounterHolder {
val (current, setCurrent, getCurrent) = useGetState(getTargetValue(initialValue, options))
val setValue: SetValueFn<Either<Int, (Int) -> Int>> = { value: Either<Int, (Int) -> Int> ->
val target = value.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,23 @@ internal class Debounce(
}
}

@Deprecated(
"Please use the performance-optimized version. Do not pass the Options instance directly. You can simply switch by adding `=` after the `optionsOf` function. If you need to use an older version, you need to explicitly declare the parameters as `options`"
@Composable
fun <S> useDebounce(value: S, optionsOf: DebounceOptions.() -> Unit = {}): State<S> =
useDebounce(value, remember { DebounceOptions.optionOf(optionsOf) })

@Composable
fun useDebounceFn(fn: VoidFunction, optionsOf: DebounceOptions.() -> Unit = {}): VoidFunction =
useDebounceFn(fn, remember { DebounceOptions.optionOf(optionsOf) })

@Composable
fun useDebounceEffect(vararg keys: Any?, optionsOf: DebounceOptions.() -> Unit = {}, block: SuspendAsyncFn) = useDebounceEffect(
keys = keys,
remember { DebounceOptions.optionOf(optionsOf) },
block
)

@Composable
fun <S> useDebounce(value: S, options: DebounceOptions = remember { DebounceOptions() }): State<S> {
private fun <S> useDebounce(value: S, options: DebounceOptions = remember { DebounceOptions() }): State<S> {
val (debounced, setDebounced) = _useGetState(value)
val debouncedSet = useDebounceFn(fn = {
setDebounced(value)
Expand All @@ -104,20 +116,13 @@ fun <S> useDebounce(value: S, options: DebounceOptions = remember { DebounceOpti
return debounced
}

@Composable
fun <S> useDebounce(value: S, optionsOf: DebounceOptions.() -> Unit): State<S> =
useDebounce(value, remember { DebounceOptions.optionOf(optionsOf) })

/**
* 需要注意:[Debounce] 不返回计算结果,在 Compose 中我们无法使用 [Debounce] 透传出结算结果,应该使用状态,而非
* [Debounce] 的返回值。 例如我们有一个计算函数,我们应该设置一个状态作为结果的保存。函数计算后的结果,通过调用对应的
* `setState(state:T)` 函数来传递。保证结算结果(状态)与计算解耦。 这样我们的[Debounce] 就可以无缝接入。
*/
@Deprecated(
"Please use the performance-optimized version. Do not pass the Options instance directly. You can simply switch by adding `=` after the `optionsOf` function. If you need to use an older version, you need to explicitly declare the parameters as `options`"
)
@Composable
fun useDebounceFn(fn: VoidFunction, options: DebounceOptions = remember { DebounceOptions() }): VoidFunction {
private fun useDebounceFn(fn: VoidFunction, options: DebounceOptions = remember { DebounceOptions() }): VoidFunction {
val latestFn by useLatestState(value = fn)
val scope = rememberCoroutineScope()
val debounced = remember {
Expand All @@ -127,14 +132,7 @@ fun useDebounceFn(fn: VoidFunction, options: DebounceOptions = remember { Deboun
}

@Composable
fun useDebounceFn(fn: VoidFunction, optionsOf: DebounceOptions.() -> Unit): VoidFunction =
useDebounceFn(fn, remember { DebounceOptions.optionOf(optionsOf) })

@Deprecated(
"Please use the performance-optimized version. Do not pass the Options instance directly. You can simply switch by adding `=` after the `optionsOf` function. If you need to use an older version, you need to explicitly declare the parameters as `options`"
)
@Composable
fun useDebounceEffect(vararg keys: Any?, options: DebounceOptions = remember { DebounceOptions() }, block: SuspendAsyncFn) {
private fun useDebounceEffect(vararg keys: Any?, options: DebounceOptions = remember { DebounceOptions() }, block: SuspendAsyncFn) {
val debouncedBlock = useDebounceFn(fn = { params ->
(params[0] as CoroutineScope).launch {
this.block()
Expand All @@ -145,10 +143,3 @@ fun useDebounceEffect(vararg keys: Any?, options: DebounceOptions = remember { D
debouncedBlock(scope)
}
}

@Composable
fun useDebounceEffect(vararg keys: Any?, optionsOf: DebounceOptions.() -> Unit, block: SuspendAsyncFn) = useDebounceEffect(
keys = keys,
remember { DebounceOptions.optionOf(optionsOf) },
block
)
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,28 @@ private class Interval(private val options: IntervalOptions) {
}
}

@Deprecated(
"Please use the performance-optimized version. Do not pass the Options instance directly. You can simply switch by adding `=` after the `optionsOf` function. If you need to use an older version, you need to explicitly declare the parameters as `options`"
@Composable
fun useInterval(optionsOf: IntervalOptions.() -> Unit = {}, block: () -> Unit): IntervalHolder = useInterval(
options = remember { IntervalOptions.optionOf(optionsOf) },
block = block
)

@Composable
fun useInterval(optionsOf: IntervalOptions.() -> Unit = {}, ready: Boolean, block: () -> Unit) = useInterval(
remember { IntervalOptions.optionOf(optionsOf) },
ready = ready,
block = block
)

@Stable
data class IntervalHolder(
val resume: ResumeFn,
val pause: PauseFn,
val isActive: IsActive,
)

@Composable
fun useInterval(options: IntervalOptions = remember { IntervalOptions() }, block: () -> Unit): IntervalHolder {
private fun useInterval(options: IntervalOptions = remember { IntervalOptions() }, block: () -> Unit): IntervalHolder {
val latestFn = useLatestRef(value = block)
val isActiveState = useState(default = false)
val scope = rememberCoroutineScope()
Expand All @@ -101,16 +118,7 @@ fun useInterval(options: IntervalOptions = remember { IntervalOptions() }, block
}

@Composable
fun useInterval(optionsOf: IntervalOptions.() -> Unit, block: () -> Unit): IntervalHolder = useInterval(
options = remember { IntervalOptions.optionOf(optionsOf) },
block = block
)

@Deprecated(
"Please use the performance-optimized version. Do not pass the Options instance directly. You can simply switch by adding `=` after the `optionsOf` function. If you need to use an older version, you need to explicitly declare the parameters as `options`"
)
@Composable
fun useInterval(options: IntervalOptions = remember { IntervalOptions() }, ready: Boolean, block: () -> Unit) {
private fun useInterval(options: IntervalOptions = remember { IntervalOptions() }, ready: Boolean, block: () -> Unit) {
val latestFn = useLatestRef(value = block)
val scope = rememberCoroutineScope()
val interval = remember {
Expand All @@ -130,17 +138,3 @@ fun useInterval(options: IntervalOptions = remember { IntervalOptions() }, ready
}
}
}

@Composable
fun useInterval(optionsOf: IntervalOptions.() -> Unit, ready: Boolean, block: () -> Unit) = useInterval(
remember { IntervalOptions.optionOf(optionsOf) },
ready = ready,
block = block
)

@Stable
data class IntervalHolder(
val resume: ResumeFn,
val pause: PauseFn,
val isActive: IsActive,
)
19 changes: 8 additions & 11 deletions hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/useNow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ data class UseNowOptions(
companion object : Options<UseNowOptions>(::UseNowOptions)
}

@Deprecated(
"Please use the performance-optimized version. Do not pass the Options instance directly. You can simply switch by adding `=` after the `optionsOf` function. If you need to use an older version, you need to explicitly declare the parameters as `options`"
)
@Composable
fun useNow(options: UseNowOptions = remember { UseNowOptions() }): State<String> {
fun useNow(optionsOf: UseNowOptions.() -> Unit = {}) = useNow(remember { UseNowOptions.optionOf(optionsOf) })

internal fun Long.toLocalDateTime(timeZone: TimeZone = TimeZone.currentSystemDefault()) =
Instant.fromEpochMilliseconds(this).toLocalDateTime(timeZone)

@Composable
private fun useNow(options: UseNowOptions = remember { UseNowOptions() }): State<String> {
val (interval, format) = with(options) { Pair(interval, format) }
val sdfRef = remember {
LocalDateTime.Format {
Expand All @@ -51,7 +54,7 @@ fun useNow(options: UseNowOptions = remember { UseNowOptions() }): State<String>
}
}
val (time) = useTimestamp(
TimestampOptions.optionOf {
optionsOf = {
this.interval = interval
}
)
Expand All @@ -60,9 +63,3 @@ fun useNow(options: UseNowOptions = remember { UseNowOptions() }): State<String>
}
return date
}

@Composable
fun useNow(optionsOf: UseNowOptions.() -> Unit) = useNow(remember { UseNowOptions.optionOf(optionsOf) })

internal fun Long.toLocalDateTime(timeZone: TimeZone = TimeZone.currentSystemDefault()) =
Instant.fromEpochMilliseconds(this).toLocalDateTime(timeZone)
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,23 @@ internal class Throttle(
}
}

@Deprecated(
"Please use the performance-optimized version. Do not pass the Options instance directly. You can simply switch by adding `=` after the `optionsOf` function. If you need to use an older version, you need to explicitly declare the parameters as `options`"
@Composable
fun <S> useThrottle(value: S, optionsOf: ThrottleOptions.() -> Unit = {}): State<S> =
useThrottle(value, remember { ThrottleOptions.optionOf(optionsOf) })

@Composable
fun useThrottleFn(fn: VoidFunction, optionsOf: ThrottleOptions.() -> Unit = {}): VoidFunction =
useThrottleFn(fn, remember { ThrottleOptions.optionOf(optionsOf) })

@Composable
fun useThrottleEffect(vararg keys: Any?, optionsOf: ThrottleOptions.() -> Unit = {}, block: SuspendAsyncFn) = useThrottleEffect(
keys = keys,
remember { ThrottleOptions.optionOf(optionsOf) },
block = block
)

@Composable
fun <S> useThrottle(value: S, options: ThrottleOptions = remember { ThrottleOptions() }): State<S> {
private fun <S> useThrottle(value: S, options: ThrottleOptions = remember { ThrottleOptions() }): State<S> {
val (throttled, setThrottled) = _useGetState(value)
val throttledSet = useThrottleFn(fn = {
setThrottled(value)
Expand All @@ -96,14 +108,7 @@ fun <S> useThrottle(value: S, options: ThrottleOptions = remember { ThrottleOpti
}

@Composable
fun <S> useThrottle(value: S, optionsOf: ThrottleOptions.() -> Unit): State<S> =
useThrottle(value, remember { ThrottleOptions.optionOf(optionsOf) })

@Deprecated(
"Please use the performance-optimized version. Do not pass the Options instance directly. You can simply switch by adding `=` after the `optionsOf` function. If you need to use an older version, you need to explicitly declare the parameters as `options`"
)
@Composable
fun useThrottleFn(fn: VoidFunction, options: ThrottleOptions = remember { ThrottleOptions() }): VoidFunction {
private fun useThrottleFn(fn: VoidFunction, options: ThrottleOptions = remember { ThrottleOptions() }): VoidFunction {
val latestFn by useLatestState(value = fn)
val scope = rememberCoroutineScope()
val throttled = remember {
Expand All @@ -113,14 +118,7 @@ fun useThrottleFn(fn: VoidFunction, options: ThrottleOptions = remember { Thrott
}

@Composable
fun useThrottleFn(fn: VoidFunction, optionsOf: ThrottleOptions.() -> Unit): VoidFunction =
useThrottleFn(fn, remember { ThrottleOptions.optionOf(optionsOf) })

@Deprecated(
"Please use the performance-optimized version. Do not pass the Options instance directly. You can simply switch by adding `=` after the `optionsOf` function. If you need to use an older version, you need to explicitly declare the parameters as `options`"
)
@Composable
fun useThrottleEffect(vararg keys: Any?, options: ThrottleOptions = remember { ThrottleOptions() }, block: SuspendAsyncFn) {
private fun useThrottleEffect(vararg keys: Any?, options: ThrottleOptions = remember { ThrottleOptions() }, block: SuspendAsyncFn) {
val throttledBlock = useThrottleFn(fn = { params ->
(params[0] as CoroutineScope).launch {
this.block()
Expand All @@ -131,10 +129,3 @@ fun useThrottleEffect(vararg keys: Any?, options: ThrottleOptions = remember { T
throttledBlock(scope)
}
}

@Composable
fun useThrottleEffect(vararg keys: Any?, optionsOf: ThrottleOptions.() -> Unit, block: SuspendAsyncFn) = useThrottleEffect(
keys = keys,
remember { ThrottleOptions.optionOf(optionsOf) },
block = block
)
Loading

0 comments on commit 190fb9f

Please sign in to comment.