Skip to content

Commit

Permalink
Syncronize interceptor mutation on JVM (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt authored May 8, 2022
1 parent 6af02e1 commit dae38fc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ internal expect fun sendInterceptedPrintRequest(
request: PrintRequest,
terminalInterface: TerminalInterface,
interceptors: List<TerminalInterceptor>,
lock: Any,
)

internal expect inline fun synchronizeJvm(lock: Any, block: () -> Unit)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -266,6 +271,8 @@ class Terminal(
}

private fun sendPrintRequest(request: PrintRequest) {
sendInterceptedPrintRequest(request, terminalInterface, interceptors, lock)
synchronizeJvm(lock) {
sendInterceptedPrintRequest(request, terminalInterface, interceptors)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ internal actual fun sendInterceptedPrintRequest(
request: PrintRequest,
terminalInterface: TerminalInterface,
interceptors: List<TerminalInterceptor>,
lock: Any,
) {
terminalInterface.completePrintRequest(
interceptors.fold(request) { acc, it -> it.intercept(acc) }
)
}

internal actual inline fun synchronizeJvm(lock: Any, block: () -> Unit) = block()
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,10 @@ internal actual fun sendInterceptedPrintRequest(
request: PrintRequest,
terminalInterface: TerminalInterface,
interceptors: List<TerminalInterceptor>,
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)
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ internal actual fun sendInterceptedPrintRequest(
request: PrintRequest,
terminalInterface: TerminalInterface,
interceptors: List<TerminalInterceptor>,
lock: Any,
) {
terminalInterface.completePrintRequest(
interceptors.fold(request) { acc, it -> it.intercept(acc) }
)
}

internal actual inline fun synchronizeJvm(lock: Any, block: () -> Unit) = block()

0 comments on commit dae38fc

Please sign in to comment.