on develop help sign in
Use an IORef instead of the 'children' Behavior to keep track of state in listHoldWithKey
commitbe7d7c0503bf86d4eda81f0d7df273b7a61124ec authorRyan Trinkle <ryan.trinkle@gmail.com> dateSat 29 Aug 2015, 15:21 (11 years ago) parent4163c31
1 file changed+4−1
src/Reflex/Dom/Widget/Basic.hs+4−1
src/Reflex/Dom/Widget/Basic.hs+4−1
@@ -34,6 +34,7 @@ import Data.Maybe
3434import Data.GADT.Compare.TH
3535import Data.Bitraversable
3636import GHCJS.DOM.MouseEvent
37+import Data.IORef
3738
3839type AttributeMap = Map String String
3940
@@ -205,12 +206,13 @@ listHoldWithKey initialVals valsChanged mkChild = do
205206 return (result, (childStart, childEnd))
206207 Just dfOrig <- liftIO $ documentCreateDocumentFragment doc
207208 initialState <- iforM initialVals $ \k v -> subWidgetWithVoidActions (toNode dfOrig) $ wrapChild k v --Note: we have to use subWidgetWithVoidActions rather than runWidget here, because running post-build actions during build can cause not-yet-constructed values to be read
209+ stateRef <- liftIO $ newIORef initialState
208210 children <- holdDyn initialState newChildren
209211 addVoidAction $ switch $ fmap (mergeWith (>>) . map snd . Map.elems) $ current children
210212 Just pOrig <- liftIO $ nodeGetParentNode endPlaceholder
211213 _ <- liftIO $ nodeInsertBefore pOrig (Just dfOrig) (Just endPlaceholder)
212214 addVoidAction $ flip fmap valsChanged $ \newVals -> do
213− curState <- sample $ current children
215+ curState <- liftIO $ readIORef stateRef
214216 --TODO: Should we remove the parent from the DOM first to avoid reflows?
215217 (newState, postBuild) <- flip runStateT (return ()) $ liftM (Map.mapMaybe id) $ iforM (align curState newVals) $ \k -> \case
216218 These ((_, (start, end)), _) Nothing -> do -- Deleting child
@@ -239,6 +241,7 @@ listHoldWithKey initialVals valsChanged mkChild = do
239241 return $ Just s
240242 This state -> do -- No change
241243 return $ Just state
244+ liftIO $ writeIORef stateRef newState
242245 runFrameWithTriggerRef newChildrenTriggerRef newState
243246 postBuild
244247 mapDyn (fmap (fst . fst)) children