Learn HalogenGHC loads in the background

16. Watch your component live

The panel below this component shows its renders, actions and state changes as they happen. It comes from Halogen.DevTools in haskell-halogen-devtools. Click "+1" a few times and watch it update.

The component tree has one row here, labelled Lesson.State# followed by its ID. Labels default to the state type's name; H.named "Clicks" component supplies a custom one. Each row shows render, action and state-change counts. Hovering outlines the component on the page.

The log lists recent events, newest first. Use the event buttons to filter by kind or click a component row to filter by component.

Where it comes from

Component trees produce Halogen.Trace events while a listener is present. The devtools panel is one such listener and is itself a Halogen component, but excludes its own tree from the display. The lesson wrapper opens it before mounting your component:

DevTools.showing @State
panel <- DevTools.openIn (coerce tools :: HTMLElement)
socket <- runUI component () (coerce app :: HTMLElement)

In your application, use DevTools.open to place the panel at the bottom right of the window. Open it before the components you want to inspect: outlining and state restoration require them to have been created while a listener was active.

DevTools.showing @State enables state values in the log and requires Show State. Other state types still have their changes counted and listed. Register each type whose values you want to inspect.

Restore writes a recorded state back to the component and triggers a render, like put. Click "+1" five times, then restore the third change to return to a count of 3.

Renders that change nothing

Wait a few seconds. The render and state-change counts increase every second, although the displayed clock changes only once a minute.

Tick always writes a new record with modify (\st -> st {clock = minute}). Halogen does not compare fields using Eq: component state need not provide that instance. It can skip an unchanged value such as modify id, but a fresh record still triggers a render even if its fields match.

Exercise

Update state only when the minute changes:

st <- get
when (st.clock /= minute) $ put st {clock = minute}

Import get and put from Control.Monad.State and when from Control.Monad. Compile and watch the counters. They should now increase on clicks and minute changes.

Values used only for bookkeeping may not need to be in rendered state at all. Updating them there would otherwise trigger unnecessary renders.

Lesson.hs
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}

module Lesson (component, State (..)) where

import Control.Monad (void)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.State (modify)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Time (defaultTimeLocale, formatTime, getCurrentTime)
import Halogen qualified as H
import Halogen.HTML qualified as HH
import Halogen.HTML.Events qualified as HE
import Halogen.Query.Event.Window qualified as Window

-- The panel shows a state with this Show instance, once the program has
-- said so with DevTools.showing.
data State = State {count :: Int, clock :: Text}
  deriving (Eq, Show)

data Action = Increment | Tick

component :: forall q o m. (MonadIO m) => H.Component q () o m
component =
  H.component
    (\_ -> pure State {count = 0, clock = "--:--"})
    render
    [ H.onInitialize (void (H.subscribe (Tick <$ Window.interval 1000)))
    , H.onAction handleAction
    ]
  where
    render st =
      HH.div_
        [ HH.button [HE.onClick (const Increment)] [HH.text "+1"]
        , HH.p_ [HH.text ("Clicked " <> T.pack (show st.count) <> " times. It is " <> st.clock <> " UTC.")]
        ]

    handleAction = \case
      Increment -> modify (\st -> st {count = st.count + 1})
      -- Every second: read the clock, and keep the minute. The minute
      -- changes once a minute, but this writes a new state every second.
      Tick -> do
        now <- H.sync (liftIO getCurrentTime)
        let minute = T.pack (formatTime defaultTimeLocale "%H:%M" now)
        modify (\st -> st {clock = minute})

The compiler loads once the page has started.

Your component will appear here after compilation.