Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
shouldn't
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed May 26, 2023
1 parent a3657a9 commit cf587e9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api/kweb-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ public class kweb/state/KVal : java/lang/AutoCloseable {
public final fun close (Lkweb/state/CloseReason;)V
protected final fun finalize ()V
protected final fun getCloseReason ()Lkweb/state/CloseReason;
protected final fun getListeners ()Lcom/google/common/cache/Cache;
protected final fun getListeners ()Ljava/util/concurrent/ConcurrentHashMap;
public fun getValue ()Ljava/lang/Object;
public final fun map (Lkotlin/jvm/functions/Function1;)Lkweb/state/KVal;
public final fun onClose (Lkotlin/jvm/functions/Function0;)V
Expand Down
16 changes: 6 additions & 10 deletions src/main/kotlin/kweb/state/KVal.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package kweb.state

import com.google.common.cache.Cache
import com.google.common.cache.CacheBuilder
import kweb.util.random
import mu.two.KotlinLogging
import java.util.concurrent.ConcurrentHashMap
Expand All @@ -20,11 +18,7 @@ open class KVal<T : Any?>(value: T) : AutoCloseable{

internal val isClosed get() = closeReason != null

protected val listeners : Cache<Long, (T, T) -> Unit> = CacheBuilder.newBuilder()
// A listener shouldn't cause data to be retained that wouldn't otherwise be
// retained
.softValues()
.build()
protected val listeners = ConcurrentHashMap<Long, (T, T) -> Unit>()
private val closeHandlers = ConcurrentLinkedDeque<() -> Unit>()

/**
Expand All @@ -33,7 +27,7 @@ open class KVal<T : Any?>(value: T) : AutoCloseable{
fun addListener(listener: (T, T) -> Unit): Long {
verifyNotClosed("add a listener")
val handle = random.nextLong()
listeners.put(handle, listener)
listeners[handle] = listener
return handle
}

Expand All @@ -56,7 +50,7 @@ open class KVal<T : Any?>(value: T) : AutoCloseable{
* changes.
*/
fun removeListener(handle: Long) {
listeners.invalidate(handle)
listeners.remove(handle)
}

/**
Expand All @@ -76,7 +70,7 @@ open class KVal<T : Any?>(value: T) : AutoCloseable{
logger.debug("Updating mapped $value to $new")
val mappedValue = mapper(new)
mappedKVal.pValue = mappedValue
mappedKVal.listeners.asMap().values.forEach { listener ->
mappedKVal.listeners.values.forEach { listener ->
try {
val mappedOld = mapper(old)
if (mappedOld != mappedValue) {
Expand Down Expand Up @@ -109,7 +103,9 @@ open class KVal<T : Any?>(value: T) : AutoCloseable{
fun close(reason: CloseReason) {
if (!isClosed) {
closeReason = reason
listeners.clear()
closeHandlers.forEach { it.invoke() }
closeHandlers.clear()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/kweb/state/KVar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class KVar<T : Any?>(initialValue: T) : KVal<T>(initialValue) {
override var value: T by Delegates.observable(initialValue) { _, old, new ->
if (old != new) {
verifyNotClosed("modify KVar.value")
listeners.asMap().values.forEach { listener ->
listeners.values.forEach { listener ->
try {
listener(old, new)
} catch (e: Exception) {
Expand Down

0 comments on commit cf587e9

Please sign in to comment.