Coverage Summary for Class: RemoteClientState (kweb.client)
Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
RemoteClientState |
100%
(1/1)
|
66.7%
(4/6)
|
|
81.2%
(13/16)
|
86.2%
(156/181)
|
package kweb.client
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kweb.DebugInfo
import kweb.util.random
import java.time.Instant
import java.util.concurrent.ConcurrentHashMap
data class RemoteClientState(val id: String, @Volatile var clientConnection: ClientConnection,
val handlers: MutableMap<Int, (JsonElement) -> Unit> = HashMap(),
val onCloseHandlers : ConcurrentHashMap<Int, () -> Unit> = ConcurrentHashMap(),
val debugTokens: MutableMap<String, DebugInfo> = HashMap(), var lastModified: Instant = Instant.now()) {
fun send(message: Server2ClientMessage) {
clientConnection.send(Json.encodeToString(message))
}
fun addCloseHandler(handler: () -> Unit) : Int {
synchronized(this) {
val id = random.nextInt()
onCloseHandlers[id] = handler
return id
}
}
fun removeCloseHandler(id: Int) {
synchronized(this) {
onCloseHandlers.remove(id)
}
}
fun triggerCloseListeners() {
synchronized(this) {
onCloseHandlers.values.forEach { it() }
onCloseHandlers.clear()
}
}
override fun toString() = "Remote2ClientState(id = $id)"
}