Skip to content

Conversation

@seanhess
Copy link
Owner

@seanhess seanhess commented Nov 5, 2025

I added optional view state to HyperViews. I'd appreciate some feedback from anyone interested.

ViewState is entirely backwards-compatible: the default is ()

import Effectful.State.Static.Local

data Counter = Counter
  deriving (Generic)
instance ViewId Counter where
  type ViewState Counter = Int

instance HyperView Counter es where
  data Action Counter
    = Increment
    | Decrement
    deriving (Generic, ViewAction)

  update Increment = do
    modify @Int (+ 1)
    pure viewCount
  update Decrement = do
    modify @Int (subtract 1)
    pure viewCount

viewCount :: View Counter ()
viewCount = row $ do
  n <- viewState
  col ~ gap 10 $ do
    el ~ dataFeature $ text $ pack $ show n
    row ~ gap 10 $ do
      button Decrement "Decrement" ~ Style.btn
      button Increment "Increment" ~ Style.btn

In the name of simplicity, I've resisted implementing this for a long time. We aren't trying to implement the Elm Achitecture. Instead we have a simpler model that follows the design philosophy of htmx: actions call the server and replace the view with something. However, I don't think it will hurt to have it as an option.

Some outstanding limitations:

  • modify, get, put, etc are from Effectful.State.Static.Local - It would be useful to export them from somewhere, but they might conflict with other State functions, like Effectful.State.Dynamic. They also require type annotations. Should we have specialized versions?
  • For Technical Reasons (tm) I added it to ViewId instead of HyperView directly. (It makes working with dumb Views a pain in the ass if they all have to be members of HyperView).

fix state updates js

cleanup
@adithyaov
Copy link
Contributor

I can take a look during the weekend if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants