@@ -9,7 +9,6 @@
9{-# LANGUAGE GADTs #-}
9{-# LANGUAGE GADTs #-}
10{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10{-# LANGUAGE GeneralizedNewtypeDeriving #-}
11{-# LANGUAGE LambdaCase #-}
11{-# LANGUAGE LambdaCase #-}
12−{-# LANGUAGE MultiWayIf #-}
13{-# LANGUAGE RankNTypes #-}
12{-# LANGUAGE RankNTypes #-}
14{-# LANGUAGE RoleAnnotations #-}
13{-# LANGUAGE RoleAnnotations #-}
15{-# LANGUAGE ScopedTypeVariables #-}
14{-# LANGUAGE ScopedTypeVariables #-}
@@ -20,6 +19,7 @@
20{-# LANGUAGE DeriveFunctor #-}
19{-# LANGUAGE DeriveFunctor #-}
21{-# LANGUAGE StandaloneDeriving #-}
20{-# LANGUAGE StandaloneDeriving #-}
22{-# LANGUAGE InstanceSigs #-}
21{-# LANGUAGE InstanceSigs #-}
22+{-# LANGUAGE MultiWayIf #-}
24#ifdef USE_REFLEX_OPTIMIZER
24#ifdef USE_REFLEX_OPTIMIZER
25{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
25{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
@@ -38,7 +38,6 @@ import Control.Monad hiding (forM, forM_, mapM, mapM_)
38import Control.Monad.Exception
38import Control.Monad.Exception
39import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
39import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
40import Control.Monad.Primitive
40import Control.Monad.Primitive
41−--import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_)
42import Control.Monad.Reader.Class
41import Control.Monad.Reader.Class
43import Control.Monad.IO.Class
42import Control.Monad.IO.Class
44import Control.Monad.ReaderIO
43import Control.Monad.ReaderIO
@@ -47,8 +46,9 @@ import Control.Monad.Fail (MonadFail)
47import qualified Control.Monad.Fail as MonadFail
46import qualified Control.Monad.Fail as MonadFail
48import Data.Align
47import Data.Align
49import Data.Coerce
48import Data.Coerce
50−import Data.Dependent.Map (DMap, DSum (..))
49+import Data.Dependent.Map (DMap)
51import qualified Data.Dependent.Map as DMap
50import qualified Data.Dependent.Map as DMap
51+import Data.Dependent.Sum (DSum (..))
52import Data.FastMutableIntMap (FastMutableIntMap, PatchIntMap (..))
52import Data.FastMutableIntMap (FastMutableIntMap, PatchIntMap (..))
53import qualified Data.FastMutableIntMap as FastMutableIntMap
53import qualified Data.FastMutableIntMap as FastMutableIntMap
54import Data.Foldable hiding (concat, elem, sequence_)
54import Data.Foldable hiding (concat, elem, sequence_)
@@ -129,9 +129,33 @@ withStackOneLine expr = unsafePerformIO $ do
132+debugPropagate :: Bool
134+debugInvalidateHeight :: Bool
136+debugInvalidate :: Bool
132#ifdef DEBUG
138#ifdef DEBUG
133#define DEBUG_NODEIDS
139#define DEBUG_NODEIDS
141+#ifdef DEBUG_TRACE_PROPAGATION
142+debugPropagate = True
144+debugPropagate = False
147+#ifdef DEBUG_TRACE_HEIGHT
148+debugInvalidateHeight = True
150+debugInvalidateHeight = False
153+#ifdef DEBUG_TRACE_INVALIDATION
154+debugInvalidate = True
156+debugInvalidate = False
135class HasNodeId a where
159class HasNodeId a where
136 getNodeId :: a -> Int
160 getNodeId :: a -> Int
@@ -169,6 +193,10 @@ showNodeId' = ("#"<>) . show
196+debugPropagate = False
197+debugInvalidateHeight = False
198+debugInvalidate = False
172-- This must be inline, or error messages will cause memory leaks due to retaining the node in question
200-- This must be inline, or error messages will cause memory leaks due to retaining the node in question
173{-# INLINE showNodeId #-}
201{-# INLINE showNodeId #-}
174showNodeId :: a -> String
202showNodeId :: a -> String
@@ -279,7 +307,20 @@ data CacheSubscribed x a
282−
310+nowSpiderEventM :: (HasSpiderTimeline x) => EventM x (R.Event (SpiderTimeline x) ())
314+now :: (MonadIO m, Defer (Some Clear) m, HasSpiderTimeline x
317+ nowOrNot <- liftIO $ newIORef $ Just ()
318+ scheduleClear nowOrNot
319+ return . Event $ \_ -> do
320+ occ <- liftIO . readIORef $ nowOrNot
321+ return ( EventSubscription (return ()) eventSubscribedNow
284-- | Construct an 'Event' whose value is guaranteed not to be recomputed
325-- | Construct an 'Event' whose value is guaranteed not to be recomputed
285-- repeatedly
326-- repeatedly
@@ -423,16 +464,6 @@ data Subscriber x a = Subscriber
423 , subscriberRecalculateHeight :: !(Height -> IO ())
464 , subscriberRecalculateHeight :: !(Height -> IO ())
426−--TODO: Move this comment to WeakBag
427−-- These function are constructor functions that are marked NOINLINE so they are
428−-- opaque to GHC. If we do not do this, then GHC will sometimes fuse the constructor away
429−-- so any weak references that are attached to the constructors will have their
430−-- finalizer run. Using the opaque constructor, does not see the
431−-- constructor application, so it behaves like an IORef and cannot be fused away.
433−-- The result is also evaluated to WHNF, since forcing a thunk invalidates
434−-- the weak pointer to it in some cases.
436newSubscriberHold :: (HasSpiderTimeline x, Patch p) => Hold x p -> IO (Subscriber x p)
467newSubscriberHold :: (HasSpiderTimeline x, Patch p) => Hold x p -> IO (Subscriber x p)
437newSubscriberHold h = return $ Subscriber
468newSubscriberHold h = return $ Subscriber
438 { subscriberPropagate = {-# SCC "traverseHold" #-} propagateSubscriberHold h
469 { subscriberPropagate = {-# SCC "traverseHold" #-} propagateSubscriberHold h
@@ -576,6 +607,16 @@ eventSubscribedNever = EventSubscribed
576 , eventSubscribedWhoCreated = return ["never"]
607 , eventSubscribedWhoCreated = return ["never"]
610+eventSubscribedNow :: EventSubscribed x
611+eventSubscribedNow = EventSubscribed
612+ { eventSubscribedHeightRef = zeroRef
613+ , eventSubscribedRetained = toAny ()
615+ , eventSubscribedGetParents = return []
616+ , eventSubscribedHasOwnHeightRef = False
617+ , eventSubscribedWhoCreated = return ["now"]
580eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
621eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
581eventSubscribedFan !subscribed = EventSubscribed
622eventSubscribedFan !subscribed = EventSubscribed
@@ -1351,11 +1392,11 @@ tracePropagate p = when debugPropagate . trace p
1352{-# INLINE traceInvalidate #-}
1393{-# INLINE traceInvalidate #-}
1353traceInvalidate :: String -> IO ()
1394traceInvalidate :: String -> IO ()
1354−traceInvalidate = liftIO . debugStrLn
1395+traceInvalidate = when debugInvalidate . liftIO . debugStrLn
1356{-# INLINE traceInvalidateHeight #-}
1397{-# INLINE traceInvalidateHeight #-}
1357traceInvalidateHeight :: String -> IO ()
1398traceInvalidateHeight :: String -> IO ()
1358−traceInvalidateHeight = liftIO . debugStrLn
1399+traceInvalidateHeight = when debugInvalidateHeight . liftIO . debugStrLn
1360{-# INLINE trace #-}
1401{-# INLINE trace #-}
1361trace :: (CanTrace x m) => proxy x -> String -> m ()
1402trace :: (CanTrace x m) => proxy x -> String -> m ()
@@ -1426,8 +1467,11 @@ showStacks :: [[String]] -> String
1426showStacks = drawForest . listsToForest . fmap (filterStack "Reflex.Spider.Internal")
1467showStacks = drawForest . listsToForest . fmap (filterStack "Reflex.Spider.Internal")
1428filterStack :: String -> [String] -> [String]
1469filterStack :: String -> [String] -> [String]
1470+#ifdef DEBUG_HIDE_INTERNALS
1429filterStack prefix = filter (not . (prefix `isPrefixOf`))
1471filterStack prefix = filter (not . (prefix `isPrefixOf`))
1473+filterStack prefix = id
1432#ifdef DEBUG_CYCLES
1476#ifdef DEBUG_CYCLES
@@ -1454,7 +1498,7 @@ instance Show EventLoopException where
1454{-# INLINE propagateSubscriberHold #-}
1498{-# INLINE propagateSubscriberHold #-}
1455propagateSubscriberHold :: forall x p. (HasSpiderTimeline x, Patch p) => Hold x p -> p -> EventM x ()
1499propagateSubscriberHold :: forall x p. (HasSpiderTimeline x, Patch p) => Hold x p -> p -> EventM x ()
1456propagateSubscriberHold h a = do
1500propagateSubscriberHold h a = do
1457− traceM (Proxy :: Proxy x) $ liftIO $ do
1501+ {-# SCC "trace" #-} when debugPropagate $ traceM (Proxy :: Proxy x) $ liftIO $ do
1458 invalidators <- liftIO $ readIORef $ holdInvalidators h
1502 invalidators <- liftIO $ readIORef $ holdInvalidators h
1459 return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
1503 return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
@@ -1462,7 +1506,7 @@ propagateSubscriberHold h a = do
1462 case {-# SCC "apply" #-} apply a v of
1506 case {-# SCC "apply" #-} apply a v of
1463 Nothing -> return ()
1507 Nothing -> return ()
1464 Just v' -> do
1508 Just v' -> do
1465− withIncreasedDepth (Proxy :: Proxy x) $
1509+ {-# SCC "trace2" #-} withIncreasedDepth (Proxy :: Proxy x) $
1466 tracePropagate (Proxy :: Proxy x) ("propagateSubscriberHold: assigning Hold" <> showNodeId h)
1510 tracePropagate (Proxy :: Proxy x) ("propagateSubscriberHold: assigning Hold" <> showNodeId h)
1467 vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
1511 vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
1468 iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
1512 iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
@@ -1994,7 +2038,7 @@ revalidateMergeHeight m = do
1994 LT -> return ()
2038 LT -> return ()
1995 EQ -> do
2039 EQ -> do
1996 let height = succHeight $ heightBagMax heights
2040 let height = succHeight $ heightBagMax heights
1997− traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
2041+ traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
1998 writeIORef (_merge_heightRef m) $! height
2042 writeIORef (_merge_heightRef m) $! height
1999 subscriberRecalculateHeight (_merge_sub m) height
2043 subscriberRecalculateHeight (_merge_sub m) height
2000 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
2044 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
@@ -2158,7 +2202,7 @@ mergeIntCheap d = Event $ \sub -> do
2158 LT -> return ()
2202 LT -> return ()
2159 EQ -> do
2203 EQ -> do
2160 let height = succHeight $ heightBagMax heights
2204 let height = succHeight $ heightBagMax heights
2161− traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
2205+ traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
2162 writeIORef heightRef $! height
2206 writeIORef heightRef $! height
2163 subscriberRecalculateHeight sub height
2207 subscriberRecalculateHeight sub height
2164 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show numParents <> ") for Merge"
2208 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show numParents <> ") for Merge"
@@ -2476,6 +2520,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Event
2476 {-# INLINABLE headE #-}
2520 {-# INLINABLE headE #-}
2477 headE = R.slowHeadE
2521 headE = R.slowHeadE
2478-- headE (SpiderEvent e) = SpiderEvent <$> Reflex.Spider.Internal.headE e
2522-- headE (SpiderEvent e) = SpiderEvent <$> Reflex.Spider.Internal.headE e
2523+ {-# INLINABLE now #-}
2524+ now = nowSpiderEventM
2480instance Reflex.Class.MonadSample (SpiderTimeline x) (SpiderPullM x) where
2526instance Reflex.Class.MonadSample (SpiderTimeline x) (SpiderPullM x) where
2481 {-# INLINABLE sample #-}
2527 {-# INLINABLE sample #-}
@@ -2497,6 +2543,9 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
2497 {-# INLINABLE headE #-}
2543 {-# INLINABLE headE #-}
2498 headE = R.slowHeadE
2544 headE = R.slowHeadE
2499-- headE (SpiderEvent e) = SpiderPushM $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2545-- headE (SpiderEvent e) = SpiderPushM $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2546+ {-# INLINABLE now #-}
2547+ now = SpiderPushM nowSpiderEventM
2501instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x)) where
2550instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x)) where
2502 {-# INLINE return #-}
2551 {-# INLINE return #-}
@@ -2560,6 +2609,9 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
2560 buildDynamic getV0 e = runFrame . runSpiderHostFrame $ Reflex.Class.buildDynamic getV0 e
2609 buildDynamic getV0 e = runFrame . runSpiderHostFrame $ Reflex.Class.buildDynamic getV0 e
2561 {-# INLINABLE headE #-}
2610 {-# INLINABLE headE #-}
2562 headE e = runFrame . runSpiderHostFrame $ Reflex.Class.headE e
2611 headE e = runFrame . runSpiderHostFrame $ Reflex.Class.headE e
2612+ {-# INLINABLE now #-}
2613+ now = runFrame . runSpiderHostFrame $ Reflex.Class.now
2564instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHostFrame x) where
2616instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHostFrame x) where
2565 sample = SpiderHostFrame . readBehaviorUntracked . unSpiderBehavior --TODO: This can cause problems with laziness, so we should get rid of it if we can
2617 sample = SpiderHostFrame . readBehaviorUntracked . unSpiderBehavior --TODO: This can cause problems with laziness, so we should get rid of it if we can
@@ -2576,6 +2628,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
2576 {-# INLINABLE headE #-}
2628 {-# INLINABLE headE #-}
2577 headE = R.slowHeadE
2629 headE = R.slowHeadE
2578-- headE (SpiderEvent e) = SpiderHostFrame $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2630-- headE (SpiderEvent e) = SpiderHostFrame $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2631+ {-# INLINABLE now #-}
2632+ now = SpiderHostFrame Reflex.Class.now
2580instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHost x) where
2634instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHost x) where
2581 {-# INLINABLE sample #-}
2635 {-# INLINABLE sample #-}
@@ -2596,6 +2650,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Refle
2596 buildDynamic getV0 e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.buildDynamic getV0 e
2650 buildDynamic getV0 e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.buildDynamic getV0 e
2597 {-# INLINABLE headE #-}
2651 {-# INLINABLE headE #-}
2598 headE e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.headE e
2652 headE e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.headE e
2653+ {-# INLINABLE now #-}
2654+ now = Reflex.Spider.Internal.ReadPhase Reflex.Class.now
2600--------------------------------------------------------------------------------
2656--------------------------------------------------------------------------------
2601-- Deprecated items
2657-- Deprecated items
@@ -2719,7 +2775,7 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2719 fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
2775 fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
2720 {-# INLINABLE mergeG #-}
2776 {-# INLINABLE mergeG #-}
2722− :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2778+ :: forall k2 (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2723 => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
2779 => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
2724 -> DMap k q
2780 -> DMap k q
2725 -> R.Event (SpiderTimeline x) (DMap k v)
2781 -> R.Event (SpiderTimeline x) (DMap k v)