Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
renetik committed Jun 25, 2024
1 parent 23dc646 commit 50d830e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ import kotlin.properties.Delegates.notNull
interface CSHasChangeValue<T> : CSValue<T>, CSHasChange<T> {
companion object {

@Deprecated("Use hasChangeValue")
class DelegateValue<Return>(
var value: Return, val onChange: ArgFunc<Return>?,
val function: (Return) -> Unit
) {
operator fun invoke(newValue: Return) {
if (value != newValue) {
value = newValue
onChange?.invoke(newValue)
function(newValue)
}
}
}

fun <T> CSHasChangeValue<T>.delegate(
parent: CSHasRegistrations? = null, onChange: ArgFunc<T>? = null,
): CSHasChangeValue<T> = delegate(parent, from = { it }, onChange)

@Deprecated("Use hasChangeValue")
inline fun <T, Return> CSHasChangeValue<T>.delegate(
parent: CSHasRegistrations? = null,
crossinline from: (T) -> Return,
Expand All @@ -31,20 +42,12 @@ interface CSHasChangeValue<T> : CSValue<T>, CSHasChange<T> {
object : CSHasChangeValue<Return> {
override val value: Return get() = from(property.value)
override fun onChange(function: (Return) -> Unit): CSRegistration {
var value: Return = value
return property.onChange {
val newValue = from(it)
if (value != newValue) {
value = newValue
onChange?.invoke(newValue)
function(newValue)
}
}.registerTo(parent)
val value = DelegateValue(value, onChange, function)
return property.onChange { value(from(it)) }.registerTo(parent)
}
}
}

@Deprecated("Use hasChangeValue")
inline fun <T, V, Return> Pair<
CSHasChangeValue<T>, CSHasChangeValue<V>
>.delegate(
Expand All @@ -54,29 +57,14 @@ interface CSHasChangeValue<T> : CSValue<T>, CSHasChange<T> {
): CSHasChangeValue<Return> = object : CSHasChangeValue<Return> {
override val value: Return get() = from(first.value, second.value)
override fun onChange(function: (Return) -> Unit): CSRegistration {
var value: Return = value
val value = DelegateValue(value, onChange, function)
return CSRegistration(
first.onChange {
val newValue = from(it, second.value)
if (value != newValue) {
value = newValue
onChange?.invoke(newValue)
function(newValue)
}
}.registerTo(parent),
second.onChange {
val newValue = from(first.value, it)
if (value != newValue) {
value = newValue
onChange?.invoke(newValue)
function(newValue)
}
}.registerTo(parent),
)
first.onChange { value(from(it, second.value)) },
second.onChange { value(from(first.value, it)) },
).registerTo(parent)
}
}

@Deprecated("Use hasChangeValue")
inline fun <T, V, K, Return>
Triple<CSHasChangeValue<T>,
CSHasChangeValue<V>,
Expand All @@ -86,30 +74,18 @@ interface CSHasChangeValue<T> : CSValue<T>, CSHasChange<T> {
noinline onChange: ArgFunc<Return>? = null,
): CSHasChangeValue<Return> = object : CSHasChangeValue<Return> {
override val value: Return
get() = from(
first.value, second.value, third.value
)
get() = from(first.value, second.value, third.value)

override fun onChange(function: (Return) -> Unit) = CSRegistration(
first.onChange {
val value = from(it, second.value, third.value)
onChange?.invoke(value)
function(value)
}.registerTo(parent),
second.onChange {
val value = from(first.value, it, third.value)
onChange?.invoke(value)
function(value)
}.registerTo(parent),
third.onChange {
val value = from(first.value, second.value, it)
onChange?.invoke(value)
function(value)
}.registerTo(parent)
)
override fun onChange(function: (Return) -> Unit): CSRegistration {
val value = DelegateValue(value, onChange, function)
return CSRegistration(
first.onChange { value(from(it, second.value, third.value)) },
second.onChange { value(from(first.value, it, third.value)) },
third.onChange { value(from(first.value, second.value, it)) }
).registerTo(parent)
}
}

@Deprecated("Use hasChangeValue")
@JvmName("delegateChild")
inline fun <ParentValue, ChildValue>
CSHasChangeValue<ParentValue>.delegate(
Expand All @@ -120,33 +96,22 @@ interface CSHasChangeValue<T> : CSValue<T>, CSHasChange<T> {
object : CSHasChangeValue<ChildValue> {
override val value: ChildValue get() = child(property.value).value
override fun onChange(function: (ChildValue) -> Unit): CSRegistration {
val value = DelegateValue(value, onChange, function)
var childRegistration: CSRegistration? = null
var isPaused = false
val parentRegistration = property.action { parentValue ->
childRegistration?.cancel()
val childItem = child(parentValue)
if (childRegistration != null) childItem.also {
if (!isPaused) {
onChange?.invoke(it.value)
function(it.value)
}
}
childRegistration = childItem.onChange { childValue ->
if (!isPaused) {
onChange?.invoke(childValue)
function(childValue)
}
}
if (childRegistration != null) childItem.also { value(it.value) }
childRegistration = childItem.onChange(value::invoke)
}
return CSRegistration(isActive = true,
onPause = { isPaused = true }, onResume = { isPaused = false },
onCancel = { parentRegistration.cancel(); childRegistration?.cancel() })
.registerTo(parent)
return CSRegistration(
{ parentRegistration }, { childRegistration }
).registerTo(parent)
}
}
}

@Deprecated("Use hasChangeValue")

@JvmName("delegateNullable")
inline fun <ParentValue, ChildValue>
CSHasChangeValue<ParentValue>.delegateNullable(
Expand All @@ -157,30 +122,19 @@ interface CSHasChangeValue<T> : CSValue<T>, CSHasChange<T> {
object : CSHasChangeValue<ChildValue?> {
override val value: ChildValue? get() = child(property.value)?.value
override fun onChange(function: (ChildValue?) -> Unit): CSRegistration {
val value = DelegateValue(value, onChange, function)
var childRegistration: CSRegistration? = null
var isInitialized = false
var isPaused = false
val parentRegistration = property.action { parentValue ->
childRegistration?.cancel()
val childItem = child(parentValue)
if (isInitialized) childItem.also {
if (!isPaused) {
onChange?.invoke(it?.value)
function(it?.value)
}
}
if (isInitialized) childItem.also { value(it?.value) }
isInitialized = true
childRegistration = childItem?.onChange { childValue ->
if (!isPaused) {
onChange?.invoke(childValue)
function(childValue)
}
}
childRegistration = childItem?.onChange(value::invoke)
}
return CSRegistration(isActive = true,
onPause = { isPaused = true }, onResume = { isPaused = false },
onCancel = { parentRegistration.cancel(); childRegistration?.cancel() })
.registerTo(parent)
return CSRegistration(
{ parentRegistration }, { childRegistration }
).registerTo(parent)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ fun CSRegistration.registerTo(registrations: CSHasRegistrations?): CSRegistratio
fun CSHasRegistrations.register(registration: CSRegistration): CSRegistration =
registrations.register(registration)

//@AnyThread
//fun <Parent : CSHasRegistrations?, T : CSRegistration>
// Parent.register(registration: T): CSRegistration =
// this?.let { registrations.register(registration) } ?: registration

operator fun CSHasRegistrations.plus(registration: CSRegistration): CSRegistration =
register(registration)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import renetik.android.core.lang.Func
import renetik.android.event.registration.CSRegistration.Companion.CSRegistration
import java.io.Closeable

val CSRegistrationEmpty = object : CSRegistration {
override val isActive: Boolean = false
override val isCanceled: Boolean = false
override fun resume() = Unit
override fun pause() = Unit
override fun cancel() = Unit
}

fun CSRegistration(vararg registrations: CSRegistration?) =
CSRegistration(registrations.asList())

Expand All @@ -13,6 +21,14 @@ fun CSRegistration(registrations: List<CSRegistration?>) = CSRegistration(
onPause = { registrations.forEach { if (it?.isActive == true) it.pause() } },
onCancel = { registrations.forEach { it?.cancel() } })

fun CSRegistration(
vararg registrations: () -> CSRegistration?,
) = CSRegistration(isActive = true,
onPause = { registrations.forEach { it()?.pause() } },
onResume = { registrations.forEach { it()?.resume() } },
onCancel = { registrations.forEach { it()?.cancel() } }
)

inline fun List<CSRegistration>.paused(function: Func) {
onEach { it.pause() }
function()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ class CSRegistrationsMap(private val parent: Any) : CSRegistrations, CSHasRegist
override fun pause() = registration.pause()
override fun cancel() = cancel(registration)
}
// return CSRegistration(
// isActive = registration.isActive,
// onResume = { registration.resume() },
// onPause = { registration.pause() },
// onCancel = { cancel(registration) })
}

@Synchronized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import renetik.android.core.lang.value.CSValue.Companion.value
import renetik.android.core.lang.variable.plusAssign
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.CSHasChangeValue.Companion.delegateNullable
import renetik.android.event.registration.CSHasChangeValue.Companion.hasChangeValue
import renetik.android.event.registration.CSHasChangeValue.Companion.hasChangeValueNullable
import renetik.android.testing.CSAssert.assert
Expand All @@ -14,7 +16,7 @@ class CSHasChangeValueTest {
@Test
fun delegate() {
val property = property(0)
val isRecorded = property.hasChangeValue(from = { it > 1 })
val isRecorded = property.delegate(from = { it > 1 })
val isRecordedUser1 = isRecorded.hasChangeValue(from = { "$it" })
val isRecordedUser2 = isRecorded.hasChangeValue(from = { "$it" })
assert(expected = false, actual = isRecorded.value)
Expand All @@ -29,7 +31,7 @@ class CSHasChangeValueTest {
@Test
fun delegateChild() {
val property = property<CSValue<CSProperty<Int>>>(value(property(5)))
val delegateChild = property.hasChangeValue(child = { it.value })
val delegateChild = property.delegate(child = { it.value })
testDelegateChildProperty(property, delegateChild)
}

Expand Down Expand Up @@ -61,7 +63,7 @@ class CSHasChangeValueTest {
@Test
fun delegateNullableChild() {
val property = property<CSValue<CSProperty<Int>>?>(null)
val delegateChild = property.hasChangeValueNullable(child = { it?.value })
val delegateChild = property.delegateNullable(child = { it?.value })
testDelegateNullableChildProperty(property, delegateChild)
}

Expand All @@ -76,20 +78,25 @@ class CSHasChangeValueTest {
property: CSProperty<CSValue<CSProperty<Int>>?>,
delegateChild: CSHasChangeValue<Int?>
) {
var delegateChildValue1: Int? = null
var delegateChildValue2: Int? = null
delegateChild.onChange { delegateChildValue1 = it }
delegateChild.action { delegateChildValue2 = it }
assert(expected = null, actual = delegateChildValue1)
assert(expected = null, actual = delegateChildValue2)
var delegateChildOnChangeValue: Int? = null
var delegateChildActionValue: Int? = null
val delegateChildOnChange = delegateChild.onChange { delegateChildOnChangeValue = it }
delegateChild.action { delegateChildActionValue = it }
assert(expected = null, actual = delegateChildOnChangeValue)
assert(expected = null, actual = delegateChildActionValue)
property.value = value(property(7))
assert(expected = 7, actual = delegateChildValue1)
assert(expected = 7, actual = delegateChildValue2)
assert(expected = 7, actual = delegateChildOnChangeValue)
assert(expected = 7, actual = delegateChildActionValue)
property.value?.value?.value = 6
assert(expected = 6, actual = delegateChildValue1)
assert(expected = 6, actual = delegateChildValue2)
assert(expected = 6, actual = delegateChildOnChangeValue)
assert(expected = 6, actual = delegateChildActionValue)
delegateChildOnChange.pause()
property.value?.value?.value = 5
assert(expected = 6, actual = delegateChildOnChangeValue)
assert(expected = 5, actual = delegateChildActionValue)
delegateChildOnChange.resume()
property.value = null
assert(expected = null, actual = delegateChildValue1)
assert(expected = null, actual = delegateChildValue2)
assert(expected = null, actual = delegateChildOnChangeValue)
assert(expected = null, actual = delegateChildActionValue)
}
}

0 comments on commit 50d830e

Please sign in to comment.