@@ -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,6 @@
1920{-# LANGUAGE DeriveFunctor #-}
2021{-# LANGUAGE StandaloneDeriving #-}
2122{-# LANGUAGE InstanceSigs #-}
22−{-# LANGUAGE MultiWayIf #-}
2323
2424#ifdef USE_REFLEX_OPTIMIZER
2525{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
@@ -38,6 +38,7 @@ import Control.Monad hiding (forM, forM_, mapM, mapM_)
3838import Control.Monad.Exception
3939import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
4040import Control.Monad.Primitive
41+--import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_)
4142import Control.Monad.Reader.Class
4243import Control.Monad.IO.Class
4344import Control.Monad.ReaderIO
@@ -46,9 +47,8 @@ import Control.Monad.Fail (MonadFail)
4647import qualified Control.Monad.Fail as MonadFail
4748import Data.Align
4849import Data.Coerce
49−import Data.Dependent.Map (DMap)
50+import Data.Dependent.Map (DMap, DSum (..))
5051import qualified Data.Dependent.Map as DMap
51−import Data.Dependent.Sum (DSum (..))
5252import Data.FastMutableIntMap (FastMutableIntMap, PatchIntMap (..))
5353import qualified Data.FastMutableIntMap as FastMutableIntMap
5454import Data.Foldable hiding (concat, elem, sequence_)
@@ -114,13 +114,7 @@ import Reflex.PerformEvent.Base (PerformEventT)
114114import qualified Data.ByteString.Char8 as BS8
115115import System.IO (stderr)
116116import Data.List (isPrefixOf)
117−#endif
118117
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
124118withStackOneLine :: (BS8.ByteString -> a) -> a
125119withStackOneLine expr = unsafePerformIO $ do
126120 stack <- currentCallStack
@@ -129,33 +123,9 @@ withStackOneLine expr = unsafePerformIO $ do
129123
130124#endif
131125
132−debugPropagate :: Bool
133−
134−debugInvalidateHeight :: Bool
135−
136−debugInvalidate :: Bool
137−
138126#ifdef DEBUG
139127#define DEBUG_NODEIDS
140128
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−
159129class HasNodeId a where
160130 getNodeId :: a -> Int
161131
@@ -193,10 +163,6 @@ showNodeId' = ("#"<>) . show
193163
194164#else
195165
196−debugPropagate = False
197−debugInvalidateHeight = False
198−debugInvalidate = False
199−
200166-- This must be inline, or error messages will cause memory leaks due to retaining the node in question
201167{-# INLINE showNodeId #-}
202168showNodeId :: a -> String
@@ -253,11 +219,11 @@ subscribeAndRead = unEvent
253219{-# INLINE [1] pushCheap #-}
254220pushCheap :: HasSpiderTimeline x => (a -> ComputeM x (Maybe b)) -> Event x a -> Event x b
255221pushCheap !f e = Event $ \sub -> do
256− (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" $ sub
222+ (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" (sub
257223 { subscriberPropagate = \a -> do
258224 mb <- f a
259225 mapM_ (subscriberPropagate sub) mb
260− }
226+ })
261227 occ' <- join <$> mapM f occ
262228 return (subscription, occ')
263229
@@ -307,20 +273,7 @@ data CacheSubscribed x a
307273#endif
308274 }
309275
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− )
276+
324277
325278-- | Construct an 'Event' whose value is guaranteed not to be recomputed
326279-- repeatedly
@@ -464,6 +417,16 @@ data Subscriber x a = Subscriber
464417 , subscriberRecalculateHeight :: !(Height -> IO ())
465418 }
466419
420+--TODO: Move this comment to WeakBag
421+-- These function are constructor functions that are marked NOINLINE so they are
422+-- opaque to GHC. If we do not do this, then GHC will sometimes fuse the constructor away
423+-- so any weak references that are attached to the constructors will have their
424+-- finalizer run. Using the opaque constructor, does not see the
425+-- constructor application, so it behaves like an IORef and cannot be fused away.
426+--
427+-- The result is also evaluated to WHNF, since forcing a thunk invalidates
428+-- the weak pointer to it in some cases.
429+
467430newSubscriberHold :: (HasSpiderTimeline x, Patch p) => Hold x p -> IO (Subscriber x p)
468431newSubscriberHold h = return $ Subscriber
469432 { subscriberPropagate = {-# SCC "traverseHold" #-} propagateSubscriberHold h
@@ -607,16 +570,6 @@ eventSubscribedNever = EventSubscribed
607570 , eventSubscribedWhoCreated = return ["never"]
608571#endif
609572 }
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− }
620573
621574eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
622575eventSubscribedFan !subscribed = EventSubscribed
@@ -1346,7 +1299,7 @@ mkWeakPtrWithDebug x debugNote = do
13461299 x' <- evaluate x
13471300 mkWeakPtr x' $
13481301 if debugFinalize
1349− then Just $ debugStrLn $ "finalizing: " ++ debugNote
1302+ then Just $ putStrLn $ "finalizing: " ++ debugNote
13501303 else Nothing
13511304
13521305type WeakList a = [Weak a]
@@ -1368,13 +1321,13 @@ debugSubscriber' description subscribed = Subscriber
13681321 tracePropagate (Proxy :: Proxy x) ("subscriberPropagate: " <> description)
13691322 subscriberPropagate subscribed m
13701323 , subscriberInvalidateHeight = \old -> do
1371− traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
1324+ putStrLn $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
13721325 subscriberInvalidateHeight subscribed old
1373− traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", done"
1326+ putStrLn $ "invalidateSubscriberHeight: " <> description <> ", done"
13741327 , subscriberRecalculateHeight = \new -> do
1375− traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
1328+ putStrLn $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
13761329 subscriberRecalculateHeight subscribed new
1377− traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", done"
1330+ putStrLn $ "subscriberRecalculateHeight: " <> description <> ", done"
13781331 }
13791332
13801333
@@ -1388,15 +1341,11 @@ withIncreasedDepth _ a = do
13881341
13891342{-# INLINE tracePropagate #-}
13901343tracePropagate :: (CanTrace x m) => proxy x -> String -> m ()
1391−tracePropagate p = when debugPropagate . trace p
1344+tracePropagate p = trace p
13921345
13931346{-# INLINE traceInvalidate #-}
13941347traceInvalidate :: String -> IO ()
1395−traceInvalidate = when debugInvalidate . liftIO . debugStrLn
1396−
1397−{-# INLINE traceInvalidateHeight #-}
1398−traceInvalidateHeight :: String -> IO ()
1399−traceInvalidateHeight = when debugInvalidateHeight . liftIO . debugStrLn
1348+traceInvalidate = liftIO . putStrLn
14001349
14011350{-# INLINE trace #-}
14021351trace :: (CanTrace x m) => proxy x -> String -> m ()
@@ -1407,7 +1356,7 @@ traceM :: forall x proxy m. (CanTrace x m) => proxy x -> m String -> m ()
14071356traceM _ getMessage = do
14081357 message <- getMessage
14091358 d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
1410− liftIO $ debugStrLn $ replicate d ' ' <> message
1359+ liftIO $ putStrLn $ replicate d ' ' <> message
14111360
14121361#else
14131362
@@ -1423,10 +1372,6 @@ tracePropagate _ _ = return ()
14231372traceInvalidate :: String -> IO ()
14241373traceInvalidate _ = return ()
14251374
1426−{-# INLINE traceInvalidateHeight #-}
1427−traceInvalidateHeight :: String -> IO ()
1428−traceInvalidateHeight _ = return ()
1429−
14301375{-# INLINE debugSubscriber #-}
14311376debugSubscriber :: String -> Subscriber x a -> IO (Subscriber x a)
14321377debugSubscriber _ = return
@@ -1467,11 +1412,8 @@ showStacks :: [[String]] -> String
14671412showStacks = drawForest . listsToForest . fmap (filterStack "Reflex.Spider.Internal")
14681413
14691414filterStack :: String -> [String] -> [String]
1470−#ifdef DEBUG_HIDE_INTERNALS
14711415filterStack prefix = filter (not . (prefix `isPrefixOf`))
1472−#else
1473−filterStack prefix = id
1474−#endif
1416+
14751417
14761418#ifdef DEBUG_CYCLES
14771419
@@ -1498,7 +1440,7 @@ instance Show EventLoopException where
14981440{-# INLINE propagateSubscriberHold #-}
14991441propagateSubscriberHold :: forall x p. (HasSpiderTimeline x, Patch p) => Hold x p -> p -> EventM x ()
15001442propagateSubscriberHold h a = do
1501− {-# SCC "trace" #-} when debugPropagate $ traceM (Proxy :: Proxy x) $ liftIO $ do
1443+ traceM (Proxy :: Proxy x) $ liftIO $ do
15021444 invalidators <- liftIO $ readIORef $ holdInvalidators h
15031445 return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
15041446
@@ -1506,7 +1448,7 @@ propagateSubscriberHold h a = do
15061448 case {-# SCC "apply" #-} apply a v of
15071449 Nothing -> return ()
15081450 Just v' -> do
1509− {-# SCC "trace2" #-} withIncreasedDepth (Proxy :: Proxy x) $
1451+ withIncreasedDepth (Proxy :: Proxy x) $
15101452 tracePropagate (Proxy :: Proxy x) ("propagateSubscriberHold: assigning Hold" <> showNodeId h)
15111453 vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
15121454 iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
@@ -2038,7 +1980,7 @@ revalidateMergeHeight m = do
20381980 LT -> return ()
20391981 EQ -> do
20401982 let height = succHeight $ heightBagMax heights
2041− traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
1983+ traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
20421984 writeIORef (_merge_heightRef m) $! height
20431985 subscriberRecalculateHeight (_merge_sub m) height
20441986 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
@@ -2202,7 +2144,7 @@ mergeIntCheap d = Event $ \sub -> do
22022144 LT -> return ()
22032145 EQ -> do
22042146 let height = succHeight $ heightBagMax heights
2205− traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
2147+ traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
22062148 writeIORef heightRef $! height
22072149 subscriberRecalculateHeight sub height
22082150 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show numParents <> ") for Merge"
@@ -2385,7 +2327,7 @@ runFrame a = SpiderHost $ do
23852327 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
23862328 {-
23872329 stackTrace <- liftIO $ fmap renderStack $ ccsToStrings =<< (getCCSOf $! switchSubscribedParent subscribed)
2388− liftIO $ debugStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
2330+ liftIO $ putStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
23892331 EventRoot _ -> "EventRoot"
23902332 EventNever -> "EventNever"
23912333 _ -> "something else"
@@ -2520,8 +2462,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Event
25202462 {-# INLINABLE headE #-}
25212463 headE = R.slowHeadE
25222464-- headE (SpiderEvent e) = SpiderEvent <$> Reflex.Spider.Internal.headE e
2523− {-# INLINABLE now #-}
2524− now = nowSpiderEventM
25252465
25262466instance Reflex.Class.MonadSample (SpiderTimeline x) (SpiderPullM x) where
25272467 {-# INLINABLE sample #-}
@@ -2543,9 +2483,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
25432483 {-# INLINABLE headE #-}
25442484 headE = R.slowHeadE
25452485-- headE (SpiderEvent e) = SpiderPushM $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2546− {-# INLINABLE now #-}
2547− now = SpiderPushM nowSpiderEventM
2548−
25492486
25502487instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x)) where
25512488 {-# INLINE return #-}
@@ -2609,9 +2546,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
26092546 buildDynamic getV0 e = runFrame . runSpiderHostFrame $ Reflex.Class.buildDynamic getV0 e
26102547 {-# INLINABLE headE #-}
26112548 headE e = runFrame . runSpiderHostFrame $ Reflex.Class.headE e
2612− {-# INLINABLE now #-}
2613− now = runFrame . runSpiderHostFrame $ Reflex.Class.now
2614−
26152549
26162550instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHostFrame x) where
26172551 sample = SpiderHostFrame . readBehaviorUntracked . unSpiderBehavior --TODO: This can cause problems with laziness, so we should get rid of it if we can
@@ -2628,8 +2562,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
26282562 {-# INLINABLE headE #-}
26292563 headE = R.slowHeadE
26302564-- headE (SpiderEvent e) = SpiderHostFrame $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2631− {-# INLINABLE now #-}
2632− now = SpiderHostFrame Reflex.Class.now
26332565
26342566instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHost x) where
26352567 {-# INLINABLE sample #-}
@@ -2650,8 +2582,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Refle
26502582 buildDynamic getV0 e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.buildDynamic getV0 e
26512583 {-# INLINABLE headE #-}
26522584 headE e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.headE e
2653− {-# INLINABLE now #-}
2654− now = Reflex.Spider.Internal.ReadPhase Reflex.Class.now
26552585
26562586--------------------------------------------------------------------------------
26572587-- Deprecated items
@@ -2775,7 +2705,7 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
27752705 fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
27762706 {-# INLINABLE mergeG #-}
27772707 mergeG
2778− :: forall k2 (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2708+ :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
27792709 => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
27802710 -> DMap k q
27812711 -> R.Event (SpiderTimeline x) (DMap k v)