shrub/pkg/hs/king/lib/Vere/Behn.hs

45 lines
1.1 KiB
Haskell
Raw Normal View History

2019-08-01 03:27:13 +03:00
module Vere.Behn (behn) where
import UrbitPrelude
import Arvo hiding (Behn)
import Vere.Pier.Types
import Urbit.Time (Wen)
import Urbit.Timer (Timer)
import qualified Urbit.Time as Time
import qualified Urbit.Timer as Timer
-- Behn Stuff ------------------------------------------------------------------
bornEv :: KingId -> Ev
bornEv king = EvBlip $ BlipEvBehn $ BehnEvBorn (king, ()) ()
2019-08-01 03:27:13 +03:00
wakeEv :: Ev
wakeEv = EvBlip $ BlipEvBehn $ BehnEvWake () ()
sysTime = view Time.systemTime
2019-08-29 03:26:59 +03:00
behn :: KingId -> QueueEv -> ([Ev], Acquire (EffCb e BehnEf))
behn king enqueueEv =
2019-08-01 03:27:13 +03:00
(initialEvents, runBehn)
where
initialEvents = [bornEv king]
2019-08-01 03:27:13 +03:00
2019-08-29 03:26:59 +03:00
runBehn :: Acquire (EffCb e BehnEf)
2019-08-01 03:27:13 +03:00
runBehn = do
tim <- mkAcquire Timer.init Timer.stop
pure (handleEf tim)
2019-08-29 03:26:59 +03:00
handleEf :: Timer -> BehnEf -> RIO e ()
handleEf b = io . \case
2019-08-01 03:27:13 +03:00
BehnEfVoid v -> absurd v
BehnEfDoze (i, ()) mWen -> do
when (i == king) (doze b mWen)
2019-08-01 03:27:13 +03:00
doze :: Timer -> Maybe Wen -> IO ()
doze tim = \case
Nothing -> Timer.stop tim
Just t -> Timer.start tim (sysTime t) $ atomically (enqueueEv wakeEv)