Skip to content

Commit

Permalink
Merge pull request #2377 from nikiforo/timer-derivation
Browse files Browse the repository at this point in the history
Derivations for clock & timer
  • Loading branch information
mpilquist authored Apr 30, 2021
2 parents 268d0c1 + f52e722 commit 5d2580a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/shared/src/main/scala/fs2/Pull.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import cats.syntax.all._
import fs2.internal._
import fs2.internal.FreeC.{Eval, Result}

import scala.concurrent.duration.{FiniteDuration, TimeUnit}

/** A `p: Pull[F,O,R]` reads values from one or more streams, returns a
* result of type `R`, and produces a `Stream[F,O]` when calling `p.stream`.
*
Expand Down Expand Up @@ -211,6 +213,25 @@ object Pull extends PullLowPriority {
new PullSyncInstance[F, O]
}

/* `Clock` instance for `Pull`. */
implicit def clockInstance[F[_], O](implicit clock: Clock[F]): Clock[Pull[F, O, *]] =
new Clock[Pull[F, O, *]] {
def realTime(unit: TimeUnit): Pull[F, O, Long] =
Pull.eval(clock.realTime(unit))

def monotonic(unit: TimeUnit): Pull[F, O, Long] =
Pull.eval(clock.monotonic(unit))
}

/* `Timer` instance for `Pull`. */
implicit def timerInstance[F[_], O](implicit timer: Timer[F]): Timer[Pull[F, O, *]] =
new Timer[Pull[F, O, *]] {
val clock: Clock[Pull[F, O, *]] = clockInstance

def sleep(duration: FiniteDuration): Pull[F, O, Unit] =
Pull.eval(timer.sleep(duration))
}

/** `FunctionK` instance for `F ~> Pull[F, INothing, *]`
*
* @example {{{
Expand Down

0 comments on commit 5d2580a

Please sign in to comment.