Migrated effect to static dispatch

This commit is contained in:
Stefan Risberg 2023-04-17 11:22:21 +02:00
parent b94133614f
commit d781295480
3 changed files with 35 additions and 19 deletions

View File

@ -1,5 +1,5 @@
name: ris-utils name: ris-utils
version: 0.0.1 version: 0.0.2
homepage: "https://git.fiskhamn.se/steffenomak/ris-utils#readme" homepage: "https://git.fiskhamn.se/steffenomak/ris-utils#readme"
license: BSD3 license: BSD3
author: "Stefan Risberg" author: "Stefan Risberg"

View File

@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack -- see: https://github.com/sol/hpack
name: ris-utils name: ris-utils
version: 0.0.1 version: 0.0.2
description: Please see the README at <https://git.fiskhamn.se/steffenomak/ris-utils#readme> description: Please see the README at <https://git.fiskhamn.se/steffenomak/ris-utils#readme>
homepage: https://git.fiskhamn.se/steffenomak/ris-utils#readme homepage: https://git.fiskhamn.se/steffenomak/ris-utils#readme
author: Stefan Risberg author: Stefan Risberg

View File

@ -2,8 +2,8 @@ module Effects.Time (
currentTime, currentTime,
currentTimeB, currentTimeB,
currentTimeT, currentTimeT,
runTimeEff,
Time, Time,
runTimeEff,
) )
where where
@ -12,25 +12,41 @@ import Chronos qualified as C
import Data.Text (Text) import Data.Text (Text)
import Data.Text.Lazy.Builder qualified as T import Data.Text.Lazy.Builder qualified as T
import Effectful import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.TH
data Time :: Effect where import Effectful.Dispatch.Static
CurrentTime :: Time m C.Time
CurrentTimeB :: Time m T.Builder
CurrentTimeT :: Time m Text
makeEffect ''Time data Time :: Effect
type instance DispatchOf Time = 'Static 'WithSideEffects
newtype instance StaticRep Time = Time ()
currentTime ::
Time :> es =>
Eff es C.Time
currentTime = do
Time _ <- getStaticRep
unsafeEff_ now
currentTimeB ::
Time :> es =>
Eff es T.Builder
currentTimeB = do
Time _ <- getStaticRep
unsafeEff_ $
builder_YmdHMS (SubsecondPrecisionFixed 4) w3c . timeToDatetime
<$> now
currentTimeT ::
Time :> es =>
Eff es Text
currentTimeT = do
Time _ <- getStaticRep
unsafeEff_ $
encode_YmdHMS (SubsecondPrecisionFixed 4) w3c . timeToDatetime
<$> now
runTimeEff :: runTimeEff ::
(IOE :> es) => IOE :> es =>
Eff (Time : es) a -> Eff (Time : es) a ->
Eff es a Eff es a
runTimeEff = interpret $ \_ -> \case runTimeEff = evalStaticRep (Time ())
CurrentTime -> liftIO now
CurrentTimeB ->
builder_YmdHMS (SubsecondPrecisionFixed 4) w3c . timeToDatetime
<$> liftIO now
CurrentTimeT ->
encode_YmdHMS (SubsecondPrecisionFixed 4) w3c . timeToDatetime
<$> liftIO now