on develop help sign in
Fix runWithReplace when the initial child isn't ready immediately
107129aRyan Trinkle
reflex-dom-core/src/Reflex/Dom/Builder/Immediate.hs+2−3
@@ -579,6 +579,7 @@ instance (Reflex t, MonadAdjust t m, MonadJSM m, MonadHold t m, PerformEvent t m
579 before <- textNodeInternal ("" :: Text)
579 before <- textNodeInternal ("" :: Text)
580 let parentUnreadyChildren = _immediateDomBuilderEnv_unreadyChildren initialEnv
580 let parentUnreadyChildren = _immediateDomBuilderEnv_unreadyChildren initialEnv
581 haveEverBeenReady <- liftIO $ newIORef False
581 haveEverBeenReady <- liftIO $ newIORef False
582+ currentCohort <- liftIO $ newIORef (-1 :: Int) -- Equal to the cohort currently in the DOM
582 let myCommitAction = do
583 let myCommitAction = do
583 liftIO (readIORef haveEverBeenReady) >>= \case
584 liftIO (readIORef haveEverBeenReady) >>= \case
584 True -> return ()
585 True -> return ()
@@ -604,14 +605,13 @@ instance (Reflex t, MonadAdjust t m, MonadJSM m, MonadHold t m, PerformEvent t m
604 0 -> writeIORef haveEverBeenReady True
605 0 -> writeIORef haveEverBeenReady True
605 _ -> modifyIORef' parentUnreadyChildren succ
606 _ -> modifyIORef' parentUnreadyChildren succ
606 return result
607 return result
607− currentCohort <- liftIO $ newIORef (0 :: Int)
608 a'' <- numberOccurrences a'
608 a'' <- numberOccurrences a'
609 (result0, result') <- lift $ runWithReplace drawInitialChild $ ffor a'' $ \(cohortId, child) -> do
609 (result0, result') <- lift $ runWithReplace drawInitialChild $ ffor a'' $ \(cohortId, child) -> do
610 df <- createDocumentFragmentUnchecked $ _immediateDomBuilderEnv_document initialEnv
610 df <- createDocumentFragmentUnchecked $ _immediateDomBuilderEnv_document initialEnv
611 unreadyChildren <- liftIO $ newIORef 0
611 unreadyChildren <- liftIO $ newIORef 0
612 let commitAction = do
612 let commitAction = do
613 c <- liftIO $ readIORef currentCohort
613 c <- liftIO $ readIORef currentCohort
614− when (c < cohortId) $ do -- If a newer cohort has already been committed, just ignore this
614+ when (c <= cohortId) $ do -- If a newer cohort has already been committed, just ignore this
615 deleteBetweenExclusive before after
615 deleteBetweenExclusive before after
616 insertBefore df after
616 insertBefore df after
617 liftIO $ writeIORef currentCohort cohortId
617 liftIO $ writeIORef currentCohort cohortId
@@ -621,7 +621,6 @@ instance (Reflex t, MonadAdjust t m, MonadJSM m, MonadHold t m, PerformEvent t m
621 , _immediateDomBuilderEnv_unreadyChildren = unreadyChildren
621 , _immediateDomBuilderEnv_unreadyChildren = unreadyChildren
622 , _immediateDomBuilderEnv_commitAction = commitAction
622 , _immediateDomBuilderEnv_commitAction = commitAction
623 }
623 }
624−
625 liftIO (readIORef unreadyChildren) >>= \case
624 liftIO (readIORef unreadyChildren) >>= \case
626 0 -> liftJSM commitAction
625 0 -> liftJSM commitAction
627 _ -> return () -- Whoever decrements it to 0 will handle it
626 _ -> return () -- Whoever decrements it to 0 will handle it