on develop help sign in
fix listWithKey unnecessarily rebuilding its children
commit9626d64a734bbbad2e0ef444884377b193f5901f authorDan Haraj <dan@obsidian.systems> dateMon 6 Feb 2017, 17:26 (10 years ago) parent7669159
1 file changed+8−1
src/Reflex/Dom/Widget/Basic.hs+8−1
src/Reflex/Dom/Widget/Basic.hs+8−1
@@ -212,9 +212,16 @@ listWithKey :: forall t k v m a. (Ord k, DomBuilder t m, PostBuild t m, MonadFix
212212listWithKey vals mkChild = do
213213 postBuild <- getPostBuild
214214 let childValChangedSelector = fanMap $ updated vals
215+ -- We keep track of changes to children values in the mkChild function we pass to listHoldWithKey
216+ -- The other changes we need to keep track of are child insertions and deletions. diffOnlyKeyChanges
217+ -- keeps track of insertions and deletions but ignores value changes, since they're already accounted for.
218+ diffOnlyKeyChanges olds news = flip Map.mapMaybe (align olds news) $ \case
219+ This _ -> Just Nothing
220+ These _ _ -> Nothing
221+ That new -> Just $ Just new
215222 rec sentVals :: Dynamic t (Map k v) <- foldDyn applyMap Map.empty changeVals
216223 let changeVals :: Event t (Map k (Maybe v))
217− changeVals = attachWith diffMapNoEq (current sentVals) $ leftmost
224+ changeVals = attachWith diffOnlyKeyChanges (current sentVals) $ leftmost
218225 [ updated vals
219226 , tag (current vals) postBuild --TODO: This should probably be added to the attachWith, not to the updated; if we were using diffMap instead of diffMapNoEq, I think it might not work
220227 ]