Skip to content

Commit

Permalink
feat: use a better approach to implement it
Browse files Browse the repository at this point in the history
  • Loading branch information
nimatrueway committed Aug 20, 2024
1 parent ef3174c commit 52bf1ae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 41 deletions.
40 changes: 0 additions & 40 deletions core/src/main/scala/ox/channels/SourceOfSourceOps.scala

This file was deleted.

32 changes: 32 additions & 0 deletions core/src/main/scala/ox/channels/SourceOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,38 @@ trait SourceOps[+T] { outer: Source[T] =>
}
c

/** Pipes the elements of child sources into the output source. If the parent source or any of the child sources emit an error, the
* pulling stops and the output source emits the error.
*/
def flatten[U](using Ox, StageCapacity, T <:< Source[U]): Source[U] = {
val c2 = StageCapacity.newChannel[U]

forkPropagate(c2) {
var pool = List[Source[T] | Source[U]](this)
repeatWhile {
selectOrClosed(pool) match {
case ChannelClosed.Done =>
// TODO: best to remove the specific channel that signalled to be Done
pool = pool.filterNot(_.isClosedForReceiveDetail.contains(ChannelClosed.Done))
if pool.isEmpty then
c2.doneOrClosed()
false
else true
case ChannelClosed.Error(e) =>
c2.errorOrClosed(e)
false
case t: Source[T] @unchecked =>
pool = t :: pool
true
case r: U @unchecked =>
c2.sendOrClosed(r).isValue
}
}
}

c2
}

/** Concatenates this source with the `other` source. The resulting source will emit elements from this source first, and then from the
* `other` source.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ox.*
import java.util.concurrent.CountDownLatch
import scala.collection.mutable.ListBuffer

class SourceOfSourceOpsTest extends AnyFlatSpec with Matchers with OptionValues {
class SourceOpsFlattenTest extends AnyFlatSpec with Matchers with OptionValues {

"flatten" should "pipe all elements of the child sources into the output source" in {
supervised {
Expand Down

0 comments on commit 52bf1ae

Please sign in to comment.