diff --git a/CHANGELOG.md b/CHANGELOG.md index 315b777de..4652485d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ - Update Kotlin to 1.6.20 - Publish JS target with the IR format in addition to LEGACY +### Fixed +- Fix race condition when using ProgressAnimation and adding interceptors in JVM [(#55)](https://github.com/ajalt/mordant/issues/55) + # 2.0.0-beta5 ### Added - Progress bars and other single-line animations are now supported in the IntelliJ console [(#49)](https://github.com/ajalt/mordant/issues/49) diff --git a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/internal/MppH.kt b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/internal/MppH.kt index 1133cd575..b7f62637d 100644 --- a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/internal/MppH.kt +++ b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/internal/MppH.kt @@ -38,5 +38,6 @@ internal expect fun sendInterceptedPrintRequest( request: PrintRequest, terminalInterface: TerminalInterface, interceptors: List, - lock: Any, ) + +internal expect inline fun synchronizeJvm(lock: Any, block: () -> Unit) diff --git a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/terminal/Terminal.kt b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/terminal/Terminal.kt index c64e4b905..2177b96ad 100644 --- a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/terminal/Terminal.kt +++ b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/terminal/Terminal.kt @@ -3,6 +3,7 @@ package com.github.ajalt.mordant.terminal import com.github.ajalt.mordant.internal.makePrintingTerminalCursor import com.github.ajalt.mordant.internal.renderLinesAnsi import com.github.ajalt.mordant.internal.sendInterceptedPrintRequest +import com.github.ajalt.mordant.internal.synchronizeJvm import com.github.ajalt.mordant.rendering.* import com.github.ajalt.mordant.table.table import com.github.ajalt.mordant.widgets.HorizontalRule @@ -250,11 +251,15 @@ class Terminal( } internal fun addInterceptor(interceptor: TerminalInterceptor) { - interceptors += interceptor + synchronizeJvm(lock) { + interceptors += interceptor + } } internal fun removeInterceptor(interceptor: TerminalInterceptor) { - interceptors.remove(interceptor) + synchronizeJvm(lock) { + interceptors.remove(interceptor) + } } private fun rawPrintln(message: String) { @@ -266,6 +271,8 @@ class Terminal( } private fun sendPrintRequest(request: PrintRequest) { - sendInterceptedPrintRequest(request, terminalInterface, interceptors, lock) + synchronizeJvm(lock) { + sendInterceptedPrintRequest(request, terminalInterface, interceptors) + } } } diff --git a/mordant/src/jsMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt b/mordant/src/jsMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt index 664f48ebe..f4c0b9152 100644 --- a/mordant/src/jsMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt +++ b/mordant/src/jsMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt @@ -115,9 +115,10 @@ internal actual fun sendInterceptedPrintRequest( request: PrintRequest, terminalInterface: TerminalInterface, interceptors: List, - lock: Any, ) { terminalInterface.completePrintRequest( interceptors.fold(request) { acc, it -> it.intercept(acc) } ) } + +internal actual inline fun synchronizeJvm(lock: Any, block: () -> Unit) = block() diff --git a/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt b/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt index 7a446a416..25c09107d 100644 --- a/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt +++ b/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt @@ -131,11 +131,10 @@ internal actual fun sendInterceptedPrintRequest( request: PrintRequest, terminalInterface: TerminalInterface, interceptors: List, - lock: Any, ) { - synchronized(lock) { - terminalInterface.completePrintRequest( - interceptors.fold(request) { acc, it -> it.intercept(acc) } - ) - } + terminalInterface.completePrintRequest( + interceptors.fold(request) { acc, it -> it.intercept(acc) } + ) } + +internal actual inline fun synchronizeJvm(lock: Any, block: () -> Unit) = synchronized(lock, block) diff --git a/mordant/src/nativeMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt b/mordant/src/nativeMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt index d1c784e9b..ca9e787e3 100644 --- a/mordant/src/nativeMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt +++ b/mordant/src/nativeMain/kotlin/com/github/ajalt/mordant/internal/MppImpl.kt @@ -110,9 +110,10 @@ internal actual fun sendInterceptedPrintRequest( request: PrintRequest, terminalInterface: TerminalInterface, interceptors: List, - lock: Any, ) { terminalInterface.completePrintRequest( interceptors.fold(request) { acc, it -> it.intercept(acc) } ) } + +internal actual inline fun synchronizeJvm(lock: Any, block: () -> Unit) = block()