Created
April 19, 2023 13:50
-
-
Save kubukoz/6a68b21ad26ce64a4d9f6cf525515db6 to your computer and use it in GitHub Desktop.
Using `getAndDiscreteUpdates` and `mapN` and it still works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//> 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