Skip to content

Commit

Permalink
fix: don't start server if it is already running
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek-12 committed Nov 5, 2024
1 parent 17d9633 commit 57a737b
Showing 1 changed file with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,40 @@ internal object DebugServer : Container<ServerState, ServerIntent, ServerAction>
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<SendClientEvent>()
.filter { it.client == storeId }
.collect { sendSerialized<ServerEvent>(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<SendClientEvent>()
.filter { it.client == storeId }
.collect { sendSerialized<ServerEvent>(it.event) }
}
while (true) {
val event = receiveDeserialized<ClientEvent>()
intent(EventReceived(event, storeId))
}
} finally {
logger(StoreLogLevel.Debug) { "Store $storeId disconnected" }
intent(EventReceived(StoreDisconnected(storeId), storeId))
}
while (true) {
val event = receiveDeserialized<ClientEvent>()
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)
Expand Down

0 comments on commit 57a737b

Please sign in to comment.