@@ -9,7 +9,6 @@
99{-# LANGUAGE GADTs #-}
1010{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1111{-# LANGUAGE LambdaCase #-}
12−{-# LANGUAGE MultiWayIf #-}
1312{-# LANGUAGE RankNTypes #-}
1413{-# LANGUAGE RoleAnnotations #-}
1514{-# LANGUAGE ScopedTypeVariables #-}
@@ -20,6 +19,7 @@
2019{-# LANGUAGE DeriveFunctor #-}
2120{-# LANGUAGE StandaloneDeriving #-}
2221{-# LANGUAGE InstanceSigs #-}
22+{-# LANGUAGE MultiWayIf #-}
2323
2424#ifdef USE_REFLEX_OPTIMIZER
2525{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
@@ -38,15 +38,17 @@ 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_)
4241import Control.Monad.Reader.Class
4342import Control.Monad.IO.Class
4443import Control.Monad.ReaderIO
4544import Control.Monad.Ref
45+import Control.Monad.Fail (MonadFail)
46+import qualified Control.Monad.Fail as MonadFail
4647import Data.Align
4748import Data.Coerce
48−import Data.Dependent.Map (DMap, DSum (..))
49+import Data.Dependent.Map (DMap)
4950import qualified Data.Dependent.Map as DMap
51+import Data.Dependent.Sum (DSum (..))
5052import Data.FastMutableIntMap (FastMutableIntMap, PatchIntMap (..))
5153import qualified Data.FastMutableIntMap as FastMutableIntMap
5254import Data.Foldable hiding (concat, elem, sequence_)
@@ -63,6 +65,7 @@ import Data.Monoid (mempty, (<>))
6365import Data.Proxy
6466import Data.These
6567import Data.Traversable
68+import Data.Type.Equality ((:~:)(Refl))
6669import Data.Witherable (Filterable, mapMaybe)
6770import GHC.Exts hiding (toList)
6871import GHC.IORef (IORef (..))
@@ -103,15 +106,21 @@ import qualified Reflex.Class
103106import qualified Reflex.Class as R
104107import qualified Reflex.Host.Class
105108import Reflex.NotReady.Class
106−import Reflex.Patch
107−import qualified Reflex.Patch.DMapWithMove as PatchDMapWithMove
109+import Data.Patch
110+import qualified Data.Patch.DMapWithMove as PatchDMapWithMove
108111import Reflex.PerformEvent.Base (PerformEventT)
109112
110113#ifdef DEBUG_TRACE_EVENTS
111114import qualified Data.ByteString.Char8 as BS8
112115import System.IO (stderr)
113116import Data.List (isPrefixOf)
117+#endif
114118
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
115124withStackOneLine :: (BS8.ByteString -> a) -> a
116125withStackOneLine expr = unsafePerformIO $ do
117126 stack <- currentCallStack
@@ -120,9 +129,33 @@ withStackOneLine expr = unsafePerformIO $ do
120129
121130#endif
122131
132+debugPropagate :: Bool
133+
134+debugInvalidateHeight :: Bool
135+
136+debugInvalidate :: Bool
137+
123138#ifdef DEBUG
124139#define DEBUG_NODEIDS
125140
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+
126159class HasNodeId a where
127160 getNodeId :: a -> Int
128161
@@ -160,6 +193,10 @@ showNodeId' = ("#"<>) . show
160193
161194#else
162195
196+debugPropagate = False
197+debugInvalidateHeight = False
198+debugInvalidate = False
199+
163200-- This must be inline, or error messages will cause memory leaks due to retaining the node in question
164201{-# INLINE showNodeId #-}
165202showNodeId :: a -> String
@@ -216,11 +253,11 @@ subscribeAndRead = unEvent
216253{-# INLINE [1] pushCheap #-}
217254pushCheap :: HasSpiderTimeline x => (a -> ComputeM x (Maybe b)) -> Event x a -> Event x b
218255pushCheap !f e = Event $ \sub -> do
219− (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" (sub
256+ (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" $ sub
220257 { subscriberPropagate = \a -> do
221258 mb <- f a
222259 mapM_ (subscriberPropagate sub) mb
223− })
260+ }
224261 occ' <- join <$> mapM f occ
225262 return (subscription, occ')
226263
@@ -270,7 +307,20 @@ data CacheSubscribed x a
270307#endif
271308 }
272309
273−
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+ )
274324
275325-- | Construct an 'Event' whose value is guaranteed not to be recomputed
276326-- repeatedly
@@ -414,16 +464,6 @@ data Subscriber x a = Subscriber
414464 , subscriberRecalculateHeight :: !(Height -> IO ())
415465 }
416466
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−
427467newSubscriberHold :: (HasSpiderTimeline x, Patch p) => Hold x p -> IO (Subscriber x p)
428468newSubscriberHold h = return $ Subscriber
429469 { subscriberPropagate = {-# SCC "traverseHold" #-} propagateSubscriberHold h
@@ -567,6 +607,16 @@ eventSubscribedNever = EventSubscribed
567607 , eventSubscribedWhoCreated = return ["never"]
568608#endif
569609 }
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+ }
570620
571621eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
572622eventSubscribedFan !subscribed = EventSubscribed
@@ -944,8 +994,10 @@ instance Monad (BehaviorM x) where
944994 BehaviorM x >> BehaviorM y = BehaviorM $ x >> y
945995 {-# INLINE return #-}
946996 return x = BehaviorM $ return x
997+#if !MIN_VERSION_base(4,13,0)
947998 {-# INLINE fail #-}
948999 fail s = BehaviorM $ fail s
1000+#endif
9491001
9501002data BehaviorSubscribed x a
9511003 = forall p. BehaviorSubscribedHold (Hold x p)
@@ -1294,7 +1346,7 @@ mkWeakPtrWithDebug x debugNote = do
12941346 x' <- evaluate x
12951347 mkWeakPtr x' $
12961348 if debugFinalize
1297− then Just $ putStrLn $ "finalizing: " ++ debugNote
1349+ then Just $ debugStrLn $ "finalizing: " ++ debugNote
12981350 else Nothing
12991351
13001352type WeakList a = [Weak a]
@@ -1316,13 +1368,13 @@ debugSubscriber' description subscribed = Subscriber
13161368 tracePropagate (Proxy :: Proxy x) ("subscriberPropagate: " <> description)
13171369 subscriberPropagate subscribed m
13181370 , subscriberInvalidateHeight = \old -> do
1319− putStrLn $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
1371+ traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
13201372 subscriberInvalidateHeight subscribed old
1321− putStrLn $ "invalidateSubscriberHeight: " <> description <> ", done"
1373+ traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", done"
13221374 , subscriberRecalculateHeight = \new -> do
1323− putStrLn $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
1375+ traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
13241376 subscriberRecalculateHeight subscribed new
1325− putStrLn $ "subscriberRecalculateHeight: " <> description <> ", done"
1377+ traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", done"
13261378 }
13271379
13281380
@@ -1336,11 +1388,15 @@ withIncreasedDepth _ a = do
13361388
13371389{-# INLINE tracePropagate #-}
13381390tracePropagate :: (CanTrace x m) => proxy x -> String -> m ()
1339−tracePropagate p = trace p
1391+tracePropagate p = when debugPropagate . trace p
13401392
13411393{-# INLINE traceInvalidate #-}
13421394traceInvalidate :: String -> IO ()
1343−traceInvalidate = liftIO . putStrLn
1395+traceInvalidate = when debugInvalidate . liftIO . debugStrLn
1396+
1397+{-# INLINE traceInvalidateHeight #-}
1398+traceInvalidateHeight :: String -> IO ()
1399+traceInvalidateHeight = when debugInvalidateHeight . liftIO . debugStrLn
13441400
13451401{-# INLINE trace #-}
13461402trace :: (CanTrace x m) => proxy x -> String -> m ()
@@ -1351,7 +1407,7 @@ traceM :: forall x proxy m. (CanTrace x m) => proxy x -> m String -> m ()
13511407traceM _ getMessage = do
13521408 message <- getMessage
13531409 d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
1354− liftIO $ putStrLn $ replicate d ' ' <> message
1410+ liftIO $ debugStrLn $ replicate d ' ' <> message
13551411
13561412#else
13571413
@@ -1367,6 +1423,10 @@ tracePropagate _ _ = return ()
13671423traceInvalidate :: String -> IO ()
13681424traceInvalidate _ = return ()
13691425
1426+{-# INLINE traceInvalidateHeight #-}
1427+traceInvalidateHeight :: String -> IO ()
1428+traceInvalidateHeight _ = return ()
1429+
13701430{-# INLINE debugSubscriber #-}
13711431debugSubscriber :: String -> Subscriber x a -> IO (Subscriber x a)
13721432debugSubscriber _ = return
@@ -1407,8 +1467,11 @@ showStacks :: [[String]] -> String
14071467showStacks = drawForest . listsToForest . fmap (filterStack "Reflex.Spider.Internal")
14081468
14091469filterStack :: String -> [String] -> [String]
1470+#ifdef DEBUG_HIDE_INTERNALS
14101471filterStack prefix = filter (not . (prefix `isPrefixOf`))
1411−
1472+#else
1473+filterStack prefix = id
1474+#endif
14121475
14131476#ifdef DEBUG_CYCLES
14141477
@@ -1435,7 +1498,7 @@ instance Show EventLoopException where
14351498{-# INLINE propagateSubscriberHold #-}
14361499propagateSubscriberHold :: forall x p. (HasSpiderTimeline x, Patch p) => Hold x p -> p -> EventM x ()
14371500propagateSubscriberHold h a = do
1438− traceM (Proxy :: Proxy x) $ liftIO $ do
1501+ {-# SCC "trace" #-} when debugPropagate $ traceM (Proxy :: Proxy x) $ liftIO $ do
14391502 invalidators <- liftIO $ readIORef $ holdInvalidators h
14401503 return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
14411504
@@ -1443,7 +1506,7 @@ propagateSubscriberHold h a = do
14431506 case {-# SCC "apply" #-} apply a v of
14441507 Nothing -> return ()
14451508 Just v' -> do
1446− withIncreasedDepth (Proxy :: Proxy x) $
1509+ {-# SCC "trace2" #-} withIncreasedDepth (Proxy :: Proxy x) $
14471510 tracePropagate (Proxy :: Proxy x) ("propagateSubscriberHold: assigning Hold" <> showNodeId h)
14481511 vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
14491512 iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
@@ -1975,7 +2038,7 @@ revalidateMergeHeight m = do
19752038 LT -> return ()
19762039 EQ -> do
19772040 let height = succHeight $ heightBagMax heights
1978− traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
2041+ traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
19792042 writeIORef (_merge_heightRef m) $! height
19802043 subscriberRecalculateHeight (_merge_sub m) height
19812044 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
@@ -2139,7 +2202,7 @@ mergeIntCheap d = Event $ \sub -> do
21392202 LT -> return ()
21402203 EQ -> do
21412204 let height = succHeight $ heightBagMax heights
2142− traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
2205+ traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
21432206 writeIORef heightRef $! height
21442207 subscriberRecalculateHeight sub height
21452208 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show numParents <> ") for Merge"
@@ -2322,7 +2385,7 @@ runFrame a = SpiderHost $ do
23222385 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
23232386 {-
23242387 stackTrace <- liftIO $ fmap renderStack $ ccsToStrings =<< (getCCSOf $! switchSubscribedParent subscribed)
2325− liftIO $ putStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
2388+ liftIO $ debugStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
23262389 EventRoot _ -> "EventRoot"
23272390 EventNever -> "EventNever"
23282391 _ -> "something else"
@@ -2457,6 +2520,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Event
24572520 {-# INLINABLE headE #-}
24582521 headE = R.slowHeadE
24592522-- headE (SpiderEvent e) = SpiderEvent <$> Reflex.Spider.Internal.headE e
2523+ {-# INLINABLE now #-}
2524+ now = nowSpiderEventM
24602525
24612526instance Reflex.Class.MonadSample (SpiderTimeline x) (SpiderPullM x) where
24622527 {-# INLINABLE sample #-}
@@ -2478,6 +2543,9 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
24782543 {-# INLINABLE headE #-}
24792544 headE = R.slowHeadE
24802545-- headE (SpiderEvent e) = SpiderPushM $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2546+ {-# INLINABLE now #-}
2547+ now = SpiderPushM nowSpiderEventM
2548+
24812549
24822550instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x)) where
24832551 {-# INLINE return #-}
@@ -2486,8 +2554,10 @@ instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x))
24862554 x >>= f = SpiderDynamic $ dynamicDynIdentity $ newJoinDyn $ newMapDyn (unSpiderDynamic . f) $ unSpiderDynamic x
24872555 {-# INLINE (>>) #-}
24882556 (>>) = (*>)
2557+#if !MIN_VERSION_base(4,13,0)
24892558 {-# INLINE fail #-}
24902559 fail _ = error "Dynamic does not support 'fail'"
2560+#endif
24912561
24922562{-# INLINABLE newJoinDyn #-}
24932563newJoinDyn :: HasSpiderTimeline x => DynamicS x (Identity (DynamicS x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
@@ -2539,6 +2609,9 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
25392609 buildDynamic getV0 e = runFrame . runSpiderHostFrame $ Reflex.Class.buildDynamic getV0 e
25402610 {-# INLINABLE headE #-}
25412611 headE e = runFrame . runSpiderHostFrame $ Reflex.Class.headE e
2612+ {-# INLINABLE now #-}
2613+ now = runFrame . runSpiderHostFrame $ Reflex.Class.now
2614+
25422615
25432616instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHostFrame x) where
25442617 sample = SpiderHostFrame . readBehaviorUntracked . unSpiderBehavior --TODO: This can cause problems with laziness, so we should get rid of it if we can
@@ -2555,6 +2628,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
25552628 {-# INLINABLE headE #-}
25562629 headE = R.slowHeadE
25572630-- headE (SpiderEvent e) = SpiderHostFrame $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2631+ {-# INLINABLE now #-}
2632+ now = SpiderHostFrame Reflex.Class.now
25582633
25592634instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHost x) where
25602635 {-# INLINABLE sample #-}
@@ -2575,6 +2650,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Refle
25752650 buildDynamic getV0 e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.buildDynamic getV0 e
25762651 {-# INLINABLE headE #-}
25772652 headE e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.headE e
2653+ {-# INLINABLE now #-}
2654+ now = Reflex.Spider.Internal.ReadPhase Reflex.Class.now
25782655
25792656--------------------------------------------------------------------------------
25802657-- Deprecated items
@@ -2698,7 +2775,7 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
26982775 fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
26992776 {-# INLINABLE mergeG #-}
27002777 mergeG
2701− :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2778+ :: forall k2 (k :: k2 -> *) q (v :: k2 -> *). GCompare k
27022779 => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
27032780 -> DMap k q
27042781 -> R.Event (SpiderTimeline x) (DMap k v)
@@ -2766,8 +2843,14 @@ instance Monad (SpiderHost x) where
27662843 SpiderHost x >> SpiderHost y = SpiderHost $ x >> y
27672844 {-# INLINABLE return #-}
27682845 return x = SpiderHost $ return x
2846+#if !MIN_VERSION_base(4,13,0)
27692847 {-# INLINABLE fail #-}
2770− fail s = SpiderHost $ fail s
2848+ fail = MonadFail.fail
2849+#endif
2850+
2851+instance MonadFail (SpiderHost x) where
2852+ {-# INLINABLE fail #-}
2853+ fail s = SpiderHost $ MonadFail.fail s
27712854
27722855-- | Run an action affecting the global Spider timeline; this will be guarded by
27732856-- a mutex for that timeline
@@ -2789,8 +2872,10 @@ instance Monad (SpiderHostFrame x) where
27892872 SpiderHostFrame x >> SpiderHostFrame y = SpiderHostFrame $ x >> y
27902873 {-# INLINABLE return #-}
27912874 return x = SpiderHostFrame $ return x
2875+#if !MIN_VERSION_base(4,13,0)
27922876 {-# INLINABLE fail #-}
27932877 fail s = SpiderHostFrame $ fail s
2878+#endif
27942879
27952880instance NotReady (SpiderTimeline x) (SpiderHostFrame x) where
27962881 notReadyUntil _ = pure ()