module Polysemy.Time.Loop where
import qualified Polysemy.Time.Effect.Time as Time
import Polysemy.Time.Effect.Time (Time)
import Polysemy.Time.Data.TimeUnit (TimeUnit)
while ::
∀ t d u r .
Member (Time t d) r =>
TimeUnit u =>
u ->
Sem r Bool ->
Sem r ()
while :: forall t d u (r :: [(* -> *) -> * -> *]).
(Member (Time t d) r, TimeUnit u) =>
u -> Sem r Bool -> Sem r ()
while u
interval Sem r Bool
action =
Sem r ()
spin
where
spin :: Sem r ()
spin =
Sem r Bool -> Sem r () -> Sem r ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM Sem r Bool
action (forall t d u (r :: [(* -> *) -> * -> *]).
(TimeUnit u, Member (Time t d) r) =>
u -> Sem r ()
Time.sleep @t @d u
interval Sem r () -> Sem r () -> Sem r ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Sem r ()
spin)
loop ::
∀ t d u a r .
Member (Time t d) r =>
TimeUnit u =>
u ->
a ->
(a -> Sem r a) ->
Sem r ()
loop :: forall t d u a (r :: [(* -> *) -> * -> *]).
(Member (Time t d) r, TimeUnit u) =>
u -> a -> (a -> Sem r a) -> Sem r ()
loop u
interval a
initial a -> Sem r a
action =
Sem r Any -> Sem r ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (a -> Sem r Any
spin a
initial)
where
spin :: a -> Sem r Any
spin a
a = do
a
new <- a -> Sem r a
action a
a
forall t d u (r :: [(* -> *) -> * -> *]).
(TimeUnit u, Member (Time t d) r) =>
u -> Sem r ()
Time.sleep @t @d u
interval
a -> Sem r Any
spin a
new
loop_ ::
∀ t d u r .
Member (Time t d) r =>
TimeUnit u =>
u ->
Sem r () ->
Sem r ()
loop_ :: forall t d u (r :: [(* -> *) -> * -> *]).
(Member (Time t d) r, TimeUnit u) =>
u -> Sem r () -> Sem r ()
loop_ u
interval Sem r ()
action =
Sem r () -> Sem r ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (Sem r ()
action Sem r () -> Sem r () -> Sem r ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall t d u (r :: [(* -> *) -> * -> *]).
(TimeUnit u, Member (Time t d) r) =>
u -> Sem r ()
Time.sleep @t @d u
interval)