-----------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Miso.Diff
-- Copyright   :  (C) 2016-2025 David M. Johnson
-- License     :  BSD3-style (see the file LICENSE)
-- Maintainer  :  David M. Johnson <code@dmj.io>
-- Stability   :  experimental
-- Portability :  non-portable
----------------------------------------------------------------------------
module Miso.Diff
  ( diff
  , mountElement
  ) where
-----------------------------------------------------------------------------
import           GHCJS.Foreign.Internal hiding (Object)
import           GHCJS.Types
import           JavaScript.Object.Internal
-----------------------------------------------------------------------------
import qualified Miso.FFI.Internal as FFI
import           Miso.FFI.Internal (JSM)
import           Miso.Html.Types
import           Miso.String
-----------------------------------------------------------------------------
-- | diffing / patching a given element
diff :: Maybe VTree -> Maybe VTree -> JSVal -> JSM ()
diff :: Maybe VTree -> Maybe VTree -> JSVal -> JSM ()
diff Maybe VTree
current Maybe VTree
new JSVal
mountEl =
  case (Maybe VTree
current, Maybe VTree
new) of
    (Maybe VTree
Nothing, Maybe VTree
Nothing) -> () -> JSM ()
forall a. a -> JSM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    (Just (VTree Object
current'), Just (VTree Object
new')) ->
      Object -> Object -> JSVal -> JSM ()
FFI.diff Object
current' Object
new' JSVal
mountEl
    (Maybe VTree
Nothing, Just (VTree Object
new')) -> do
      Object -> Object -> JSVal -> JSM ()
FFI.diff (JSVal -> Object
Object JSVal
jsNull) Object
new' JSVal
mountEl
    (Just (VTree Object
current'), Maybe VTree
Nothing) ->
      Object -> Object -> JSVal -> JSM ()
FFI.diff Object
current' (JSVal -> Object
Object JSVal
jsNull) JSVal
mountEl
-----------------------------------------------------------------------------
-- | return the configured mountPoint element or the body
mountElement :: MisoString -> JSM JSVal
mountElement :: MisoString -> JSM JSVal
mountElement MisoString
"body" = JSM JSVal
FFI.getBody
mountElement MisoString
e = MisoString -> JSM JSVal
FFI.getElementById MisoString
e
-----------------------------------------------------------------------------