From cf587e9c690246783dbbefdf644482fd2731f4ea Mon Sep 17 00:00:00 2001 From: Ian Clarke Date: Fri, 26 May 2023 17:30:40 -0500 Subject: [PATCH] shouldn't --- api/kweb-core.api | 2 +- src/main/kotlin/kweb/state/KVal.kt | 16 ++++++---------- src/main/kotlin/kweb/state/KVar.kt | 2 +- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/api/kweb-core.api b/api/kweb-core.api index 48f80355b..9ae282926 100644 --- a/api/kweb-core.api +++ b/api/kweb-core.api @@ -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 diff --git a/src/main/kotlin/kweb/state/KVal.kt b/src/main/kotlin/kweb/state/KVal.kt index fc4a6ee91..7a212c8f2 100755 --- a/src/main/kotlin/kweb/state/KVal.kt +++ b/src/main/kotlin/kweb/state/KVal.kt @@ -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 @@ -20,11 +18,7 @@ open class KVal(value: T) : AutoCloseable{ internal val isClosed get() = closeReason != null - protected val listeners : Cache Unit> = CacheBuilder.newBuilder() - // A listener shouldn't cause data to be retained that wouldn't otherwise be - // retained - .softValues() - .build() + protected val listeners = ConcurrentHashMap Unit>() private val closeHandlers = ConcurrentLinkedDeque<() -> Unit>() /** @@ -33,7 +27,7 @@ open class KVal(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 } @@ -56,7 +50,7 @@ open class KVal(value: T) : AutoCloseable{ * changes. */ fun removeListener(handle: Long) { - listeners.invalidate(handle) + listeners.remove(handle) } /** @@ -76,7 +70,7 @@ open class KVal(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) { @@ -109,7 +103,9 @@ open class KVal(value: T) : AutoCloseable{ fun close(reason: CloseReason) { if (!isClosed) { closeReason = reason + listeners.clear() closeHandlers.forEach { it.invoke() } + closeHandlers.clear() } } diff --git a/src/main/kotlin/kweb/state/KVar.kt b/src/main/kotlin/kweb/state/KVar.kt index b98ab8148..cc28603ad 100755 --- a/src/main/kotlin/kweb/state/KVar.kt +++ b/src/main/kotlin/kweb/state/KVar.kt @@ -28,7 +28,7 @@ class KVar(initialValue: T) : KVal(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) {