@@ -9,6 +9,7 @@
99{-# LANGUAGE GADTs #-}
1010{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1111{-# LANGUAGE LambdaCase #-}
12+{-# LANGUAGE MultiWayIf #-}
1213{-# LANGUAGE RankNTypes #-}
1314{-# LANGUAGE RoleAnnotations #-}
1415{-# LANGUAGE ScopedTypeVariables #-}
@@ -19,7 +20,7 @@
1920{-# LANGUAGE DeriveFunctor #-}
2021{-# LANGUAGE StandaloneDeriving #-}
2122{-# LANGUAGE InstanceSigs #-}
22−{-# LANGUAGE MultiWayIf #-}
23+{-# LANGUAGE RecursiveDo #-}
2324
2425#ifdef USE_REFLEX_OPTIMIZER
2526{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
@@ -38,17 +39,15 @@ import Control.Monad hiding (forM, forM_, mapM, mapM_)
3839import Control.Monad.Exception
3940import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
4041import Control.Monad.Primitive
42+--import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_)
4143import Control.Monad.Reader.Class
4244import Control.Monad.IO.Class
4345import Control.Monad.ReaderIO
4446import Control.Monad.Ref
45−import Control.Monad.Fail (MonadFail)
46−import qualified Control.Monad.Fail as MonadFail
4747import Data.Align
4848import Data.Coerce
49−import Data.Dependent.Map (DMap)
49+import Data.Dependent.Map (DMap, DSum (..))
5050import qualified Data.Dependent.Map as DMap
51−import Data.Dependent.Sum (DSum (..))
5251import Data.FastMutableIntMap (FastMutableIntMap, PatchIntMap (..))
5352import qualified Data.FastMutableIntMap as FastMutableIntMap
5453import Data.Foldable hiding (concat, elem, sequence_)
@@ -65,7 +64,6 @@ import Data.Monoid (mempty, (<>))
6564import Data.Proxy
6665import Data.These
6766import Data.Traversable
68−import Data.Type.Equality ((:~:)(Refl))
6967import Data.Witherable (Filterable, mapMaybe)
7068import GHC.Exts hiding (toList)
7169import GHC.IORef (IORef (..))
@@ -106,21 +104,15 @@ import qualified Reflex.Class
106104import qualified Reflex.Class as R
107105import qualified Reflex.Host.Class
108106import Reflex.NotReady.Class
109−import Data.Patch
110−import qualified Data.Patch.DMapWithMove as PatchDMapWithMove
107+import Reflex.Patch
108+import qualified Reflex.Patch.DMapWithMove as PatchDMapWithMove
111109import Reflex.PerformEvent.Base (PerformEventT)
112110
113111#ifdef DEBUG_TRACE_EVENTS
114112import qualified Data.ByteString.Char8 as BS8
115113import System.IO (stderr)
116114import Data.List (isPrefixOf)
117−#endif
118115
119−-- TODO stdout might not be the best channel for debug output
120−debugStrLn :: String -> IO ()
121−debugStrLn = putStrLn
122−
123−#ifdef DEBUG_TRACE_EVENTS
124116withStackOneLine :: (BS8.ByteString -> a) -> a
125117withStackOneLine expr = unsafePerformIO $ do
126118 stack <- currentCallStack
@@ -129,33 +121,9 @@ withStackOneLine expr = unsafePerformIO $ do
129121
130122#endif
131123
132−debugPropagate :: Bool
133−
134−debugInvalidateHeight :: Bool
135−
136−debugInvalidate :: Bool
137−
138124#ifdef DEBUG
139125#define DEBUG_NODEIDS
140126
141−#ifdef DEBUG_TRACE_PROPAGATION
142−debugPropagate = True
143−#else
144−debugPropagate = False
145−#endif
146−
147−#ifdef DEBUG_TRACE_HEIGHT
148−debugInvalidateHeight = True
149−#else
150−debugInvalidateHeight = False
151−#endif
152−
153−#ifdef DEBUG_TRACE_INVALIDATION
154−debugInvalidate = True
155−#else
156−debugInvalidate = False
157−#endif
158−
159127class HasNodeId a where
160128 getNodeId :: a -> Int
161129
@@ -193,10 +161,6 @@ showNodeId' = ("#"<>) . show
193161
194162#else
195163
196−debugPropagate = False
197−debugInvalidateHeight = False
198−debugInvalidate = False
199−
200164-- This must be inline, or error messages will cause memory leaks due to retaining the node in question
201165{-# INLINE showNodeId #-}
202166showNodeId :: a -> String
@@ -253,11 +217,11 @@ subscribeAndRead = unEvent
253217{-# INLINE [1] pushCheap #-}
254218pushCheap :: HasSpiderTimeline x => (a -> ComputeM x (Maybe b)) -> Event x a -> Event x b
255219pushCheap !f e = Event $ \sub -> do
256− (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" $ sub
220+ (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" (sub
257221 { subscriberPropagate = \a -> do
258222 mb <- f a
259223 mapM_ (subscriberPropagate sub) mb
260− }
224+ })
261225 occ' <- join <$> mapM f occ
262226 return (subscription, occ')
263227
@@ -307,20 +271,7 @@ data CacheSubscribed x a
307271#endif
308272 }
309273
310−nowSpiderEventM :: (HasSpiderTimeline x) => EventM x (R.Event (SpiderTimeline x) ())
311−nowSpiderEventM =
312− SpiderEvent <$> now
313−
314−now :: (MonadIO m, Defer (Some Clear) m, HasSpiderTimeline x
315− ) => m (Event x ())
316−now = do
317− nowOrNot <- liftIO $ newIORef $ Just ()
318− scheduleClear nowOrNot
319− return . Event $ \_ -> do
320− occ <- liftIO . readIORef $ nowOrNot
321− return ( EventSubscription (return ()) eventSubscribedNow
322− , occ
323− )
274+
324275
325276-- | Construct an 'Event' whose value is guaranteed not to be recomputed
326277-- repeatedly
@@ -464,6 +415,16 @@ data Subscriber x a = Subscriber
464415 , subscriberRecalculateHeight :: !(Height -> IO ())
465416 }
466417
418+--TODO: Move this comment to WeakBag
419+-- These function are constructor functions that are marked NOINLINE so they are
420+-- opaque to GHC. If we do not do this, then GHC will sometimes fuse the constructor away
421+-- so any weak references that are attached to the constructors will have their
422+-- finalizer run. Using the opaque constructor, does not see the
423+-- constructor application, so it behaves like an IORef and cannot be fused away.
424+--
425+-- The result is also evaluated to WHNF, since forcing a thunk invalidates
426+-- the weak pointer to it in some cases.
427+
467428newSubscriberHold :: (HasSpiderTimeline x, Patch p) => Hold x p -> IO (Subscriber x p)
468429newSubscriberHold h = return $ Subscriber
469430 { subscriberPropagate = {-# SCC "traverseHold" #-} propagateSubscriberHold h
@@ -607,16 +568,6 @@ eventSubscribedNever = EventSubscribed
607568 , eventSubscribedWhoCreated = return ["never"]
608569#endif
609570 }
610−eventSubscribedNow :: EventSubscribed x
611−eventSubscribedNow = EventSubscribed
612− { eventSubscribedHeightRef = zeroRef
613− , eventSubscribedRetained = toAny ()
614−#ifdef DEBUG_CYCLES
615− , eventSubscribedGetParents = return []
616− , eventSubscribedHasOwnHeightRef = False
617− , eventSubscribedWhoCreated = return ["now"]
618−#endif
619− }
620571
621572eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
622573eventSubscribedFan !subscribed = EventSubscribed
@@ -994,10 +945,8 @@ instance Monad (BehaviorM x) where
994945 BehaviorM x >> BehaviorM y = BehaviorM $ x >> y
995946 {-# INLINE return #-}
996947 return x = BehaviorM $ return x
997−#if !MIN_VERSION_base(4,13,0)
998948 {-# INLINE fail #-}
999949 fail s = BehaviorM $ fail s
1000−#endif
1001950
1002951data BehaviorSubscribed x a
1003952 = forall p. BehaviorSubscribedHold (Hold x p)
@@ -1346,7 +1295,7 @@ mkWeakPtrWithDebug x debugNote = do
13461295 x' <- evaluate x
13471296 mkWeakPtr x' $
13481297 if debugFinalize
1349− then Just $ debugStrLn $ "finalizing: " ++ debugNote
1298+ then Just $ putStrLn $ "finalizing: " ++ debugNote
13501299 else Nothing
13511300
13521301type WeakList a = [Weak a]
@@ -1368,13 +1317,13 @@ debugSubscriber' description subscribed = Subscriber
13681317 tracePropagate (Proxy :: Proxy x) ("subscriberPropagate: " <> description)
13691318 subscriberPropagate subscribed m
13701319 , subscriberInvalidateHeight = \old -> do
1371− traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
1320+ putStrLn $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
13721321 subscriberInvalidateHeight subscribed old
1373− traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", done"
1322+ putStrLn $ "invalidateSubscriberHeight: " <> description <> ", done"
13741323 , subscriberRecalculateHeight = \new -> do
1375− traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
1324+ putStrLn $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
13761325 subscriberRecalculateHeight subscribed new
1377− traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", done"
1326+ putStrLn $ "subscriberRecalculateHeight: " <> description <> ", done"
13781327 }
13791328
13801329
@@ -1388,15 +1337,11 @@ withIncreasedDepth _ a = do
13881337
13891338{-# INLINE tracePropagate #-}
13901339tracePropagate :: (CanTrace x m) => proxy x -> String -> m ()
1391−tracePropagate p = when debugPropagate . trace p
1340+tracePropagate p = trace p
13921341
13931342{-# INLINE traceInvalidate #-}
13941343traceInvalidate :: String -> IO ()
1395−traceInvalidate = when debugInvalidate . liftIO . debugStrLn
1396−
1397−{-# INLINE traceInvalidateHeight #-}
1398−traceInvalidateHeight :: String -> IO ()
1399−traceInvalidateHeight = when debugInvalidateHeight . liftIO . debugStrLn
1344+traceInvalidate = liftIO . putStrLn
14001345
14011346{-# INLINE trace #-}
14021347trace :: (CanTrace x m) => proxy x -> String -> m ()
@@ -1407,7 +1352,7 @@ traceM :: forall x proxy m. (CanTrace x m) => proxy x -> m String -> m ()
14071352traceM _ getMessage = do
14081353 message <- getMessage
14091354 d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
1410− liftIO $ debugStrLn $ replicate d ' ' <> message
1355+ liftIO $ putStrLn $ replicate d ' ' <> message
14111356
14121357#else
14131358
@@ -1423,10 +1368,6 @@ tracePropagate _ _ = return ()
14231368traceInvalidate :: String -> IO ()
14241369traceInvalidate _ = return ()
14251370
1426−{-# INLINE traceInvalidateHeight #-}
1427−traceInvalidateHeight :: String -> IO ()
1428−traceInvalidateHeight _ = return ()
1429−
14301371{-# INLINE debugSubscriber #-}
14311372debugSubscriber :: String -> Subscriber x a -> IO (Subscriber x a)
14321373debugSubscriber _ = return
@@ -1467,11 +1408,8 @@ showStacks :: [[String]] -> String
14671408showStacks = drawForest . listsToForest . fmap (filterStack "Reflex.Spider.Internal")
14681409
14691410filterStack :: String -> [String] -> [String]
1470−#ifdef DEBUG_HIDE_INTERNALS
14711411filterStack prefix = filter (not . (prefix `isPrefixOf`))
1472−#else
1473−filterStack prefix = id
1474−#endif
1412+
14751413
14761414#ifdef DEBUG_CYCLES
14771415
@@ -1498,7 +1436,7 @@ instance Show EventLoopException where
14981436{-# INLINE propagateSubscriberHold #-}
14991437propagateSubscriberHold :: forall x p. (HasSpiderTimeline x, Patch p) => Hold x p -> p -> EventM x ()
15001438propagateSubscriberHold h a = do
1501− {-# SCC "trace" #-} when debugPropagate $ traceM (Proxy :: Proxy x) $ liftIO $ do
1439+ traceM (Proxy :: Proxy x) $ liftIO $ do
15021440 invalidators <- liftIO $ readIORef $ holdInvalidators h
15031441 return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
15041442
@@ -1506,7 +1444,7 @@ propagateSubscriberHold h a = do
15061444 case {-# SCC "apply" #-} apply a v of
15071445 Nothing -> return ()
15081446 Just v' -> do
1509− {-# SCC "trace2" #-} withIncreasedDepth (Proxy :: Proxy x) $
1447+ withIncreasedDepth (Proxy :: Proxy x) $
15101448 tracePropagate (Proxy :: Proxy x) ("propagateSubscriberHold: assigning Hold" <> showNodeId h)
15111449 vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
15121450 iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
@@ -2038,7 +1976,7 @@ revalidateMergeHeight m = do
20381976 LT -> return ()
20391977 EQ -> do
20401978 let height = succHeight $ heightBagMax heights
2041− traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
1979+ traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
20421980 writeIORef (_merge_heightRef m) $! height
20431981 subscriberRecalculateHeight (_merge_sub m) height
20441982 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
@@ -2202,7 +2140,7 @@ mergeIntCheap d = Event $ \sub -> do
22022140 LT -> return ()
22032141 EQ -> do
22042142 let height = succHeight $ heightBagMax heights
2205− traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
2143+ traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
22062144 writeIORef heightRef $! height
22072145 subscriberRecalculateHeight sub height
22082146 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show numParents <> ") for Merge"
@@ -2385,7 +2323,7 @@ runFrame a = SpiderHost $ do
23852323 subscription <- unSpiderHost $ runFrame $ {-# SCC "subscribeSwitch" #-} subscribe e sub --TODO: Assert that the event isn't firing --TODO: This should not loop because none of the events should be firing, but still, it is inefficient
23862324 {-
23872325 stackTrace <- liftIO $ fmap renderStack $ ccsToStrings =<< (getCCSOf $! switchSubscribedParent subscribed)
2388− liftIO $ debugStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
2326+ liftIO $ putStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
23892327 EventRoot _ -> "EventRoot"
23902328 EventNever -> "EventNever"
23912329 _ -> "something else"
@@ -2520,8 +2458,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Event
25202458 {-# INLINABLE headE #-}
25212459 headE = R.slowHeadE
25222460-- headE (SpiderEvent e) = SpiderEvent <$> Reflex.Spider.Internal.headE e
2523− {-# INLINABLE now #-}
2524− now = nowSpiderEventM
25252461
25262462instance Reflex.Class.MonadSample (SpiderTimeline x) (SpiderPullM x) where
25272463 {-# INLINABLE sample #-}
@@ -2543,9 +2479,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
25432479 {-# INLINABLE headE #-}
25442480 headE = R.slowHeadE
25452481-- headE (SpiderEvent e) = SpiderPushM $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2546− {-# INLINABLE now #-}
2547− now = SpiderPushM nowSpiderEventM
2548−
25492482
25502483instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x)) where
25512484 {-# INLINE return #-}
@@ -2554,10 +2487,8 @@ instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x))
25542487 x >>= f = SpiderDynamic $ dynamicDynIdentity $ newJoinDyn $ newMapDyn (unSpiderDynamic . f) $ unSpiderDynamic x
25552488 {-# INLINE (>>) #-}
25562489 (>>) = (*>)
2557−#if !MIN_VERSION_base(4,13,0)
25582490 {-# INLINE fail #-}
25592491 fail _ = error "Dynamic does not support 'fail'"
2560−#endif
25612492
25622493{-# INLINABLE newJoinDyn #-}
25632494newJoinDyn :: HasSpiderTimeline x => DynamicS x (Identity (DynamicS x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
@@ -2609,9 +2540,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
26092540 buildDynamic getV0 e = runFrame . runSpiderHostFrame $ Reflex.Class.buildDynamic getV0 e
26102541 {-# INLINABLE headE #-}
26112542 headE e = runFrame . runSpiderHostFrame $ Reflex.Class.headE e
2612− {-# INLINABLE now #-}
2613− now = runFrame . runSpiderHostFrame $ Reflex.Class.now
2614−
26152543
26162544instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHostFrame x) where
26172545 sample = SpiderHostFrame . readBehaviorUntracked . unSpiderBehavior --TODO: This can cause problems with laziness, so we should get rid of it if we can
@@ -2628,8 +2556,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
26282556 {-# INLINABLE headE #-}
26292557 headE = R.slowHeadE
26302558-- headE (SpiderEvent e) = SpiderHostFrame $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2631− {-# INLINABLE now #-}
2632− now = SpiderHostFrame Reflex.Class.now
26332559
26342560instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHost x) where
26352561 {-# INLINABLE sample #-}
@@ -2650,8 +2576,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Refle
26502576 buildDynamic getV0 e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.buildDynamic getV0 e
26512577 {-# INLINABLE headE #-}
26522578 headE e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.headE e
2653− {-# INLINABLE now #-}
2654− now = Reflex.Spider.Internal.ReadPhase Reflex.Class.now
26552579
26562580--------------------------------------------------------------------------------
26572581-- Deprecated items
@@ -2775,7 +2699,7 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
27752699 fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
27762700 {-# INLINABLE mergeG #-}
27772701 mergeG
2778− :: forall k2 (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2702+ :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
27792703 => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
27802704 -> DMap k q
27812705 -> R.Event (SpiderTimeline x) (DMap k v)
@@ -2843,14 +2767,8 @@ instance Monad (SpiderHost x) where
28432767 SpiderHost x >> SpiderHost y = SpiderHost $ x >> y
28442768 {-# INLINABLE return #-}
28452769 return x = SpiderHost $ return x
2846−#if !MIN_VERSION_base(4,13,0)
28472770 {-# INLINABLE fail #-}
2848− fail = MonadFail.fail
2849−#endif
2850−
2851−instance MonadFail (SpiderHost x) where
2852− {-# INLINABLE fail #-}
2853− fail s = SpiderHost $ MonadFail.fail s
2771+ fail s = SpiderHost $ fail s
28542772
28552773-- | Run an action affecting the global Spider timeline; this will be guarded by
28562774-- a mutex for that timeline
@@ -2872,10 +2790,8 @@ instance Monad (SpiderHostFrame x) where
28722790 SpiderHostFrame x >> SpiderHostFrame y = SpiderHostFrame $ x >> y
28732791 {-# INLINABLE return #-}
28742792 return x = SpiderHostFrame $ return x
2875−#if !MIN_VERSION_base(4,13,0)
28762793 {-# INLINABLE fail #-}
28772794 fail s = SpiderHostFrame $ fail s
2878−#endif
28792795
28802796instance NotReady (SpiderTimeline x) (SpiderHostFrame x) where
28812797 notReadyUntil _ = pure ()