diff --git a/debugger/server/src/commonMain/kotlin/pro/respawn/flowmvi/debugger/server/DebugServer.kt b/debugger/server/src/commonMain/kotlin/pro/respawn/flowmvi/debugger/server/DebugServer.kt index a37ffcb9..143bd697 100644 --- a/debugger/server/src/commonMain/kotlin/pro/respawn/flowmvi/debugger/server/DebugServer.kt +++ b/debugger/server/src/commonMain/kotlin/pro/respawn/flowmvi/debugger/server/DebugServer.kt @@ -34,37 +34,40 @@ internal object DebugServer : Container private var server: EmbeddedServer<*, *>? by atomic(null) private val logger = PlatformStoreLogger - fun start(host: String, port: Int) = embeddedServer(Netty, port = port, host = host) { - configureDebugServer() - // store will be started / closed along with the server - store.start(this) - store.intent(ServerStarted) - routing { - get("/") { call.respondText("FlowMVI Debugger Online", null) } - webSocket("/{id}") { - val storeId = call.parameters.getOrFail("id").asUUID - with(store) { - try { - subscribe { - actions - .filterIsInstance() - .filter { it.client == storeId } - .collect { sendSerialized(it.event) } + fun start(host: String, port: Int) { + if (store.isActive) return + embeddedServer(Netty, port = port, host = host) { + configureDebugServer() + // store will be started / closed along with the server + store.start(this) + store.intent(ServerStarted) + routing { + get("/") { call.respondText("FlowMVI Debugger Online", null) } + webSocket("/{id}") { + val storeId = call.parameters.getOrFail("id").asUUID + with(store) { + try { + subscribe { + actions + .filterIsInstance() + .filter { it.client == storeId } + .collect { sendSerialized(it.event) } + } + while (true) { + val event = receiveDeserialized() + intent(EventReceived(event, storeId)) + } + } finally { + logger(StoreLogLevel.Debug) { "Store $storeId disconnected" } + intent(EventReceived(StoreDisconnected(storeId), storeId)) } - while (true) { - val event = receiveDeserialized() - intent(EventReceived(event, storeId)) - } - } finally { - logger(StoreLogLevel.Debug) { "Store $storeId disconnected" } - intent(EventReceived(StoreDisconnected(storeId), storeId)) } } } } + .also { server = it } + .start() } - .also { server = it } - .start() suspend fun stop() = withContext(Dispatchers.IO) { store.intent(StopRequested)