Skip to content

Instantly share code, notes, and snippets.

@mjhoy
Created October 24, 2016 21:39
Show Gist options
  • Save mjhoy/88ee501c426a1f236c79adbc13fd466f to your computer and use it in GitHub Desktop.
Save mjhoy/88ee501c426a1f236c79adbc13fd466f to your computer and use it in GitHub Desktop.
module TestTime where
import Prelude
import Thermite as T
import Control.Monad.Aff (later')
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Console (log, CONSOLE)
import Control.Monad.Eff.Now (NOW, nowDateTime)
import Control.Monad.Maybe.Trans (runMaybeT)
import Control.Monad.Rec.Class (forever)
import Control.Monad.Trans (lift)
import Data.Maybe (Maybe(..))
data GameAction = Tick
type Effect eff = ( console :: CONSOLE, now :: NOW | eff )
type GameState = { t :: Int }
perform1 :: forall eff props. T.PerformAction (Effect eff) GameState props GameAction
perform1 Tick _ _ = forever do
_ <- lift $ later' 1000 (pure unit)
now <- lift $ liftEff $ nowDateTime
lift $ liftEff $ log "tick!"
void $ T.cotransform (\s -> s { t = s.t + 1 })
-- adding runMaybeT
perform2 :: forall eff props. T.PerformAction (Effect eff) GameState props GameAction
perform2 Tick _ _ = void $ runMaybeT $ forever do
_ <- lift $ lift $ later' 1000 (pure unit)
now <- lift $ lift $ liftEff $ nowDateTime
lift $ (T.cotransform (\s -> s { t = s.t + 1 }))
pure Nothing
-- ^ that last line does not seem to stop the loop!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment