From 81b0ac83ec8d569ecbaa87ef394cc775628c7400 Mon Sep 17 00:00:00 2001 From: Renetik Date: Sat, 22 Jun 2024 22:40:13 +0800 Subject: [PATCH] ... --- .../event/registration/CSHasChangeValue+.kt | 88 +++---------------- .../registration/CSHasChangeValue+Boolean.kt | 58 ++++++++++++ 2 files changed, 71 insertions(+), 75 deletions(-) create mode 100644 library/src/main/java/renetik/android/event/registration/CSHasChangeValue+Boolean.kt diff --git a/library/src/main/java/renetik/android/event/registration/CSHasChangeValue+.kt b/library/src/main/java/renetik/android/event/registration/CSHasChangeValue+.kt index 1d1fa4b..067ef6e 100644 --- a/library/src/main/java/renetik/android/event/registration/CSHasChangeValue+.kt +++ b/library/src/main/java/renetik/android/event/registration/CSHasChangeValue+.kt @@ -1,51 +1,33 @@ package renetik.android.event.registration import kotlinx.coroutines.suspendCancellableCoroutine -import renetik.android.core.kotlin.primitives.isFalse -import renetik.android.core.kotlin.primitives.isTrue import renetik.android.core.lang.ArgFunc import renetik.android.core.lang.value.CSValue -import renetik.android.core.lang.value.isFalse -import renetik.android.core.lang.value.isTrue import renetik.android.event.common.CSHasDestruct import renetik.android.event.common.destruct import renetik.android.event.common.update import renetik.android.event.property.CSLateProperty import renetik.android.event.property.CSProperty import renetik.android.event.property.CSProperty.Companion.property -import renetik.android.event.registration.CSHasChangeValue.Companion.delegate import renetik.android.event.registration.CSRegistration.Companion.CSRegistration import kotlin.Result.Companion.success -suspend fun CSHasChangeValue.waitFor(condition: (T) -> Boolean) { - while (!condition(value)) waitForChange() -} +//suspend fun CSHasChangeValue.waitFor(condition: (T) -> Boolean) { +// while (!condition(value)) waitForChange() +//} -suspend fun CSHasChangeValue.waitIsTrue(): Unit = suspendCancellableCoroutine { - if (isTrue) it.resumeWith(success(Unit)) - else { - var registration: CSRegistration? = null - registration = onTrue { - registration?.cancel() - registration = null - it.resumeWith(success(Unit)) - } - it.invokeOnCancellation { registration?.cancel() } - } -} - -suspend fun CSHasChangeValue.waitIsFalse(): Unit = suspendCancellableCoroutine { - if (isFalse) it.resumeWith(success(Unit)) - else { +suspend fun CSHasChangeValue.waitFor(condition: (T) -> Boolean) { + if (!condition(value)) suspendCancellableCoroutine { coroutine -> var registration: CSRegistration? = null - registration = onFalse { - registration?.cancel() - registration = null - it.resumeWith(success(Unit)) + registration = onChange { + if (condition(value)) { + registration?.cancel() + registration = null + coroutine.resumeWith(success(Unit)) + } } - it.invokeOnCancellation { registration?.cancel() } + coroutine.invokeOnCancellation { registration?.cancel() } } - } fun CSHasChangeValue.onValue(function: (T) -> Unit) { @@ -74,23 +56,6 @@ fun CSHasChangeValue.actionFromTo( return onChange { function(value, it); value = it } } -fun CSHasChangeValue.onFalse(function: () -> Unit) = - onChange { if (it.isFalse) function() } - - -fun CSHasChangeValue.onTrue(function: () -> Unit) = - onChange { if (it.isTrue) function() } - -fun CSHasChangeValue.actionTrue(function: () -> Unit): CSRegistration { - if (isTrue) function() - return onTrue(function) -} - -fun CSHasChangeValue.actionFalse(function: () -> Unit): CSRegistration { - if (isFalse) function() - return onFalse(function) -} - fun CSHasChangeValue.onNull(function: () -> Unit) = onChange { if (it == null) function() } @@ -106,11 +71,6 @@ fun CSHasChangeValue.actionNotNull(function: () -> Unit) = fun CSHasChangeValue.actionNotNull(function: (T) -> Unit) = action { if (it != null) function(it) } -operator fun CSHasChangeValue.not() = delegate(from = { !it }) - -operator fun CSHasChangeValue.plus(other: CSHasChangeValue) = - (this to other).delegate(from = { first, second -> first && second }) - inline fun CSHasChangeValue.onChangeTo( value: Value, crossinline onChange: () -> Unit ): CSRegistration = onChange { if (this.value == value) onChange() } @@ -255,26 +215,4 @@ fun CSHasRegistrations.lazyFactory( } return instance!! } -} - -// I don't even understand this now... and not used. -//fun CSHasRegistrations.lazyHasChangeValue( -// property: () -> CSHasChangeValue, -// createInstance: (V) -> Instance -//): CSHasChangeValue where Instance : CSHasDestruct { -// var instance: Instance? = null -// return object : CSHasChangeValue { -// val outputModelInstance1: CSHasChangeValue by lazy { -// property().hasChangeValue(this@lazyHasChangeValue, from = { -// createInstance(it).also { -// instance?.destruct() -// instance = it -// } -// }) -// } -// override val value: Instance get() = outputModelInstance1.value -// -// override fun onChange(function: (Instance) -> Unit): CSRegistration = -// outputModelInstance1.onChange(function) -// } -//} \ No newline at end of file +} \ No newline at end of file diff --git a/library/src/main/java/renetik/android/event/registration/CSHasChangeValue+Boolean.kt b/library/src/main/java/renetik/android/event/registration/CSHasChangeValue+Boolean.kt new file mode 100644 index 0000000..af66b0b --- /dev/null +++ b/library/src/main/java/renetik/android/event/registration/CSHasChangeValue+Boolean.kt @@ -0,0 +1,58 @@ +package renetik.android.event.registration + +import kotlinx.coroutines.suspendCancellableCoroutine +import renetik.android.core.kotlin.primitives.isFalse +import renetik.android.core.kotlin.primitives.isTrue +import renetik.android.core.lang.value.isFalse +import renetik.android.core.lang.value.isTrue +import renetik.android.event.registration.CSHasChangeValue.Companion.delegate +import kotlin.Result.Companion.success + +suspend fun CSHasChangeValue.waitIsTrue() { + if (isFalse) suspendCancellableCoroutine { + var registration: CSRegistration? = null + registration = onTrue { + registration?.cancel() + registration = null + it.resumeWith(success(Unit)) + } + it.invokeOnCancellation { registration?.cancel() } + } +} + +suspend fun CSHasChangeValue.waitIsFalse() { + if (isTrue) suspendCancellableCoroutine { + var registration: CSRegistration? = null + registration = onFalse { + registration?.cancel() + registration = null + it.resumeWith(success(Unit)) + } + it.invokeOnCancellation { registration?.cancel() } + } +} + +fun CSHasChangeValue.onFalse(function: () -> Unit) = + onChange { if (it.isFalse) function() } + + +fun CSHasChangeValue.onTrue(function: () -> Unit) = + onChange { if (it.isTrue) function() } + +fun CSHasChangeValue.actionTrue(function: () -> Unit): CSRegistration { + if (isTrue) function() + return onTrue(function) +} + +fun CSHasChangeValue.actionFalse(function: () -> Unit): CSRegistration { + if (isFalse) function() + return onFalse(function) +} + +operator fun CSHasChangeValue.not() = delegate(from = { !it }) + +infix fun CSHasChangeValue.and(other: CSHasChangeValue) = + (this to other).delegate(from = { first, second -> first && second }) + +infix fun CSHasChangeValue.or(other: CSHasChangeValue) = + (this to other).delegate(from = { first, second -> first || second }) \ No newline at end of file