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

Commit cf587e9

Browse files
committed
shouldn't
1 parent a3657a9 commit cf587e9

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

api/kweb-core.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,7 @@ public class kweb/state/KVal : java/lang/AutoCloseable {
18501850
public final fun close (Lkweb/state/CloseReason;)V
18511851
protected final fun finalize ()V
18521852
protected final fun getCloseReason ()Lkweb/state/CloseReason;
1853-
protected final fun getListeners ()Lcom/google/common/cache/Cache;
1853+
protected final fun getListeners ()Ljava/util/concurrent/ConcurrentHashMap;
18541854
public fun getValue ()Ljava/lang/Object;
18551855
public final fun map (Lkotlin/jvm/functions/Function1;)Lkweb/state/KVal;
18561856
public final fun onClose (Lkotlin/jvm/functions/Function0;)V

src/main/kotlin/kweb/state/KVal.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package kweb.state
22

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

2119
internal val isClosed get() = closeReason != null
2220

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

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

@@ -56,7 +50,7 @@ open class KVal<T : Any?>(value: T) : AutoCloseable{
5650
* changes.
5751
*/
5852
fun removeListener(handle: Long) {
59-
listeners.invalidate(handle)
53+
listeners.remove(handle)
6054
}
6155

6256
/**
@@ -76,7 +70,7 @@ open class KVal<T : Any?>(value: T) : AutoCloseable{
7670
logger.debug("Updating mapped $value to $new")
7771
val mappedValue = mapper(new)
7872
mappedKVal.pValue = mappedValue
79-
mappedKVal.listeners.asMap().values.forEach { listener ->
73+
mappedKVal.listeners.values.forEach { listener ->
8074
try {
8175
val mappedOld = mapper(old)
8276
if (mappedOld != mappedValue) {
@@ -109,7 +103,9 @@ open class KVal<T : Any?>(value: T) : AutoCloseable{
109103
fun close(reason: CloseReason) {
110104
if (!isClosed) {
111105
closeReason = reason
106+
listeners.clear()
112107
closeHandlers.forEach { it.invoke() }
108+
closeHandlers.clear()
113109
}
114110
}
115111

src/main/kotlin/kweb/state/KVar.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class KVar<T : Any?>(initialValue: T) : KVal<T>(initialValue) {
2828
override var value: T by Delegates.observable(initialValue) { _, old, new ->
2929
if (old != new) {
3030
verifyNotClosed("modify KVar.value")
31-
listeners.asMap().values.forEach { listener ->
31+
listeners.values.forEach { listener ->
3232
try {
3333
listener(old, new)
3434
} catch (e: Exception) {

0 commit comments

Comments
 (0)