r/purescript • • Jul 06 '19

Some help getting an async eventloop going

Hello!

I'm trying to port a small pieced of code to PureScript but my FP-fu seems to be a bit limiting. I can't for my life figure out how Aff works compared to Haskells IO(Async a), and I'm not even sure I'm on the right track.

Essentially what I'm looking for is a model/update event loop, and I've used this one https://github.com/lazamar/elm-architecture-haskell before (50-odd lines of code).

I currently have something like this (that keeps spamming my initial messages).

module ElmArchitecture where

import Prelude

import Effect (Effect)
import Effect.Aff
import Effect.Class (liftEffect)
import Data.Tuple
import Data.Traversable (traverse, sequence)
import Data.Foldable (foldr)
import Data.Filterable (filter)
import Effect.Console (logShow)
import Control.Parallel (parOneOf, parOneOfMap)

type Cmd a = Array (Effect a)

run :: forall model msg r. { init :: Tuple model (Cmd msg), update :: msg -> model -> Tuple model (Cmd msg) | r} -> Aff Unit
run config =
  let initModel = fst config.init :: model
      initCmds = snd config.init

      run' :: Array(Aff msg) -> model -> Aff Unit
      run' affs model = do
        completed <- parOneOf affs :: Aff msg
        let next = config.update completed model :: Tuple model (Cmd msg)
        -- Need to filter out the completed aff but this does not work
        -- filter (\x -> x /= completed) affs 
        let nextAffs = (liftEffect <$> snd next) <> affs :: Array(Aff msg)
        run' nextAffs (fst next)
  in do
    run' (liftEffect <$> initCmds) initModel

init :: Tuple Model (Cmd Msg)
init = Tuple
       ( Model { cnt: 0
               }
       )
       [ log "HELLO WORLD" *> pure IncrementCounter
       , pure IncrementCounter
       ]

update :: Msg -> Model -> Tuple Model (Cmd Msg)
update msg model =
  case msg of
    (DoNothing) -> Tuple model []
    (IncrementCounter) -> Tuple (model) []

main :: Effect Unit
main = do
  _ <- launchAff_ $ run {init: init, update: update}
2 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Jul 17 '19

Thanks! I've typechecked your solution and its fine, however if an aff blocks it will block all of the other affs from completeling, so effectively it's single threaded.

1

u/natefaubion Jul 17 '19

I don't believe that's the case, since they are all forked. There is no user code that blocks the loop.