Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
renetik committed Jul 29, 2023
1 parent 5522cef commit e9ddf19
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package renetik.android.event.property

import renetik.android.event.CSEvent
import renetik.android.event.CSEvent.Companion.event
import renetik.android.event.common.CSHasDestruct
import renetik.android.event.common.CSModel
Expand All @@ -10,10 +9,10 @@ abstract class CSPropertyBase<T>(
val onChange: ((value: T) -> Unit)? = null
) : CSModel(parent), CSProperty<T> {

constructor(onApply: ((value: T) -> Unit)? = null)
: this(parent = null, onChange = onApply)
constructor(onChange: ((value: T) -> Unit)? = null)
: this(parent = null, onChange = onChange)

val eventChange by lazy<CSEvent<T>>(::event)
val eventChange by lazy { event<T>() }

override fun onChange(function: (T) -> Unit) = eventChange.listen(function)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package renetik.android.event.property
import renetik.android.core.lang.variable.CSSynchronizedVariable
import renetik.android.event.common.CSHasDestruct

interface CSSyncProperty<T> : CSSynchronizedVariable<T>, CSProperty<T>
interface CSSafeProperty<T> : CSSynchronizedVariable<T>, CSProperty<T>

fun <T> CSHasDestruct.safeProperty(
value: T, onChange: ((value: T) -> Unit)? = null
): CSSyncProperty<T> = CSSyncPropertyImpl(this, value, onChange)
): CSSafeProperty<T> = CSSafePropertyImpl(this, value, onChange)

fun <T> CSHasDestruct.safe(
property: CSProperty<T>, onChange: ((value: T) -> Unit)? = null
): CSSyncProperty<T> = CSSyncPropertyImpl(
): CSSafeProperty<T> = CSSafePropertyImpl(
this, property.value, onChange
).apply { connect(property) }

fun <T> CSProperty<T>.safe(
parent: CSHasDestruct
): CSSyncProperty<T> = parent.safe(this)
): CSSafeProperty<T> = parent.safe(this)
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package renetik.android.event.property
import renetik.android.event.common.CSHasDestruct
import renetik.android.event.util.CSLater.onMain

class CSSyncPropertyImpl<T>(
class CSSafePropertyImpl<T>(
parent: CSHasDestruct,
value: T, onChange: ((value: T) -> Unit)? = null
) : CSPropertyBase<T>(parent, onChange), CSSyncProperty<T> {
) : CSPropertyBase<T>(parent, onChange), CSSafeProperty<T> {

@get:Synchronized
private var field: T = value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package renetik.android.event.property

import renetik.android.event.common.CSHasDestruct
import renetik.android.event.util.CSLater.onMain
import java.lang.ref.WeakReference

class CSSafeWeakProperty<T>(
parent: CSHasDestruct,
value: T? = null, onChange: ((value: T?) -> Unit)? = null
) : CSPropertyBase<T?>(parent, onChange), CSSafeProperty<T?> {

private var _value: WeakReference<T>? = null

override fun value(newValue: T?, fire: Boolean): Unit = synchronized(this) {
if (_value?.get() == newValue) return
_value = WeakReference(newValue)
onMain { onValueChanged(newValue) }
}

@get:Synchronized
override var value: T?
get() = _value?.get()
set(value) = value(value)

init {
value?.let { this.value = it }
}

companion object {
fun <T> CSHasDestruct.safeWeakProperty(
value: T? = null, onChange: ((value: T?) -> Unit)? = null
): CSProperty<T?> = CSSafeWeakProperty(this, value, onChange)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package renetik.android.event.property

import java.lang.ref.WeakReference

class CSWeakProperty<T>(
value: T? = null, onChange: ((value: T?) -> Unit)? = null
) : CSPropertyBase<T?>(onChange) {

private var _value: WeakReference<T>? = null

override fun value(newValue: T?, fire: Boolean) {
if (_value?.get() == newValue) return
_value = WeakReference(newValue)
onValueChanged(newValue, fire)
}

override var value: T?
get() = _value?.get()
set(value) = value(value)

init {
value?.let { this.value = it }
}

companion object {
fun <T> weakProperty(
value: T? = null, onChange: ((value: T?) -> Unit)? = null
): CSProperty<T?> = CSWeakProperty(value, onChange)
}
}

0 comments on commit e9ddf19

Please sign in to comment.