From d77980e50e27af2bbabb76f933ae75c1ca92ceee Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Thu, 27 Jul 2023 17:59:06 +0300 Subject: [PATCH] desktop: prevent deadlock (#2785) * desktop: prevent deadlock * debug info --- .../kotlin/chat/simplex/common/model/ChatModel.kt | 9 +++++---- .../kotlin/chat/simplex/common/views/TerminalView.kt | 10 +++++----- apps/multiplatform/desktop/build.gradle.kts | 8 ++++++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt index e0f1e773c..220d71758 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt @@ -56,7 +56,7 @@ object ChatModel { val chatItems = mutableStateListOf() val groupMembers = mutableStateListOf() - val terminalItems = mutableStateListOf() + val terminalItems = mutableStateOf(emptyList()) val userAddress = mutableStateOf(null) // Allows to temporary save servers that are being edited on multiple screens val userSMPServersUnsaved = mutableStateOf<(List)?>(null) @@ -484,10 +484,11 @@ object ChatModel { networkStatuses[contact.activeConn.agentConnId] ?: NetworkStatus.Unknown() fun addTerminalItem(item: TerminalItem) { - if (terminalItems.size >= 500) { - terminalItems.removeAt(0) + if (terminalItems.value.size >= 500) { + terminalItems.value = terminalItems.value.takeLast(499) + item + } else { + terminalItems.value += item } - terminalItems.add(item) } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt index 47bb6918d..01e64d54c 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt @@ -34,7 +34,7 @@ fun TerminalView(chatModel: ChatModel, close: () -> Unit) { close() }) TerminalLayout( - remember { chatModel.terminalItems }, + chatModel.terminalItems, composeState, sendCommand = { sendCommand(chatModel, composeState) }, close @@ -62,7 +62,7 @@ private fun sendCommand(chatModel: ChatModel, composeState: MutableState, + terminalItems: MutableState>, composeState: MutableState, sendCommand: () -> Unit, close: () -> Unit @@ -115,12 +115,12 @@ fun TerminalLayout( private var lazyListState = 0 to 0 @Composable -fun TerminalLog(terminalItems: List) { +fun TerminalLog(terminalItems: MutableState>) { val listState = rememberLazyListState(lazyListState.first, lazyListState.second) DisposableEffect(Unit) { onDispose { lazyListState = listState.firstVisibleItemIndex to listState.firstVisibleItemScrollOffset } } - val reversedTerminalItems by remember { derivedStateOf { terminalItems.reversed().toList() } } + val reversedTerminalItems by remember { derivedStateOf { terminalItems.value.reversed().toList() } } val clipboard = LocalClipboardManager.current LazyColumn(state = listState, reverseLayout = true) { items(reversedTerminalItems) { item -> @@ -152,7 +152,7 @@ fun TerminalLog(terminalItems: List) { fun PreviewTerminalLayout() { SimpleXTheme { TerminalLayout( - terminalItems = TerminalItem.sampleData, + terminalItems = remember { mutableStateOf(TerminalItem.sampleData) }, composeState = remember { mutableStateOf(ComposeState(useLinkPreviews = false)) }, sendCommand = {}, close = {} diff --git a/apps/multiplatform/desktop/build.gradle.kts b/apps/multiplatform/desktop/build.gradle.kts index cda2c2504..a45fb9499 100644 --- a/apps/multiplatform/desktop/build.gradle.kts +++ b/apps/multiplatform/desktop/build.gradle.kts @@ -30,8 +30,16 @@ kotlin { compose { desktop { application { + // For debugging via VisualVM + /*jvmArgs += listOf( + "-Dcom.sun.management.jmxremote.port=8080", + "-Dcom.sun.management.jmxremote.ssl=false", + "-Dcom.sun.management.jmxremote.authenticate=false" + )*/ mainClass = "chat.simplex.desktop.MainKt" nativeDistributions { + // For debugging via VisualVM + //modules("jdk.zipfs", "jdk.management.agent") modules("jdk.zipfs") //includeAllModules = true outputBaseDir.set(project.file("../release"))