@@ -987,10 +987,10 @@ instance (Reflex t, Adjustable t m, MonadJSM m, MonadHold t m, MonadFix m, PrimM
987987 traverseDMapWithKeyWithAdjust = traverseDMapWithKeyWithAdjust'
988988 {-# INLINABLE traverseDMapWithKeyWithAdjustWithMove #-}
989989 traverseDMapWithKeyWithAdjustWithMove = do
990− let updateChildUnreadiness (p :: PatchDMapWithMove k (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v')) old = do
991− let new :: forall a. k a -> PatchDMapWithMove.NodeInfo k (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') a -> IO (PatchDMapWithMove.NodeInfo k (Constant (IORef (ChildReadyState k))) a)
990+ let updateChildUnreadiness (p :: PatchDMapWithMove k (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v')) old = do
991+ let new :: forall a. k a -> PatchDMapWithMove.NodeInfo k (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') a -> IO (PatchDMapWithMove.NodeInfo k (Constant (IORef (ChildReadyState k))) a)
992992 new k = PatchDMapWithMove.nodeInfoMapFromM $ \case
993− PatchDMapWithMove.From_Insert (Compose (_, _, sRef, _)) -> do
993+ PatchDMapWithMove.From_Insert (Compose (_, _, _, sRef, _)) -> do
994994 readIORef sRef >>= \case
995995 ChildReadyState_Ready -> return PatchDMapWithMove.From_Delete
996996 ChildReadyState_Unready _ -> do
@@ -1005,11 +1005,11 @@ instance (Reflex t, Adjustable t m, MonadJSM m, MonadHold t m, MonadFix m, PrimM
10051005 p' <- fmap unsafePatchDMapWithMove $ DMap.traverseWithKey new $ unPatchDMapWithMove p
10061006 _ <- DMap.traverseWithKey deleteOrMove $ PatchDMapWithMove.getDeletionsAndMoves p old
10071007 return $ applyAlways p' old
1008− hoistTraverseWithKeyWithAdjust traverseDMapWithKeyWithAdjustWithMove mapPatchDMapWithMove updateChildUnreadiness $ \placeholders lastPlaceholderRef (p_ :: PatchDMapWithMove k (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v')) -> do
1008+ hoistTraverseWithKeyWithAdjust traverseDMapWithKeyWithAdjustWithMove mapPatchDMapWithMove updateChildUnreadiness $ \placeholders lastPlaceholderRef (p_ :: PatchDMapWithMove k (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v')) -> do
10091009 let p = unPatchDMapWithMove p_
10101010 phsBefore <- liftIO $ readIORef placeholders
10111011 lastPlaceholder <- liftIO $ readIORef lastPlaceholderRef
1012− let collectIfMoved :: forall a. k a -> PatchDMapWithMove.NodeInfo k (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') a -> JSM (Constant (Maybe DOM.DocumentFragment) a)
1012+ let collectIfMoved :: forall a. k a -> PatchDMapWithMove.NodeInfo k (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') a -> JSM (Constant (Maybe DOM.DocumentFragment) a)
10131013 collectIfMoved k e = do
10141014 let mThisPlaceholder = Map.lookup (Some.This k) phsBefore -- Will be Nothing if this element wasn't present before
10151015 nextPlaceholder = maybe lastPlaceholder snd $ Map.lookupGT (Some.This k) phsBefore
@@ -1020,12 +1020,12 @@ instance (Reflex t, Adjustable t m, MonadJSM m, MonadHold t m, MonadFix m, PrimM
10201020 True -> do
10211021 Constant <$> mapM (`collectUpTo` nextPlaceholder) mThisPlaceholder
10221022 collected <- DMap.traverseWithKey collectIfMoved p
1023− let !phsAfter = fromMaybe phsBefore $ apply (weakenPatchDMapWithMoveWith (\(Compose (_, ph, _, _)) -> ph) p_) phsBefore --TODO: Don't recompute this
1024− let placeFragment :: forall a. k a -> PatchDMapWithMove.NodeInfo k (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') a -> JSM (Constant () a)
1023+ let !phsAfter = fromMaybe phsBefore $ apply (weakenPatchDMapWithMoveWith (\(Compose (_, _, ph, _, _)) -> ph) p_) phsBefore --TODO: Don't recompute this
1024+ let placeFragment :: forall a. k a -> PatchDMapWithMove.NodeInfo k (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') a -> JSM (Constant () a)
10251025 placeFragment k e = do
10261026 let nextPlaceholder = maybe lastPlaceholder snd $ Map.lookupGT (Some.This k) phsAfter
10271027 case PatchDMapWithMove._nodeInfo_from e of
1028− PatchDMapWithMove.From_Insert (Compose (df, _, _, _)) -> do
1028+ PatchDMapWithMove.From_Insert (Compose (_, df, _, _, _)) -> do
10291029 df `insertBefore` nextPlaceholder
10301030 PatchDMapWithMove.From_Delete -> do
10311031 return ()
@@ -1044,11 +1044,11 @@ traverseDMapWithKeyWithAdjust'
10441044 -> Event t (PatchDMap k v)
10451045 -> HydrationDomBuilderT t m (DMap k v', Event t (PatchDMap k v'))
10461046traverseDMapWithKeyWithAdjust' = do
1047− let updateChildUnreadiness (p :: PatchDMap k (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v')) old = do
1048− let new :: forall a. k a -> ComposeMaybe (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') a -> IO (ComposeMaybe (Constant (IORef (ChildReadyState k))) a)
1047+ let updateChildUnreadiness (p :: PatchDMap k (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v')) old = do
1048+ let new :: forall a. k a -> ComposeMaybe (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') a -> IO (ComposeMaybe (Constant (IORef (ChildReadyState k))) a)
10491049 new k (ComposeMaybe m) = ComposeMaybe <$> case m of
10501050 Nothing -> return Nothing
1051− Just (Compose (_, _, sRef, _)) -> do
1051+ Just (Compose (_, _, _, sRef, _)) -> do
10521052 readIORef sRef >>= \case
10531053 ChildReadyState_Ready -> return Nothing -- Delete this child, since it's ready
10541054 ChildReadyState_Unready _ -> do
@@ -1066,8 +1066,8 @@ traverseDMapWithKeyWithAdjust' = do
10661066 lastPlaceholder <- liftIO $ readIORef lastPlaceholderRef
10671067 let nextPlaceholder = maybe lastPlaceholder snd $ Map.lookupGT (Some.This k) phs
10681068 forM_ (Map.lookup (Some.This k) phs) $ \thisPlaceholder -> thisPlaceholder `deleteUpTo` nextPlaceholder
1069− forM_ mv $ \(Compose (df, _, _, _)) -> df `insertBefore` nextPlaceholder
1070− liftIO $ writeIORef placeholders $! fromMaybe phs $ apply (weakenPatchDMapWith (\(Compose (_, ph, _, _)) -> ph) $ PatchDMap p) phs
1069+ forM_ mv $ \(Compose (_, df, _, _, _)) -> df `insertBefore` nextPlaceholder
1070+ liftIO $ writeIORef placeholders $! fromMaybe phs $ apply (weakenPatchDMapWith (\(Compose (_, _, ph, _, _)) -> ph) $ PatchDMap p) phs
10711071
10721072{-# INLINABLE traverseIntMapWithKeyWithAdjust' #-}
10731073traverseIntMapWithKeyWithAdjust'
@@ -1191,6 +1191,13 @@ hoistTraverseIntMapWithKeyWithAdjust base updateChildUnreadiness applyDomUpdate_
11911191 applyDomUpdate newP
11921192 return (result0, result')
11931193
1194+-- traverseDMapWithKeyWithAdjust
1195+-- :: GCompare k
1196+-- => (forall a. k a -> v a -> m (v' a))
1197+-- -> DMap k v
1198+-- -> Event t (PatchDMap k v)
1199+-- -> m (DMap k v', Event t (PatchDMap k v'))
1200+
11941201{-# INLINABLE hoistTraverseWithKeyWithAdjust #-}
11951202hoistTraverseWithKeyWithAdjust :: forall (k :: * -> *) v v' t m p.
11961203 ( Adjustable t m
@@ -1202,7 +1209,9 @@ hoistTraverseWithKeyWithAdjust :: forall (k :: * -> *) v v' t m p.
12021209 , MonadFix m
12031210 , Patch (p k v)
12041211 , PatchTarget (p k (Constant Int)) ~ DMap k (Constant Int)
1205− , Monoid (p k (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v'))
1212+ , Patch (p k (Const (DOM t m)))
1213+ , PatchTarget (p k (Const (DOM t m))) ~ DMap k (Const (DOM t m))
1214+ , Monoid (p k (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v'))
12061215 , Patch (p k (Constant Int))
12071216 )
12081217 => (forall vv vv'.
@@ -1212,8 +1221,8 @@ hoistTraverseWithKeyWithAdjust :: forall (k :: * -> *) v v' t m p.
12121221 -> DomRenderHookT t m (DMap k vv', Event t (p k vv'))
12131222 ) -- ^ The base monad's traversal
12141223 -> (forall vv vv'. (forall a. vv a -> vv' a) -> p k vv -> p k vv') -- ^ A way of mapping over the patch type
1215− -> (p k (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') -> DMap k (Constant (IORef (ChildReadyState k))) -> IO (DMap k (Constant (IORef (ChildReadyState k))))) -- ^ Given a patch for the children DOM elements, produce a patch for the childrens' unreadiness state
1216− -> (IORef (Map.Map (Some.Some k) DOM.Text) -> IORef DOM.Text -> p k (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') -> JSM ()) -- ^ Apply a patch to the DOM
1224+ -> (p k (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') -> DMap k (Constant (IORef (ChildReadyState k))) -> IO (DMap k (Constant (IORef (ChildReadyState k))))) -- ^ Given a patch for the children DOM elements, produce a patch for the childrens' unreadiness state
1225+ -> (IORef (Map.Map (Some.Some k) DOM.Text) -> IORef DOM.Text -> p k (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v') -> JSM ()) -- ^ Apply a patch to the DOM
12171226 -> (forall a. k a -> v a -> HydrationDomBuilderT t m (v' a))
12181227 -> DMap k v
12191228 -> Event t (p k v)
@@ -1222,7 +1231,7 @@ hoistTraverseWithKeyWithAdjust base mapPatch updateChildUnreadiness applyDomUpda
12221231 (hydrateStart, _, key) <- skipToReplaceStart
12231232 initialEnv <- HydrationDomBuilderT ask
12241233 let parentUnreadyChildren = _hydrationDomBuilderEnv_unreadyChildren initialEnv
1225− pendingChange :: IORef (DMap k (Constant (IORef (ChildReadyState k))), p k (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v')) <- liftIO $ newIORef mempty
1234+ pendingChange :: IORef (DMap k (Constant (IORef (ChildReadyState k))), p k (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v')) <- liftIO $ newIORef mempty
12261235 haveEverBeenReady <- liftIO $ newIORef False
12271236 placeholders <- liftIO $ newIORef $ error "placeholders not yet initialized"
12281237 lastPlaceholderRef <- liftIO $ newIORef $ error "lastPlaceholderRef not yet initialized"
@@ -1254,10 +1263,9 @@ hoistTraverseWithKeyWithAdjust base mapPatch updateChildUnreadiness applyDomUpda
12541263 liftIO $ writeIORef pendingChange (newUnready, p)
12551264 when (DMap.null newUnready) $ do
12561265 applyDomUpdate p
1257− stateRef <- liftIO . newIORef =<< HydrationDomBuilderT get
1258− (children0, children') <- HydrationDomBuilderT $ lift $ lift $ base (\k v -> drawChildUpdate stateRef initialEnv markChildReady $ f k v) dm0 dm'
1259− HydrationDomBuilderT . put =<< liftIO (readIORef stateRef)
1260− let processChild k (Compose (_, _, sRef, _)) = ComposeMaybe <$> do
1266+ (children0, children') <- HydrationDomBuilderT $ lift $ lift $ base (\k v -> drawChildUpdate initialEnv markChildReady $ f k v) dm0 dm'
1267+-- HydrationDomBuilderT . put =<< liftIO (readIORef stateRef)
1268+ let processChild k (Compose (_, _, _, sRef, _)) = ComposeMaybe <$> do
12611269 readIORef sRef >>= \case
12621270 ChildReadyState_Ready -> return Nothing
12631271 ChildReadyState_Unready _ -> do
@@ -1269,12 +1277,19 @@ hoistTraverseWithKeyWithAdjust base mapPatch updateChildUnreadiness applyDomUpda
12691277 else do
12701278 modifyIORef' parentUnreadyChildren succ
12711279 writeIORef pendingChange (initialUnready, mempty) -- The patch is always empty because it got applied implicitly when we ran the children the first time
1272− let result0 = DMap.map (\(Compose (_, _, _, v)) -> v) children0
1273− placeholders0 = weakenDMapWith (\(Compose (_, ph, _, _)) -> ph) children0
1274− result' = ffor children' $ mapPatch $ \(Compose (_, _, _, r)) -> r
1280+ let result0 = DMap.map (\(Compose (_, _, _, _, v)) -> v) children0
1281+ placeholders0 = weakenDMapWith (\(Compose (_, _, ph, _, _)) -> ph) children0
1282+ delayed0 :: DMap k (Const (DOM t m))
1283+ delayed0 = DMap.map (\(Compose (delayed, _, _, _, _)) -> Const delayed) children0
1284+ delayed' :: Event t (p k (Const (DOM t m)))
1285+ delayed' = ffor children' $ mapPatch $ \(Compose (d, _, _, _, _)) -> Const d
1286+ result' = ffor children' $ mapPatch $ \(Compose (_, _, _, _, r)) -> r
1287+ delayed <- accumMaybeB (flip apply) delayed0 delayed'
1288+ hydrateDOM $ DOM_Replace (liftIO $ putStr "one") (liftIO $ putStr "two")
1289+ (fmap (\(_ :=> Const d) -> d) . DMap.toList <$> delayed)
12751290 liftIO $ writeIORef placeholders $! placeholders0
12761291 getHydrationMode >>= \case
1277− HydrationMode_Immediate -> void $ DMap.traverseWithKey (\_ (Compose (df, _, _, _)) -> Constant () <$ append (toNode df)) children0
1292+ HydrationMode_Immediate -> void $ DMap.traverseWithKey (\_ (Compose (_, df, _, _, _)) -> Constant () <$ append (toNode df)) children0
12781293 _ -> pure ()
12791294 (hydrateEnd, lastPlaceholderRef) <- skipToReplaceEnd key
12801295 requestDomAction_ $ ffor children' $ \p -> do
@@ -1287,18 +1302,16 @@ hoistTraverseWithKeyWithAdjust base mapPatch updateChildUnreadiness applyDomUpda
12871302 return (result0, result')
12881303
12891304{-# INLINABLE drawChildUpdate #-}
1290−drawChildUpdate :: (MonadIO m, MonadJSM m)
1291− => IORef [DOM t m]
1292− -> HydrationDomBuilderEnv
1305+drawChildUpdate :: (MonadIO m, MonadJSM m, Reflex t)
1306+ => HydrationDomBuilderEnv
12931307 -> (IORef (ChildReadyState k) -> JSM ()) -- This will NOT be called if the child is ready at initialization time; instead, the ChildReadyState return value will be ChildReadyState_Ready
12941308 -> HydrationDomBuilderT t m (v' a)
1295− -> DomRenderHookT t m (Compose ((,,,) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v' a)
1296−drawChildUpdate stateRef initialEnv markReady child = do
1309+ -> DomRenderHookT t m (Compose ((,,,,) (DOM t m) DOM.DocumentFragment DOM.Text (IORef (ChildReadyState k))) v' a)
1310+drawChildUpdate initialEnv markReady child = do
12971311 childReadyState <- liftIO $ newIORef $ ChildReadyState_Unready Nothing
12981312 let doc = _hydrationDomBuilderEnv_document initialEnv
12991313 unreadyChildren <- liftIO $ newIORef 0
13001314 hydrationMode <- liftIO $ readIORef $ _hydrationDomBuilderEnv_hydrationMode initialEnv
1301− initialState <- liftIO $ readIORef stateRef
13021315 placeholder <- createTextNode doc ("" :: Text)
13031316 p <- case hydrationMode of
13041317 HydrationMode_Hydrating -> do
@@ -1313,16 +1326,18 @@ drawChildUpdate stateRef initialEnv markReady child = do
13131326 Node.appendChild_ df placeholder
13141327 pure (Right df)
13151328 p' <- either pure (liftIO . newIORef . toNode) p
1316− let env = initialEnv
1329+ (result, finalState) <- flip runStateT [] $ runReaderT (unHydrationDomBuilderT child) initialEnv
13171330 { _hydrationDomBuilderEnv_parent = p'
13181331 , _hydrationDomBuilderEnv_unreadyChildren = unreadyChildren
13191332 , _hydrationDomBuilderEnv_commitAction = markReady childReadyState
13201333 }
1321− (result, finalState) <- runStateT (runReaderT (unHydrationDomBuilderT child) env) initialState-- { _hydrationState_previousNode = Just $ toNode placeholder })
1322− liftIO $ writeIORef stateRef finalState
13231334 u <- liftIO $ readIORef unreadyChildren
13241335 when (u == 0) $ liftIO $ writeIORef childReadyState ChildReadyState_Ready
1325− return (Compose (either (const $ error "drawChildUpdate: Tried to use document fragment in hydration mode") id p, placeholder, childReadyState, result))
1336+ return $ Compose
1337+ ( DOM_Replace (liftIO $ putStrLn "before") (liftIO $ putStrLn "after") $ pure finalState
1338+ , either (const $ error "drawChildUpdate: Tried to use document fragment in hydration mode") id p
1339+ , placeholder, childReadyState, result
1340+ )
13261341
13271342{-# INLINABLE drawChildUpdateInt #-}
13281343drawChildUpdateInt :: (MonadIO m, MonadJSM m)