Last active
May 30, 2020 17:17
-
-
Save JordanMartinez/eb5e3ba79b4f23ebf0f488fca037fcc2 to your computer and use it in GitHub Desktop.
Halogen removes component when disposed
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 Data.Maybe (Maybe(..)) | |
| import Data.Monoid (power) | |
| import Effect (Effect) | |
| import Effect.Class (liftEffect) | |
| import Effect.Console (log) | |
| import Effect.Aff (Aff, Milliseconds(..), delay) | |
| import Halogen as H | |
| import Halogen.Aff (awaitBody, runHalogenAff) | |
| import Halogen.HTML as HH | |
| import Halogen.VDom.Driver (runUI) | |
| main :: Effect Unit | |
| main = runHalogenAff do | |
| body <- awaitBody | |
| loadingIO <- runUI loading unit body | |
| delay (Milliseconds 2000.0) | |
| loadingIO.dispose | |
| data ActionType = Initialize | Finalize | |
| type MonadType = Aff | |
| loading :: forall q o. H.Component HH.HTML q Unit o Aff | |
| loading = | |
| H.mkComponent | |
| { initialState: const (-1) | |
| , render: render | |
| , eval: H.mkEval $ H.defaultEval { handleAction = handleAction, initialize = Just Initialize, finalize = Just Finalize } | |
| } | |
| where | |
| render :: Int -> H.ComponentHTML ActionType () Aff | |
| render st = | |
| HH.div_ | |
| [ HH.h1_ | |
| [ HH.text $ "Loading" <> (power "." st) | |
| ] | |
| ] | |
| handleAction :: ActionType -> H.HalogenM Int ActionType () o MonadType Unit | |
| handleAction = case _ of | |
| Initialize -> do | |
| let increaseOrResetDots = do | |
| H.liftAff $ delay (Milliseconds 200.0) | |
| H.modify_ (\s -> if s == 3 then -1 else s + 1) | |
| increaseOrResetDots | |
| void $ H.fork increaseOrResetDots | |
| Finalize -> do | |
| liftEffect $ log "Finalizing..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment