@@ -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,17 +38,15 @@ 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
4445import Control.Monad.Ref
45−import Control.Monad.Fail (MonadFail)
46−import qualified Control.Monad.Fail as MonadFail
4746import Data.Align
4847import Data.Coerce
49−import Data.Dependent.Map (DMap)
48+import Data.Dependent.Map (DMap, DSum (..))
5049import qualified Data.Dependent.Map as DMap
51−import Data.Dependent.Sum (DSum (..))
5250import Data.FastMutableIntMap (FastMutableIntMap, PatchIntMap (..))
5351import qualified Data.FastMutableIntMap as FastMutableIntMap
5452import Data.Foldable hiding (concat, elem, sequence_)
@@ -65,7 +63,6 @@ import Data.Monoid (mempty, (<>))
6563import Data.Proxy
6664import Data.These
6765import Data.Traversable
68−import Data.Type.Equality ((:~:)(Refl))
6966import Data.Witherable (Filterable, mapMaybe)
7067import GHC.Exts hiding (toList)
7168import GHC.IORef (IORef (..))
@@ -106,21 +103,15 @@ import qualified Reflex.Class
106103import qualified Reflex.Class as R
107104import qualified Reflex.Host.Class
108105import Reflex.NotReady.Class
109−import Data.Patch
110−import qualified Data.Patch.DMapWithMove as PatchDMapWithMove
106+import Reflex.Patch
107+import qualified Reflex.Patch.DMapWithMove as PatchDMapWithMove
111108import Reflex.PerformEvent.Base (PerformEventT)
112109
113110#ifdef DEBUG_TRACE_EVENTS
114111import qualified Data.ByteString.Char8 as BS8
115112import System.IO (stderr)
116113import Data.List (isPrefixOf)
117−#endif
118114
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
124115withStackOneLine :: (BS8.ByteString -> a) -> a
125116withStackOneLine expr = unsafePerformIO $ do
126117 stack <- currentCallStack
@@ -129,33 +120,9 @@ withStackOneLine expr = unsafePerformIO $ do
129120
130121#endif
131122
132−debugPropagate :: Bool
133−
134−debugInvalidateHeight :: Bool
135−
136−debugInvalidate :: Bool
137−
138123#ifdef DEBUG
139124#define DEBUG_NODEIDS
140125
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−
159126class HasNodeId a where
160127 getNodeId :: a -> Int
161128
@@ -193,10 +160,6 @@ showNodeId' = ("#"<>) . show
193160
194161#else
195162
196−debugPropagate = False
197−debugInvalidateHeight = False
198−debugInvalidate = False
199−
200163-- This must be inline, or error messages will cause memory leaks due to retaining the node in question
201164{-# INLINE showNodeId #-}
202165showNodeId :: a -> String
@@ -253,11 +216,11 @@ subscribeAndRead = unEvent
253216{-# INLINE [1] pushCheap #-}
254217pushCheap :: HasSpiderTimeline x => (a -> ComputeM x (Maybe b)) -> Event x a -> Event x b
255218pushCheap !f e = Event $ \sub -> do
256− (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" $ sub
219+ (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" (sub
257220 { subscriberPropagate = \a -> do
258221 mb <- f a
259222 mapM_ (subscriberPropagate sub) mb
260− }
223+ })
261224 occ' <- join <$> mapM f occ
262225 return (subscription, occ')
263226
@@ -307,20 +270,7 @@ data CacheSubscribed x a
307270#endif
308271 }
309272
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− )
273+
324274
325275-- | Construct an 'Event' whose value is guaranteed not to be recomputed
326276-- repeatedly
@@ -464,6 +414,16 @@ data Subscriber x a = Subscriber
464414 , subscriberRecalculateHeight :: !(Height -> IO ())
465415 }
466416
417+--TODO: Move this comment to WeakBag
418+-- These function are constructor functions that are marked NOINLINE so they are
419+-- opaque to GHC. If we do not do this, then GHC will sometimes fuse the constructor away
420+-- so any weak references that are attached to the constructors will have their
421+-- finalizer run. Using the opaque constructor, does not see the
422+-- constructor application, so it behaves like an IORef and cannot be fused away.
423+--
424+-- The result is also evaluated to WHNF, since forcing a thunk invalidates
425+-- the weak pointer to it in some cases.
426+
467427newSubscriberHold :: (HasSpiderTimeline x, Patch p) => Hold x p -> IO (Subscriber x p)
468428newSubscriberHold h = return $ Subscriber
469429 { subscriberPropagate = {-# SCC "traverseHold" #-} propagateSubscriberHold h
@@ -607,16 +567,6 @@ eventSubscribedNever = EventSubscribed
607567 , eventSubscribedWhoCreated = return ["never"]
608568#endif
609569 }
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− }
620570
621571eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
622572eventSubscribedFan !subscribed = EventSubscribed
@@ -994,10 +944,8 @@ instance Monad (BehaviorM x) where
994944 BehaviorM x >> BehaviorM y = BehaviorM $ x >> y
995945 {-# INLINE return #-}
996946 return x = BehaviorM $ return x
997−#if !MIN_VERSION_base(4,13,0)
998947 {-# INLINE fail #-}
999948 fail s = BehaviorM $ fail s
1000−#endif
1001949
1002950data BehaviorSubscribed x a
1003951 = forall p. BehaviorSubscribedHold (Hold x p)
@@ -1346,7 +1294,7 @@ mkWeakPtrWithDebug x debugNote = do
13461294 x' <- evaluate x
13471295 mkWeakPtr x' $
13481296 if debugFinalize
1349− then Just $ debugStrLn $ "finalizing: " ++ debugNote
1297+ then Just $ putStrLn $ "finalizing: " ++ debugNote
13501298 else Nothing
13511299
13521300type WeakList a = [Weak a]
@@ -1368,13 +1316,13 @@ debugSubscriber' description subscribed = Subscriber
13681316 tracePropagate (Proxy :: Proxy x) ("subscriberPropagate: " <> description)
13691317 subscriberPropagate subscribed m
13701318 , subscriberInvalidateHeight = \old -> do
1371− traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
1319+ putStrLn $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
13721320 subscriberInvalidateHeight subscribed old
1373− traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", done"
1321+ putStrLn $ "invalidateSubscriberHeight: " <> description <> ", done"
13741322 , subscriberRecalculateHeight = \new -> do
1375− traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
1323+ putStrLn $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
13761324 subscriberRecalculateHeight subscribed new
1377− traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", done"
1325+ putStrLn $ "subscriberRecalculateHeight: " <> description <> ", done"
13781326 }
13791327
13801328
@@ -1388,15 +1336,11 @@ withIncreasedDepth _ a = do
13881336
13891337{-# INLINE tracePropagate #-}
13901338tracePropagate :: (CanTrace x m) => proxy x -> String -> m ()
1391−tracePropagate p = when debugPropagate . trace p
1339+tracePropagate p = trace p
13921340
13931341{-# INLINE traceInvalidate #-}
13941342traceInvalidate :: String -> IO ()
1395−traceInvalidate = when debugInvalidate . liftIO . debugStrLn
1396−
1397−{-# INLINE traceInvalidateHeight #-}
1398−traceInvalidateHeight :: String -> IO ()
1399−traceInvalidateHeight = when debugInvalidateHeight . liftIO . debugStrLn
1343+traceInvalidate = liftIO . putStrLn
14001344
14011345{-# INLINE trace #-}
14021346trace :: (CanTrace x m) => proxy x -> String -> m ()
@@ -1407,7 +1351,7 @@ traceM :: forall x proxy m. (CanTrace x m) => proxy x -> m String -> m ()
14071351traceM _ getMessage = do
14081352 message <- getMessage
14091353 d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
1410− liftIO $ debugStrLn $ replicate d ' ' <> message
1354+ liftIO $ putStrLn $ replicate d ' ' <> message
14111355
14121356#else
14131357
@@ -1423,10 +1367,6 @@ tracePropagate _ _ = return ()
14231367traceInvalidate :: String -> IO ()
14241368traceInvalidate _ = return ()
14251369
1426−{-# INLINE traceInvalidateHeight #-}
1427−traceInvalidateHeight :: String -> IO ()
1428−traceInvalidateHeight _ = return ()
1429−
14301370{-# INLINE debugSubscriber #-}
14311371debugSubscriber :: String -> Subscriber x a -> IO (Subscriber x a)
14321372debugSubscriber _ = return
@@ -1467,11 +1407,8 @@ showStacks :: [[String]] -> String
14671407showStacks = drawForest . listsToForest . fmap (filterStack "Reflex.Spider.Internal")
14681408
14691409filterStack :: String -> [String] -> [String]
1470−#ifdef DEBUG_HIDE_INTERNALS
14711410filterStack prefix = filter (not . (prefix `isPrefixOf`))
1472−#else
1473−filterStack prefix = id
1474−#endif
1411+
14751412
14761413#ifdef DEBUG_CYCLES
14771414
@@ -1498,7 +1435,7 @@ instance Show EventLoopException where
14981435{-# INLINE propagateSubscriberHold #-}
14991436propagateSubscriberHold :: forall x p. (HasSpiderTimeline x, Patch p) => Hold x p -> p -> EventM x ()
15001437propagateSubscriberHold h a = do
1501− {-# SCC "trace" #-} when debugPropagate $ traceM (Proxy :: Proxy x) $ liftIO $ do
1438+ traceM (Proxy :: Proxy x) $ liftIO $ do
15021439 invalidators <- liftIO $ readIORef $ holdInvalidators h
15031440 return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
15041441
@@ -1506,7 +1443,7 @@ propagateSubscriberHold h a = do
15061443 case {-# SCC "apply" #-} apply a v of
15071444 Nothing -> return ()
15081445 Just v' -> do
1509− {-# SCC "trace2" #-} withIncreasedDepth (Proxy :: Proxy x) $
1446+ withIncreasedDepth (Proxy :: Proxy x) $
15101447 tracePropagate (Proxy :: Proxy x) ("propagateSubscriberHold: assigning Hold" <> showNodeId h)
15111448 vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
15121449 iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
@@ -2038,7 +1975,7 @@ revalidateMergeHeight m = do
20381975 LT -> return ()
20391976 EQ -> do
20401977 let height = succHeight $ heightBagMax heights
2041− traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
1978+ traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
20421979 writeIORef (_merge_heightRef m) $! height
20431980 subscriberRecalculateHeight (_merge_sub m) height
20441981 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
@@ -2202,7 +2139,7 @@ mergeIntCheap d = Event $ \sub -> do
22022139 LT -> return ()
22032140 EQ -> do
22042141 let height = succHeight $ heightBagMax heights
2205− traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
2142+ traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
22062143 writeIORef heightRef $! height
22072144 subscriberRecalculateHeight sub height
22082145 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show numParents <> ") for Merge"
@@ -2385,7 +2322,7 @@ runFrame a = SpiderHost $ do
23852322 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
23862323 {-
23872324 stackTrace <- liftIO $ fmap renderStack $ ccsToStrings =<< (getCCSOf $! switchSubscribedParent subscribed)
2388− liftIO $ debugStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
2325+ liftIO $ putStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
23892326 EventRoot _ -> "EventRoot"
23902327 EventNever -> "EventNever"
23912328 _ -> "something else"
@@ -2520,8 +2457,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Event
25202457 {-# INLINABLE headE #-}
25212458 headE = R.slowHeadE
25222459-- headE (SpiderEvent e) = SpiderEvent <$> Reflex.Spider.Internal.headE e
2523− {-# INLINABLE now #-}
2524− now = nowSpiderEventM
25252460
25262461instance Reflex.Class.MonadSample (SpiderTimeline x) (SpiderPullM x) where
25272462 {-# INLINABLE sample #-}
@@ -2543,9 +2478,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
25432478 {-# INLINABLE headE #-}
25442479 headE = R.slowHeadE
25452480-- headE (SpiderEvent e) = SpiderPushM $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2546− {-# INLINABLE now #-}
2547− now = SpiderPushM nowSpiderEventM
2548−
25492481
25502482instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x)) where
25512483 {-# INLINE return #-}
@@ -2554,10 +2486,8 @@ instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x))
25542486 x >>= f = SpiderDynamic $ dynamicDynIdentity $ newJoinDyn $ newMapDyn (unSpiderDynamic . f) $ unSpiderDynamic x
25552487 {-# INLINE (>>) #-}
25562488 (>>) = (*>)
2557−#if !MIN_VERSION_base(4,13,0)
25582489 {-# INLINE fail #-}
25592490 fail _ = error "Dynamic does not support 'fail'"
2560−#endif
25612491
25622492{-# INLINABLE newJoinDyn #-}
25632493newJoinDyn :: HasSpiderTimeline x => DynamicS x (Identity (DynamicS x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
@@ -2609,9 +2539,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
26092539 buildDynamic getV0 e = runFrame . runSpiderHostFrame $ Reflex.Class.buildDynamic getV0 e
26102540 {-# INLINABLE headE #-}
26112541 headE e = runFrame . runSpiderHostFrame $ Reflex.Class.headE e
2612− {-# INLINABLE now #-}
2613− now = runFrame . runSpiderHostFrame $ Reflex.Class.now
2614−
26152542
26162543instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHostFrame x) where
26172544 sample = SpiderHostFrame . readBehaviorUntracked . unSpiderBehavior --TODO: This can cause problems with laziness, so we should get rid of it if we can
@@ -2628,8 +2555,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
26282555 {-# INLINABLE headE #-}
26292556 headE = R.slowHeadE
26302557-- headE (SpiderEvent e) = SpiderHostFrame $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2631− {-# INLINABLE now #-}
2632− now = SpiderHostFrame Reflex.Class.now
26332558
26342559instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHost x) where
26352560 {-# INLINABLE sample #-}
@@ -2650,8 +2575,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Refle
26502575 buildDynamic getV0 e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.buildDynamic getV0 e
26512576 {-# INLINABLE headE #-}
26522577 headE e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.headE e
2653− {-# INLINABLE now #-}
2654− now = Reflex.Spider.Internal.ReadPhase Reflex.Class.now
26552578
26562579--------------------------------------------------------------------------------
26572580-- Deprecated items
@@ -2775,7 +2698,7 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
27752698 fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
27762699 {-# INLINABLE mergeG #-}
27772700 mergeG
2778− :: forall k2 (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2701+ :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
27792702 => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
27802703 -> DMap k q
27812704 -> R.Event (SpiderTimeline x) (DMap k v)
@@ -2843,14 +2766,8 @@ instance Monad (SpiderHost x) where
28432766 SpiderHost x >> SpiderHost y = SpiderHost $ x >> y
28442767 {-# INLINABLE return #-}
28452768 return x = SpiderHost $ return x
2846−#if !MIN_VERSION_base(4,13,0)
28472769 {-# 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
2770+ fail s = SpiderHost $ fail s
28542771
28552772-- | Run an action affecting the global Spider timeline; this will be guarded by
28562773-- a mutex for that timeline
@@ -2872,10 +2789,8 @@ instance Monad (SpiderHostFrame x) where
28722789 SpiderHostFrame x >> SpiderHostFrame y = SpiderHostFrame $ x >> y
28732790 {-# INLINABLE return #-}
28742791 return x = SpiderHostFrame $ return x
2875−#if !MIN_VERSION_base(4,13,0)
28762792 {-# INLINABLE fail #-}
28772793 fail s = SpiderHostFrame $ fail s
2878−#endif
28792794
28802795instance NotReady (SpiderTimeline x) (SpiderHostFrame x) where
28812796 notReadyUntil _ = pure ()