@@ -1272,8 +1272,8 @@ hydrateComment doc t mSetContents = do
12721272skipToAndReplaceComment
12731273 :: (MonadJSM m, Reflex t, MonadFix m, Adjustable t m, MonadHold t m, RawDocument (DomBuilderSpace (HydrationDomBuilderT s t m)) ~ Document)
12741274 => Text
1275− -> IORef Text
1276− -> HydrationDomBuilderT s t m (HydrationRunnerT t m (), IORef DOM.Text, IORef Text)
1275+ -> IORef (Maybe Text)
1276+ -> HydrationDomBuilderT s t m (HydrationRunnerT t m (), IORef DOM.Text, IORef (Maybe Text))
12771277skipToAndReplaceComment prefix key0Ref = getHydrationMode >>= \case
12781278 HydrationMode_Immediate -> do
12791279 -- If we're in immediate mode, we don't try to replace an existing comment,
@@ -1281,43 +1281,51 @@ skipToAndReplaceComment prefix key0Ref = getHydrationMode >>= \case
12811281 t <- textNodeImmediate $ TextNodeConfig ("" :: Text) Nothing
12821282 append $ toNode t
12831283 textNodeRef <- liftIO $ newIORef t
1284− keyRef <- liftIO $ newIORef ""
1284+ keyRef <- liftIO $ newIORef Nothing
12851285 pure (pure (), textNodeRef, keyRef)
12861286 HydrationMode_Hydrating -> do
12871287 doc <- askDocument
12881288 textNodeRef <- liftIO $ newIORef $ error "textNodeRef not yet initialized"
12891289 keyRef <- liftIO $ newIORef $ error "keyRef not yet initialized"
1290− let go key0 mLastNode = do
1291− parent <- askParent
1292− node <- maybe (Node.getFirstChildUnchecked parent) Node.getNextSiblingUnchecked mLastNode
1293− DOM.castTo DOM.Comment node >>= \case
1290+ let
1291+ go Nothing _ = do
1292+ tn <- createTextNode doc ("" :: Text)
1293+ insertAfterPreviousNode tn
1294+ pure (tn, Nothing)
1295+ go (Just key0) mLastNode = do
1296+ parent <- askParent
1297+ maybe (Node.getFirstChild parent) Node.getNextSibling mLastNode >>= \case
1298+ Nothing -> do
1299+ HydrationRunnerT $ modify' $ \s -> s { _hydrationState_failed = True }
1300+ go Nothing Nothing
1301+ Just node -> DOM.castTo DOM.Comment node >>= \case
12941302 Just comment -> do
1295− commentText <- Node.getTextContentUnchecked comment
1296− case T.stripPrefix (prefix <> key0) commentText of
1303+ commentText <- fromMaybe (error "Cannot get text content of comment node") <$> Node.getTextContent comment
1304+ case T.stripPrefix (prefix <> key0) commentText of -- 'key0' may be @""@ in which case we're just finding the actual key; TODO: Don't be clever.
12971305 Just key -> do
12981306 -- Replace the comment with an (invisible) text node
12991307 tn <- createTextNode doc ("" :: Text)
13001308 Node.replaceChild_ parent tn comment
1301− pure (tn, key)
1309+ pure (tn, Just key)
13021310 Nothing -> do
1303− go key0 (Just node)
1311+ go (Just key0) (Just node)
13041312 Nothing -> do
1305− go key0 (Just node)
1306− switchComment = do
1307− key0 <- liftIO $ readIORef key0Ref
1308− (tn, key) <- go key0 =<< getPreviousNode
1309− setPreviousNode $ Just $ toNode tn
1310− liftIO $ do
1311− writeIORef textNodeRef tn
1312− writeIORef keyRef key
1313+ go (Just key0) (Just node)
1314+ switchComment = do
1315+ key0 <- liftIO $ readIORef key0Ref
1316+ (tn, key) <- go key0 =<< getPreviousNode
1317+ setPreviousNode $ Just $ toNode tn
1318+ liftIO $ do
1319+ writeIORef textNodeRef tn
1320+ writeIORef keyRef key
13131321 pure (switchComment, textNodeRef, keyRef)
13141322
13151323{-# INLINABLE skipToReplaceStart #-}
1316−skipToReplaceStart :: (MonadJSM m, Reflex t, MonadFix m, Adjustable t m, MonadHold t m, RawDocument (DomBuilderSpace (HydrationDomBuilderT s t m)) ~ Document) => HydrationDomBuilderT s t m (HydrationRunnerT t m (), IORef DOM.Text, IORef Text)
1317−skipToReplaceStart = skipToAndReplaceComment "replace-start" =<< liftIO (newIORef "")
1324+skipToReplaceStart :: (MonadJSM m, Reflex t, MonadFix m, Adjustable t m, MonadHold t m, RawDocument (DomBuilderSpace (HydrationDomBuilderT s t m)) ~ Document) => HydrationDomBuilderT s t m (HydrationRunnerT t m (), IORef DOM.Text, IORef (Maybe Text))
1325+skipToReplaceStart = skipToAndReplaceComment "replace-start" =<< liftIO (newIORef $ Just "") -- TODO: Don't rely on clever usage @""@ to make this work.
13181326
13191327{-# INLINABLE skipToReplaceEnd #-}
1320−skipToReplaceEnd :: (MonadJSM m, Reflex t, MonadFix m, Adjustable t m, MonadHold t m, RawDocument (DomBuilderSpace (HydrationDomBuilderT s t m)) ~ Document) => IORef Text -> HydrationDomBuilderT s t m (HydrationRunnerT t m (), IORef DOM.Text)
1328+skipToReplaceEnd :: (MonadJSM m, Reflex t, MonadFix m, Adjustable t m, MonadHold t m, RawDocument (DomBuilderSpace (HydrationDomBuilderT s t m)) ~ Document) => IORef (Maybe Text) -> HydrationDomBuilderT s t m (HydrationRunnerT t m (), IORef DOM.Text)
13211329skipToReplaceEnd key = fmap (\(m,e,_) -> (m,e)) $ skipToAndReplaceComment "replace-end" key
13221330
13231331instance SupportsHydrationDomBuilder t m => NotReady t (HydrationDomBuilderT s t m) where