Created
November 29, 2021 21:00
-
-
Save flip111/1839f8c49feeee004f615e1b66f011ee to your computer and use it in GitHub Desktop.
trying to get halogen store working with monomorphic Aff instead of polymorphic MonadAff m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Prelude | |
import Effect (Effect) | |
import Effect.Aff (launchAff_) | |
import Halogen.Aff as HA | |
import Halogen.Store.Monad (runStoreT) | |
import Halogen.VDom.Driver (runUI) | |
import Data.Maybe (Maybe(..)) | |
import Halogen as H | |
import Halogen.HTML as HH | |
import Halogen.HTML.Events as HE | |
import Halogen.Store.Connect (Connected, connect) | |
import Halogen.Store.Monad (class MonadStore) | |
import Halogen.Store.Monad as Store | |
import Halogen.Store.Select (Selector, selectEq) | |
import Effect.Aff (Aff) | |
main :: Effect Unit | |
main = launchAff_ do | |
body <- HA.awaitBody | |
root <- runStoreT initialStore reduce component | |
runUI root unit body | |
type Store = { count :: Int } | |
initialStore :: Store | |
initialStore = { count: 0 } | |
data StoreAction | |
= StoreIncrement | |
| StoreDecrement | |
reduce :: Store -> StoreAction -> Store | |
reduce store = case _ of | |
StoreIncrement -> store { count = store.count + 1 } | |
StoreDecrement -> store { count = store.count - 1 } | |
type Input = Unit | |
type Context = Int | |
type State = Int | |
data Action | |
= Increment | |
| Decrement | |
| Receive (Connected Context Input) | |
deriveState :: Connected Context Input -> State | |
deriveState { context } = context | |
selectCount :: Selector Store Context | |
selectCount = selectEq _.count | |
-- Working type signature | |
--component | |
-- :: forall query output m | |
-- . MonadStore StoreAction Store m | |
-- => H.Component query Input output m | |
-- Would like to fix `m` to `Aff`, type signature needs to be reworked somehow. | |
-- Perhaps use StoreT instead of MonadStore in some way .. | |
component | |
:: forall query output | |
. MonadStore StoreAction Store Aff | |
=> H.Component query Input output Aff | |
component = connect selectCount $ H.mkComponent | |
{ initialState: deriveState | |
, render | |
, eval: H.mkEval $ H.defaultEval | |
{ handleAction = handleAction | |
, receive = Just <<< Receive | |
} | |
} | |
where | |
render count = | |
HH.div_ | |
[ HH.button | |
[ HE.onClick \_ -> Increment ] | |
[ HH.text "Increment" ] | |
, HH.text $ " Count: " <> show count <> " " | |
, HH.button | |
[ HE.onClick \_ -> Decrement ] | |
[ HH.text "Decrement" ] | |
] | |
handleAction = case _ of | |
Increment -> | |
Store.updateStore StoreIncrement | |
Decrement -> | |
Store.updateStore StoreDecrement | |
Receive input -> | |
H.put $ deriveState input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment