{-# options_haddock prune #-}

-- |Helpers for sleeping, Internal
module Polysemy.Time.Sleep where

import Control.Concurrent (threadDelay)

import Polysemy.Time.Data.TimeUnit (MicroSeconds (MicroSeconds), TimeUnit, convert)

uSleep ::
  Member (Embed IO) r =>
  MicroSeconds ->
  Sem r ()
uSleep :: forall (r :: EffectRow).
Member (Embed IO) r =>
MicroSeconds -> Sem r ()
uSleep (MicroSeconds Int64
0) =
  Sem r ()
forall (f :: * -> *). Applicative f => f ()
unit
uSleep (MicroSeconds Int64
us) =
  IO () -> Sem r ()
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
embed (Int -> IO ()
threadDelay (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
us))

tSleep ::
  Member (Embed IO) r =>
  TimeUnit t =>
  t ->
  Sem r ()
tSleep :: forall (r :: EffectRow) t.
(Member (Embed IO) r, TimeUnit t) =>
t -> Sem r ()
tSleep =
  MicroSeconds -> Sem r ()
forall (r :: EffectRow).
Member (Embed IO) r =>
MicroSeconds -> Sem r ()
uSleep (MicroSeconds -> Sem r ()) -> (t -> MicroSeconds) -> t -> Sem r ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> MicroSeconds
forall a b. (TimeUnit a, TimeUnit b) => a -> b
convert