package flow
Implementation of the reactive-streams protocol for fs2; based on Java Flow.
- Source
- package.scala
scala> import cats.effect.{IO, Resource} scala> import fs2.Stream scala> import fs2.interop.flow.syntax._ scala> import java.util.concurrent.Flow.Publisher scala> scala> val upstream: Stream[IO, Int] = Stream(1, 2, 3).covary[IO] scala> val publisher: Resource[IO, Publisher[Int]] = upstream.toPublisher scala> val downstream: Stream[IO, Int] = Stream.resource(publisher).flatMap { publisher => | publisher.toStream[IO](chunkSize = 16) | } scala> scala> import cats.effect.unsafe.implicits.global scala> downstream.compile.toVector.unsafeRunSync() res0: Vector[Int] = Vector(1, 2, 3)
- See also
- Alphabetic
- By Inheritance
- flow
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- val defaultChunkSize: Int
A default value for the
chunkSize
argument, that may be used in the absence of other constraints; we encourage choosing an appropriate value consciously.A default value for the
chunkSize
argument, that may be used in the absence of other constraints; we encourage choosing an appropriate value consciously. Alias for defaultBufferSize.- Note
the current value is
256
.
- def fromPublisher[F[_]]: FromPublisherPartiallyApplied[F]
Creates a Stream from an Publisher.
Creates a Stream from an Publisher.
scala> import cats.effect.IO scala> import fs2.Stream scala> import java.util.concurrent.Flow.Publisher scala> scala> def getThirdPartyPublisher(): Publisher[Int] = ??? scala> scala> // Interop with the third party library. scala> Stream.eval(IO.delay(getThirdPartyPublisher())).flatMap { publisher => | fs2.interop.flow.fromPublisher[IO](publisher, chunkSize = 16) | } res0: Stream[IO, Int] = Stream(..)
- Note
The publisher will not receive a subscriber until the stream is run.
- See also
the
toStream
extension method added toPublisher
Example: - def fromPublisher[F[_], A](chunkSize: Int)(subscribe: (Subscriber[A]) => F[Unit])(implicit F: Async[F]): Stream[F, A]
Creates a Stream from a
subscribe
function; analogous to aPublisher
, but effectual.Creates a Stream from a
subscribe
function; analogous to aPublisher
, but effectual.This function is useful when you actually need to provide a subscriber to a third-party.
- chunkSize
setup the number of elements asked each time from the Publisher. A high number may be useful if the publisher is triggering from IO, like requesting elements from a database. A high number will also lead to more elements in memory. The stream will not emit new element until, either the
Chunk
is filled or the publisher finishes.- subscribe
The effectual function that will be used to initiate the consumption process, it receives a Subscriber that should be used to subscribe to a Publisher. The
subscribe
operation must be called exactly once.
scala> import cats.effect.IO scala> import fs2.Stream scala> import java.util.concurrent.Flow.{Publisher, Subscriber} scala> scala> def thirdPartyLibrary(subscriber: Subscriber[Int]): Unit = { | def somePublisher: Publisher[Int] = ??? | somePublisher.subscribe(subscriber) | } scala> scala> // Interop with the third party library. scala> fs2.interop.flow.fromPublisher[IO, Int](chunkSize = 16) { subscriber => | IO.println("Subscribing!") >> | IO.delay(thirdPartyLibrary(subscriber)) >> | IO.println("Subscribed!") | } res0: Stream[IO, Int] = Stream(..)
- Note
The subscribe function will not be executed until the stream is run.
- See also
the overload that only requires a Publisher.
Example: - def subscribeStream[F[_], A](stream: Stream[F, A], subscriber: Subscriber[A])(implicit F: Async[F]): F[Unit]
Allows subscribing a Subscriber to a Stream.
Allows subscribing a Subscriber to a Stream.
The returned program will run until all the stream elements were consumed. Cancelling this program will gracefully shutdown the subscription.
- stream
the Stream that will be consumed by the subscriber.
- subscriber
the Subscriber that will receive the elements of the stream.
- def toPublisher[F[_], A](stream: Stream[F, A])(implicit F: Async[F]): Resource[F, Publisher[A]]
Creates a Publisher from a Stream.
Creates a Publisher from a Stream.
The stream is only ran when elements are requested. Closing the Resource means gracefully shutting down all active subscriptions. Thus, no more elements will be published.
- stream
The Stream to transform.
- Note
This Publisher can be reused for multiple Subscribers, each subscription will re-run the Stream from the beginning.
- See also
subscribeStream for a simpler version that only requires a Subscriber.
- object syntax