fs2.interop.flow
Implementation of the reactive-streams protocol for fs2; based on Java Flow.
Attributes
- See also
-
java.util.concurrent.Flow
- Example
-
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)
Members list
Type members
Classlikes
Attributes
- Source
- syntax.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
syntax.type
Value members
Concrete methods
Creates a Stream from a subscribe
function; analogous to a Publisher
, but effectual.
Creates a Stream from a subscribe
function; analogous to a Publisher
, but effectual.
This function is useful when you actually need to provide a subscriber to a third-party.
Value parameters
- 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.
Attributes
- See also
-
the overload that only requires a Publisher.
- Note
-
The subscribe function will not be executed until the stream is run.
- Example
-
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(..)
- Source
- package.scala
Creates a Stream from an Publisher.
Creates a Stream from an Publisher.
Value parameters
- 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. - publisher
-
The Publisher to consume.
Attributes
- See also
-
the
toStream
extension method added toPublisher
- Note
-
The publisher will not receive a subscriber until the stream is run.
- Example
-
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(..)
- Source
- package.scala
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.
Value parameters
- stream
-
the Stream that will be consumed by the subscriber.
- subscriber
-
the Subscriber that will receive the elements of the stream.
Attributes
- Source
- package.scala
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.
Value parameters
- stream
-
The Stream to transform.
Attributes
- See also
-
subscribeStream for a simpler version that only requires a Subscriber.
- Note
-
This Publisher can be reused for multiple Subscribers, each subscription will re-run the Stream from the beginning.
- Source
- package.scala
Concrete fields
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.
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.
Attributes
- Note
-
the current value is
256
. - Source
- package.scala