@@ -6,6 +6,7 @@ import Reflex.Dom.Class
66
77import Reflex
88import Reflex.Host.Class
9+import Data.Functor.Misc
910import Data.Map (Map)
1011import qualified Data.Map as Map
1112import Data.Set (Set)
@@ -198,7 +199,7 @@ listWithKey vals mkChild = do
198199 liftIO $ putStrLn "Triggering newChildren done"
199200 holdDyn Map.empty $ fmap (fmap (fst . fst)) newChildren
200201
201−listWithKey' :: forall t m k v a. (Ord k, MonadWidget t m) => Map k v -> Event t (Map k (Maybe v)) -> (k -> v -> m a) -> m (Dynamic t (Map k a))
202+listWithKey' :: forall t m k v a. (Ord k, MonadWidget t m) => Map k v -> Event t (Map k (Maybe v)) -> (k -> v -> Event t v -> m a) -> m (Dynamic t (Map k a))
202203listWithKey' initialVals valsChanged mkChild = do
203204 doc <- askDocument
204205 startPlaceholder <- text' ""
@@ -206,20 +207,23 @@ listWithKey' initialVals valsChanged mkChild = do
206207 (newChildren, newChildrenTriggerRef) <- newEventWithTriggerRef
207208-- performEvent_ $ fmap (const $ return ()) newChildren --TODO: Get rid of this hack
208209 runWidget <- getRunWidget
209− let buildChild :: DocumentFragment -> k -> v -> WidgetHost m ((a, (Text, Text)), Event t (WidgetHost m ()))
210− buildChild df k v = runWidget df $ do
210+ let childValChangedSelector :: EventSelector t (Const2 k v)
211+ childValChangedSelector = fanMap $ fmap (Map.mapMaybe id) valsChanged
212+ buildChild :: DocumentFragment -> k -> v -> WidgetHost m ((a, (Text, Text)), Event t (WidgetHost m ()))
213+ buildChild df k v = runWidget df $ wrapChild k v
214+ wrapChild k v = do
211215 childStart <- text' ""
212− result <- mkChild k v
216+ result <- mkChild k v $ select childValChangedSelector $ Const2 k
213217 childEnd <- text' ""
214218 return (result, (childStart, childEnd))
215219 Just df <- liftIO $ documentCreateDocumentFragment doc
216− initialState <- liftWidgetHost $ iforM initialVals $ buildChild df
217− children <- hold initialState $ traceEventWith (\x -> "newChildren: " <> show (Map.size x)) newChildren
218− addVoidAction $ switch $ fmap (mergeWith (>>) . map snd . Map.elems) children
220+ initialState <- iforM initialVals $ \k v -> subWidgetWithVoidActions (toNode df) $ 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
221+ children <- holdDyn initialState $ traceEventWith (\x -> "newChildren: " <> show (Map.size x)) newChildren
222+ addVoidAction $ switch $ fmap (mergeWith (>>) . map snd . Map.elems) $ current children
219223 Just p <- liftIO $ nodeGetParentNode endPlaceholder
220224 liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
221225 addVoidAction $ flip fmap valsChanged $ \newVals -> do
222− curState <- sample children
226+ curState <- sample $ current children
223227 --TODO: Should we remove the parent from the DOM first to avoid reflows?
224228 newState <- liftM (Map.mapMaybe id) $ iforM (align curState newVals) $ \k -> \case
225229 These ((_, (start, end)), _) Nothing -> do -- Deleting child
@@ -248,7 +252,7 @@ listWithKey' initialVals valsChanged mkChild = do
248252 liftIO $ putStrLn . ("newChildrenTriggerRef is Just? " <>) . show . isJust =<< readRef newChildrenTriggerRef
249253 runFrameWithTriggerRef newChildrenTriggerRef newState
250254 liftIO $ putStrLn "Triggering newChildren done"
251− holdDyn Map.empty $ fmap (fmap (fst . fst)) newChildren
255+ mapDyn (fmap (fst . fst)) children
252256
253257selectViewListWithKey_ :: forall t m k v a. (MonadWidget t m, Ord k) => Dynamic t k -> Dynamic t (Map k v) -> (k -> Dynamic t v -> Dynamic t Bool -> m (Event t a)) -> m (Event t k)
254258selectViewListWithKey_ selection vals mkChild = do
@@ -306,6 +310,7 @@ wrapElement e = do
306310 scrolled <- wrapDomEvent e elementOnscroll $ liftIO $ elementGetScrollTop e
307311 return $ El e clicked keypress scrolled
308312
313+elDynAttr' :: forall t m a. MonadWidget t m => String -> Dynamic t (Map String String) -> m a -> m (El t, a)
309314elDynAttr' elementTag attrs child = do
310315 (e, result) <- buildElement elementTag attrs child
311316 e' <- wrapElement e
@@ -318,11 +323,11 @@ elAttr elementTag attrs child = do
318323 return result
319324
320325{-# INLINABLE el' #-}
321−--el' :: forall t m a. MonadWidget t m => String -> m a -> m (El t, a)
326+el' :: forall t m a. MonadWidget t m => String -> m a -> m (El t, a)
322327el' tag child = elAttr' tag (Map.empty :: AttributeMap) child
323328
324329{-# INLINABLE elAttr' #-}
325−--elAttr' :: forall t m a. MonadWidget t m => String -> Map String String -> m a -> m (El t, a)
330+elAttr' :: forall t m a. MonadWidget t m => String -> Map String String -> m a -> m (El t, a)
326331elAttr' elementTag attrs child = do
327332 (e, result) <- buildElement elementTag attrs child
328333 e' <- wrapElement e