Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactive-stream/Flow Interop #896

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ jmh-result.json
*.jfr
*.json
*.gpg
test-output
.DS_Store
17 changes: 17 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ lazy val kyoJVM = project
`kyo-stats-registry`.jvm,
`kyo-stats-otel`.jvm,
`kyo-cache`.jvm,
`kyo-reactive-streams`.jvm,
`kyo-sttp`.jvm,
`kyo-tapir`.jvm,
`kyo-caliban`.jvm,
Expand Down Expand Up @@ -340,6 +341,22 @@ lazy val `kyo-cache` =
)
.jvmSettings(mimaCheck(false))

lazy val `kyo-reactive-streams` =
crossProject(JVMPlatform)
.withoutSuffixFor(JVMPlatform)
.crossType(CrossType.Full)
.in(file("kyo-reactive-streams"))
.dependsOn(`kyo-core`)
.settings(
`kyo-settings`,
libraryDependencies ++= Seq(
"org.reactivestreams" % "reactive-streams" % "1.0.4",
"org.reactivestreams" % "reactive-streams-tck" % "1.0.4" % Test,
"org.scalatestplus" %% "testng-7-5" % "3.2.17.0" % Test
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required to reuse the tck tests? Would it be possible to use scalatest like the other modules?

Copy link
Contributor Author

@HollandDM HollandDM Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to reimplement those tests in scalatest though.

)
)
.jvmSettings(mimaCheck(false))

lazy val `kyo-sttp` =
crossProject(JSPlatform, JVMPlatform, NativePlatform)
.withoutSuffixFor(JVMPlatform)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package kyo.interop.flow

import java.util.concurrent.Flow.*
import kyo.*
import kyo.interop.flow.StreamSubscription.StreamFinishState
import kyo.kernel.Boundary
import scala.annotation.nowarn

abstract private[kyo] class StreamPublisher[V, Ctx](
stream: Stream[V, Ctx]
) extends Publisher[V]:

protected def bind(subscriber: Subscriber[? >: V]): Unit

override def subscribe(subscriber: Subscriber[? >: V]): Unit =
if isNull(subscriber) then
throw new NullPointerException("Subscriber must not be null.")
else
bind(subscriber)
end subscribe

end StreamPublisher

object StreamPublisher:

def apply[V, Ctx](
stream: Stream[V, Ctx],
capacity: Int = Int.MaxValue
)(
using
Boundary[Ctx, IO],
Frame,
Tag[Emit[Chunk[V]]],
Tag[Poll[Chunk[V]]]
): StreamPublisher[V, Ctx] < (Resource & IO & Ctx) =
def discardSubscriber(subscriber: Subscriber[? >: V]): Unit =
subscriber.onSubscribe(new Subscription:
override def request(n: Long): Unit = ()
override def cancel(): Unit = ()
)
subscriber.onComplete()
end discardSubscriber

def consumeChannel(
channel: Channel[Subscriber[? >: V]],
supervisor: Fiber.Promise[Nothing, Unit]
): Unit < (Async & Ctx) =
Abort.recover[Closed](_ => supervisor.interrupt.unit)(
channel.stream().runForeach: subscriber =>
for
subscription <- IO.Unsafe(new StreamSubscription[V, Ctx](stream, subscriber))
fiber <- subscription.subscribe.andThen(subscription.consume)
_ <- supervisor.onInterrupt(_ => fiber.interrupt(Result.Panic(Interrupt())).unit)
yield ()
)

for
channel <-
Resource.acquireRelease(Channel.init[Subscriber[? >: V]](capacity))(
_.close.map(_.foreach(_.foreach(discardSubscriber(_))))
)
publisher <- IO.Unsafe {
new StreamPublisher[V, Ctx](stream):
override protected def bind(
subscriber: Subscriber[? >: V]
): Unit =
channel.unsafe.offer(subscriber) match
case Result.Success(true) => ()
case _ => discardSubscriber(subscriber)
}
supervisor <- Resource.acquireRelease(Fiber.Promise.init[Nothing, Unit])(_.interrupt.unit)
_ <- Resource.acquireRelease(Async._run(consumeChannel(channel, supervisor)))(_.interrupt.unit)
yield publisher
end for
end apply

object Unsafe:
@nowarn("msg=anonymous")
inline def apply[V, Ctx](
stream: Stream[V, Ctx],
subscribeCallback: (Fiber[Nothing, StreamFinishState] < (IO & Ctx)) => Unit
)(
using
AllowUnsafe,
Frame,
Tag[Emit[Chunk[V]]],
Tag[Poll[Chunk[V]]]
): StreamPublisher[V, Ctx] =
new StreamPublisher[V, Ctx](stream):
override protected def bind(
subscriber: Subscriber[? >: V]
): Unit =
discard(StreamSubscription.Unsafe._subscribe(
stream,
subscriber
)(
subscribeCallback
))
end Unsafe
end StreamPublisher
Loading
Loading