Module
React.Explore
- Package
- purescript-react-explore
- Repository
- paf31/purescript-react-explore
We can think of user interfaces as exploring a (pointed) space of states. We can model that space using a comonad, and different choices of comonad correspond to different UI patterns:
- Store corresponds to something like React, where we just have a state, and a function taking each state to a user interface.
- The Traced comonad corresponds to something like an incremental game, where we have a state for every value in some monoid.
- Mealy machines are more like the Elm architecture, where we respond to input events, and update some internal state.
- A cofree comonad is a bit like Halogen. We have some functor which describes the transitions to new states, but which can also respond to queries on the current state.
We can use Co w to explore the state space. Co w actions will be
connected to the user interface. For example:
Co (Store s)is isomorphic toState s, so we can use get and put to read and write the stored state.Co (Traced w)is isomorphic toWriter, so we can usetellto append some monoidal value to our state.Mealy actionisCofree (Function action), soCo (Mealy action)is isomorphic toFree (Tuple action). We can emit zero or more actions in response to each user event.Co (Cofree f)is isomorphic toFree gwheneverfpairs withg. This corresponds to something like the Halogen API.
#UI Source
type UI a = Handler a -> ReactElementA UI, which is parameterized by its type of actions. For the purposes of
this implementation, a UI is just a ReactElement which takes its event
Handler as an explicit argument.