Skip to content

Commit

Permalink
Modify OutputFeed to also dispatch null to indicate end of stream
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed Apr 1, 2024
1 parent 937133d commit 6347a29
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ abstract class ProcessBaseTest {
.encodeToByteArray()

p.stdoutFeed { line ->
if (line == null) return@stdoutFeed
actual.add(line)
}

Expand Down Expand Up @@ -361,11 +362,13 @@ abstract class ProcessBaseTest {
val stderrBuilder = StringBuilder()

p.stdoutFeed { line ->
if (line == null) return@stdoutFeed
with(stdoutBuilder) {
if (isNotEmpty()) appendLine()
append(line)
}
}.stderrFeed { line ->
if (line == null) return@stderrFeed
with(stderrBuilder) {
if (isNotEmpty()) appendLine()
append(line)
Expand Down
3 changes: 0 additions & 3 deletions library/process/api/process.api
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,9 @@ public final class io/matthewnelson/kmp/process/Process$Current {
public final class io/matthewnelson/kmp/process/ReadBuffer {
public static final field Companion Lio/matthewnelson/kmp/process/ReadBuffer$Companion;
public static final synthetic fun box-impl (Ljava/lang/Object;)Lio/matthewnelson/kmp/process/ReadBuffer;
public static final fun capacity-impl (Ljava/lang/Object;)I
public static final fun decodeToUtf8-impl (Ljava/lang/Object;II)Ljava/lang/String;
public fun equals (Ljava/lang/Object;)Z
public static fun equals-impl (Ljava/lang/Object;Ljava/lang/Object;)Z
public static final fun equals-impl0 (Ljava/lang/Object;Ljava/lang/Object;)Z
public static final fun get-impl (Ljava/lang/Object;I)B
public static final fun getBuf-impl (Ljava/lang/Object;)[B
public fun hashCode ()I
public static fun hashCode-impl (Ljava/lang/Object;)I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ internal class OutputFeedBuffer private constructor(maxSize: Int): OutputFeed {
internal var maxSizeExceeded: Boolean = false
private set

override fun onOutput(line: String) {
override fun onOutput(line: String?) {
if (line == null) return
if (maxSizeExceeded) return

// do we need to add a new line character
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ import kotlin.time.Duration.Companion.milliseconds
*
* val p = builder.spawn()
* .stdoutFeed { line ->
* println(line)
* println(line ?: "--STDOUT EOS--")
* }.stderrFeed(
* // attach multiple at once
* OutputFeed { line ->
* println(line)
* println(line ?: "--STDERR EOS--")
* },
* OutputFeed { line ->
* // do something
Expand All @@ -72,8 +72,10 @@ public fun interface OutputFeed {
/**
* A line of output from `stdout` or `stderr` (whichever
* this [OutputFeed] has been attached to).
*
* `null` is dispatched to indicate [OutputFeed] closure.
* */
public fun onOutput(line: String)
public fun onOutput(line: String?)

/**
* Helper class which [Process] implements that handles everything
Expand Down Expand Up @@ -232,9 +234,6 @@ public fun interface OutputFeed {

@Suppress("NOTHING_TO_INLINE")
private inline fun SynchronizedSet<OutputFeed>.dispatch(line: String?) {
// TODO: dispatch null to indicate end of stream
if (line == null) return

withLock { toSet() }.forEach { feed ->
try {
feed.onOutput(line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class SpawnUnitTest {
val output = mutableListOf<String>()
val code = try {
p.stdoutFeed { line ->
if (line == null) return@stdoutFeed
output.add(line)
}
p.waitFor()
Expand Down Expand Up @@ -95,6 +96,7 @@ class SpawnUnitTest {
val output = mutableListOf<String>()
val code = try {
p.stdoutFeed { line ->
if (line == null) return@stdoutFeed
output.add(line)
}
p.waitFor()
Expand Down

0 comments on commit 6347a29

Please sign in to comment.