0

I'm just a beginner to the whole world of Purescript and Pux, so I'm a little confused as to where we handle effects.

Currently I'm modelling the effects in my type:

type State = { countries ∷ Maybe (Eff (random :: RANDOM) Countries) }

And then using that in my foldp function:

foldp (Request) state = { state, effects: [countries] }

Where countries is defined as:

countries = do
  response <- attempt $ get "/countries.json"
  let countries = either (Left <<< show) decode response
  pure $ Just $ Receive $ case shuffle <$> countries of
    Left _   → Nothing
    Right xs → Just xs

However at some point I need to unwrap the RANDOM effect from the type to be able to return it from my view: State → HTML Event.

Wildhoney
  • 4,969
  • 33
  • 38

1 Answers1

1

You simply don't do any side effects in the view. Lift the random effect in your foldp:

data Countries
data Event = Request | Received (Maybe Countries) | FetchError String

type State =  {countries :: Maybe Countries, error :: String}

type AppFx fx = (random :: RANDOM | fx)

foldp :: ∀ fx. FoldP State Event (AppFx fx)
foldp (FetchError msg) state = noEffects state{error = msg}
foldp (Received countries) state = noEffects state{countries = countries}
foldp Request state = {state, effects: pure countriesRequest}
  where
  countriesRequest = Just <$> do
    response <- attempt $ getCountries
    case response of
      Left errorMsg -> pure $ FetchError $ show errorMsg
      Right countries -> case shuffle countries of
          Left _ -> pure $ Received Nothing
          Right shuffleEff -> do
            shuffledCountries <- liftEff shuffleEff
            pure $ Received $ Just shuffledCountries

shuffle :: ∀ fx. Countries -> Either String (Eff (random :: RANDOM | fx) Countries)
shuffle = unsafeCrashWith "TODO"

getCountries :: ∀ fx. Aff fx Countries
getCountries = unsafeCrashWith "TODO"
pete
  • 146
  • 1