@@ -6,6 +6,7 @@ import Reflex.Dom.Class
7import Reflex
7import Reflex
8import Reflex.Host.Class
8import Reflex.Host.Class
9+import Data.Functor.Misc
9import Data.Map (Map)
10import Data.Map (Map)
10import qualified Data.Map as Map
11import qualified Data.Map as Map
11import Data.Set (Set)
12import Data.Set (Set)
@@ -198,7 +199,7 @@ listWithKey vals mkChild = do
198 liftIO $ putStrLn "Triggering newChildren done"
199 liftIO $ putStrLn "Triggering newChildren done"
199 holdDyn Map.empty $ fmap (fmap (fst . fst)) newChildren
200 holdDyn Map.empty $ fmap (fmap (fst . fst)) newChildren
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))
202listWithKey' initialVals valsChanged mkChild = do
203listWithKey' initialVals valsChanged mkChild = do
203 doc <- askDocument
204 doc <- askDocument
204 startPlaceholder <- text' ""
205 startPlaceholder <- text' ""
@@ -206,20 +207,23 @@ listWithKey' initialVals valsChanged mkChild = do
206 (newChildren, newChildrenTriggerRef) <- newEventWithTriggerRef
207 (newChildren, newChildrenTriggerRef) <- newEventWithTriggerRef
207-- performEvent_ $ fmap (const $ return ()) newChildren --TODO: Get rid of this hack
208-- performEvent_ $ fmap (const $ return ()) newChildren --TODO: Get rid of this hack
208 runWidget <- getRunWidget
209 runWidget <- getRunWidget
210+ let childValChangedSelector :: EventSelector t (Const2 k v)
211+ childValChangedSelector = fanMap $ fmap (Map.mapMaybe id) valsChanged
209− let buildChild :: DocumentFragment -> k -> v -> WidgetHost m ((a, (Text, Text)), Event t (WidgetHost m ()))
212+ buildChild :: DocumentFragment -> k -> v -> WidgetHost m ((a, (Text, Text)), Event t (WidgetHost m ()))
210− buildChild df k v = runWidget df $ do
213+ buildChild df k v = runWidget df $ wrapChild k v
211 childStart <- text' ""
215 childStart <- text' ""
212− result <- mkChild k v
216+ result <- mkChild k v $ select childValChangedSelector $ Const2 k
213 childEnd <- text' ""
217 childEnd <- text' ""
214 return (result, (childStart, childEnd))
218 return (result, (childStart, childEnd))
215 Just df <- liftIO $ documentCreateDocumentFragment doc
219 Just df <- liftIO $ documentCreateDocumentFragment doc
216− initialState <- liftWidgetHost $ iforM initialVals $ buildChild df
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
217− children <- hold initialState $ traceEventWith (\x -> "newChildren: " <> show (Map.size x)) newChildren
221+ children <- holdDyn initialState $ traceEventWith (\x -> "newChildren: " <> show (Map.size x)) newChildren
218− addVoidAction $ switch $ fmap (mergeWith (>>) . map snd . Map.elems) children
222+ addVoidAction $ switch $ fmap (mergeWith (>>) . map snd . Map.elems) $ current children
219 Just p <- liftIO $ nodeGetParentNode endPlaceholder
223 Just p <- liftIO $ nodeGetParentNode endPlaceholder
220 liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
224 liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
221 addVoidAction $ flip fmap valsChanged $ \newVals -> do
225 addVoidAction $ flip fmap valsChanged $ \newVals -> do
222− curState <- sample children
226+ curState <- sample $ current children
223 --TODO: Should we remove the parent from the DOM first to avoid reflows?
227 --TODO: Should we remove the parent from the DOM first to avoid reflows?
224 newState <- liftM (Map.mapMaybe id) $ iforM (align curState newVals) $ \k -> \case
228 newState <- liftM (Map.mapMaybe id) $ iforM (align curState newVals) $ \k -> \case
225 These ((_, (start, end)), _) Nothing -> do -- Deleting child
229 These ((_, (start, end)), _) Nothing -> do -- Deleting child
@@ -248,7 +252,7 @@ listWithKey' initialVals valsChanged mkChild = do
248 liftIO $ putStrLn . ("newChildrenTriggerRef is Just? " <>) . show . isJust =<< readRef newChildrenTriggerRef
252 liftIO $ putStrLn . ("newChildrenTriggerRef is Just? " <>) . show . isJust =<< readRef newChildrenTriggerRef
249 runFrameWithTriggerRef newChildrenTriggerRef newState
253 runFrameWithTriggerRef newChildrenTriggerRef newState
250 liftIO $ putStrLn "Triggering newChildren done"
254 liftIO $ putStrLn "Triggering newChildren done"
251− holdDyn Map.empty $ fmap (fmap (fst . fst)) newChildren
255+ mapDyn (fmap (fst . fst)) children
253selectViewListWithKey_ :: 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)
257selectViewListWithKey_ :: 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)
254selectViewListWithKey_ selection vals mkChild = do
258selectViewListWithKey_ selection vals mkChild = do
@@ -306,6 +310,7 @@ wrapElement e = do
306 scrolled <- wrapDomEvent e elementOnscroll $ liftIO $ elementGetScrollTop e
310 scrolled <- wrapDomEvent e elementOnscroll $ liftIO $ elementGetScrollTop e
307 return $ El e clicked keypress scrolled
311 return $ El e clicked keypress scrolled
313+elDynAttr' :: forall t m a. MonadWidget t m => String -> Dynamic t (Map String String) -> m a -> m (El t, a)
309elDynAttr' elementTag attrs child = do
314elDynAttr' elementTag attrs child = do
310 (e, result) <- buildElement elementTag attrs child
315 (e, result) <- buildElement elementTag attrs child
311 e' <- wrapElement e
316 e' <- wrapElement e
@@ -318,11 +323,11 @@ elAttr elementTag attrs child = do
318 return result
323 return result
320{-# INLINABLE el' #-}
325{-# 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)
322el' tag child = elAttr' tag (Map.empty :: AttributeMap) child
327el' tag child = elAttr' tag (Map.empty :: AttributeMap) child
324{-# INLINABLE elAttr' #-}
329{-# 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)
326elAttr' elementTag attrs child = do
331elAttr' elementTag attrs child = do
327 (e, result) <- buildElement elementTag attrs child
332 (e, result) <- buildElement elementTag attrs child
328 e' <- wrapElement e
333 e' <- wrapElement e