11package kweb.state
22
3- import com.google.common.cache.Cache
4- import com.google.common.cache.CacheBuilder
53import kweb.util.random
64import mu.two.KotlinLogging
75import 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
0 commit comments