Packages

  • package root
    Definition Classes
    root
  • package fs2
    Definition Classes
    root
  • package interop
    Definition Classes
    fs2
  • package flow

    Implementation of the reactive-streams protocol for fs2; based on Java Flow.

    Implementation of the reactive-streams protocol for fs2; based on Java Flow.

    Definition Classes
    interop
    Deprecated

    All syntax has been moved directly onto Stream.

    Example:
    1. scala> import cats.effect.IO
      scala> import fs2.Stream
      scala> import java.util.concurrent.Flow.Publisher
      scala>
      scala> val upstream: Stream[IO, Int] = Stream(1, 2, 3).covary[IO]
      scala> val publisher: Stream[IO, Publisher[Int]] = upstream.toPublisher
      scala> val downstream: Stream[IO, Int] = publisher.flatMap { publisher =>
           |   Stream.fromPublisher[IO](publisher, chunkSize = 16)
           | }
      scala>
      scala> import cats.effect.unsafe.implicits.global
      scala> downstream.compile.toVector.unsafeRunSync()
      res0: Vector[Int] = Vector(1, 2, 3)
    See also

    java.util.concurrent.Flow

  • syntax

package flow

Implementation of the reactive-streams protocol for fs2; based on Java Flow.

Deprecated

All syntax has been moved directly onto Stream.

Source
package.scala
Example:
  1. scala> import cats.effect.IO
    scala> import fs2.Stream
    scala> import java.util.concurrent.Flow.Publisher
    scala>
    scala> val upstream: Stream[IO, Int] = Stream(1, 2, 3).covary[IO]
    scala> val publisher: Stream[IO, Publisher[Int]] = upstream.toPublisher
    scala> val downstream: Stream[IO, Int] = publisher.flatMap { publisher =>
         |   Stream.fromPublisher[IO](publisher, chunkSize = 16)
         | }
    scala>
    scala> import cats.effect.unsafe.implicits.global
    scala> downstream.compile.toVector.unsafeRunSync()
    res0: Vector[Int] = Vector(1, 2, 3)
See also

java.util.concurrent.Flow

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. flow
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Value Members

  1. 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.

  2. def fromPublisher[F[_]]: FromPublisherPartiallyApplied[F]

    Creates a Stream from a Publisher.

    Creates a Stream from a Publisher.

    Example:
    1. 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 to Publisher

  3. 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 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.

    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.

    Example:
    1. 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.

  4. 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.

  5. 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 not accepting new subscriptions, but waiting for all active ones to finish consuming. Canceling the Resource.use 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

    unsafeToPublisher for an unsafe version that returns a plain Publisher.

    subscribeStream for a simpler version that only requires a Subscriber.

  6. def unsafeToPublisher[A](stream: Stream[IO, A])(implicit runtime: IORuntime): Publisher[A]

    Creates a Publisher from a Stream.

    Creates a Publisher from a Stream.

    The stream is only ran when elements are requested.

    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

    toPublisher for a safe version that returns a Resource.

  7. object syntax

Inherited from AnyRef

Inherited from Any

Ungrouped