Skip to content

Instantly share code, notes, and snippets.

@ubourdon
Last active May 24, 2019 14:24
Show Gist options
  • Save ubourdon/1953c9dd8a3fd7cbcbe22428ed0dc258 to your computer and use it in GitHub Desktop.
Save ubourdon/1953c9dd8a3fd7cbcbe22428ed0dc258 to your computer and use it in GitHub Desktop.
schedule ZIO into ZStream
package zio
/*
repeatElems repeats each element of the stream according to the schedule, so delays are placed in-between the elements
repeat just repeats the entire stream, so delays are placed between the streams
*/
package object schedule {
import scalaz.zio.clock.Clock
import scalaz.zio.{DefaultRuntime, IO, Schedule, UIO, ZIO}
import scalaz.zio.stream.{ZSink, ZStream}
import scalaz.zio.duration._
val runtime = new DefaultRuntime {}
val log = IO.effect(println("test repeat Stream"))
val stream: ZStream[Any with Clock, Throwable, Unit] = ZStream.fromEffect(log).repeat(Schedule.spaced(10 seconds))
val result: ZIO[Any, Throwable, Unit] = stream.run(ZSink.drain).provide(runtime.Environment).consume.flatMap(_.fold(
_ => ConsoleLogger.info("error"),
IO.succeed
))
runtime.unsafeRunToFuture(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment