Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Created April 19, 2023 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubukoz/6a68b21ad26ce64a4d9f6cf525515db6 to your computer and use it in GitHub Desktop.
Save kubukoz/6a68b21ad26ce64a4d9f6cf525515db6 to your computer and use it in GitHub Desktop.
Using `getAndDiscreteUpdates` and `mapN` and it still works
//> using lib "co.fs2::fs2-core:3.7.0-RC4"
import cats.effect.IOApp
import cats.effect.IO
import scala.concurrent.duration._
import cats.effect.std.UUIDGen
import cats.implicits._
import scala.util.Random
import fs2.concurrent.Signal
object Demo extends IOApp.Simple {
override def run: IO[Unit] =
fs2
.Stream
.never[IO]
.covaryOutput[Int]
.holdResource(0)
.flatMap { currentPosSig =>
fs2
.Stream
.repeatEval(
IO(Map("foo" -> Random.nextInt()))
)
.metered(1.second)
.debug()
.holdResource(Map.empty)
.flatMap { sig2 =>
val v: Signal[cats.effect.IO, String] = (sig2, currentPosSig)
.mapN { (payload, myCoords) =>
payload + ("You" -> myCoords)
}
.map(_.toString)
v.getAndDiscreteUpdates.flatMap { (v, updates) =>
IO.println(v).toResource *>
updates.debug().compile.drain.background
}
}
}
.useForever
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment