@@ -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,7 +19,7 @@
20{-# LANGUAGE DeriveFunctor #-}
19{-# LANGUAGE DeriveFunctor #-}
21{-# LANGUAGE StandaloneDeriving #-}
20{-# LANGUAGE StandaloneDeriving #-}
22{-# LANGUAGE InstanceSigs #-}
21{-# LANGUAGE InstanceSigs #-}
23−{-# LANGUAGE RecursiveDo #-}
22+{-# LANGUAGE MultiWayIf #-}
25#ifdef USE_REFLEX_OPTIMIZER
24#ifdef USE_REFLEX_OPTIMIZER
26{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
25{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
@@ -39,15 +38,17 @@ import Control.Monad hiding (forM, forM_, mapM, mapM_)
39import Control.Monad.Exception
38import Control.Monad.Exception
40import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
39import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
41import Control.Monad.Primitive
40import Control.Monad.Primitive
42−--import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_)
43import Control.Monad.Reader.Class
41import Control.Monad.Reader.Class
44import Control.Monad.IO.Class
42import Control.Monad.IO.Class
45import Control.Monad.ReaderIO
43import Control.Monad.ReaderIO
46import Control.Monad.Ref
44import Control.Monad.Ref
45+import Control.Monad.Fail (MonadFail)
46+import qualified Control.Monad.Fail as MonadFail
47import Data.Align
47import Data.Align
48import Data.Coerce
48import Data.Coerce
49−import Data.Dependent.Map (DMap, DSum (..))
49+import Data.Dependent.Map (DMap)
50import qualified Data.Dependent.Map as DMap
50import qualified Data.Dependent.Map as DMap
51+import Data.Dependent.Sum (DSum (..))
51import Data.FastMutableIntMap (FastMutableIntMap, PatchIntMap (..))
52import Data.FastMutableIntMap (FastMutableIntMap, PatchIntMap (..))
52import qualified Data.FastMutableIntMap as FastMutableIntMap
53import qualified Data.FastMutableIntMap as FastMutableIntMap
53import Data.Foldable hiding (concat, elem, sequence_)
54import Data.Foldable hiding (concat, elem, sequence_)
@@ -64,6 +65,7 @@ import Data.Monoid (mempty, (<>))
64import Data.Proxy
65import Data.Proxy
65import Data.These
66import Data.These
66import Data.Traversable
67import Data.Traversable
68+import Data.Type.Equality ((:~:)(Refl))
67import Data.Witherable (Filterable, mapMaybe)
69import Data.Witherable (Filterable, mapMaybe)
68import GHC.Exts hiding (toList)
70import GHC.Exts hiding (toList)
69import GHC.IORef (IORef (..))
71import GHC.IORef (IORef (..))
@@ -84,9 +86,9 @@ import Data.Zip (Zip (..))
85#ifdef DEBUG_CYCLES
87#ifdef DEBUG_CYCLES
86import Control.Monad.State hiding (forM, forM_, mapM, mapM_, sequence)
88import Control.Monad.State hiding (forM, forM_, mapM, mapM_, sequence)
87−import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
91+import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
90import qualified Data.List.NonEmpty as NonEmpty
92import qualified Data.List.NonEmpty as NonEmpty
91import Data.Tree (Forest, Tree (..), drawForest)
93import Data.Tree (Forest, Tree (..), drawForest)
92import Data.List (isPrefixOf)
94import Data.List (isPrefixOf)
@@ -104,15 +106,21 @@ import qualified Reflex.Class
104import qualified Reflex.Class as R
106import qualified Reflex.Class as R
105import qualified Reflex.Host.Class
107import qualified Reflex.Host.Class
106import Reflex.NotReady.Class
108import Reflex.NotReady.Class
107−import Reflex.Patch
109+import Data.Patch
108−import qualified Reflex.Patch.DMapWithMove as PatchDMapWithMove
110+import qualified Data.Patch.DMapWithMove as PatchDMapWithMove
109import Reflex.PerformEvent.Base (PerformEventT)
111import Reflex.PerformEvent.Base (PerformEventT)
111#ifdef DEBUG_TRACE_EVENTS
113#ifdef DEBUG_TRACE_EVENTS
112import qualified Data.ByteString.Char8 as BS8
114import qualified Data.ByteString.Char8 as BS8
113import System.IO (stderr)
115import System.IO (stderr)
114import Data.List (isPrefixOf)
116import Data.List (isPrefixOf)
119+-- TODO stdout might not be the best channel for debug output
120+debugStrLn :: String -> IO ()
121+debugStrLn = putStrLn
123+#ifdef DEBUG_TRACE_EVENTS
116withStackOneLine :: (BS8.ByteString -> a) -> a
124withStackOneLine :: (BS8.ByteString -> a) -> a
117withStackOneLine expr = unsafePerformIO $ do
125withStackOneLine expr = unsafePerformIO $ do
118 stack <- currentCallStack
126 stack <- currentCallStack
@@ -121,15 +129,39 @@ withStackOneLine expr = unsafePerformIO $ do
132+debugPropagate :: Bool
134+debugInvalidateHeight :: Bool
136+debugInvalidate :: Bool
124#ifdef DEBUG
138#ifdef DEBUG
125#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
127class HasNodeId a where
159class HasNodeId a where
128 getNodeId :: a -> Int
160 getNodeId :: a -> Int
130instance HasNodeId (CacheSubscribed x a) where
162instance HasNodeId (CacheSubscribed x a) where
131 getNodeId = _cacheSubscribed_nodeId
163 getNodeId = _cacheSubscribed_nodeId
133instance HasNodeId (FanInt x a) where
165instance HasNodeId (FanInt x a) where
134 getNodeId = _fanInt_nodeId
166 getNodeId = _fanInt_nodeId
@@ -156,16 +188,24 @@ showNodeId :: HasNodeId a => a -> String
156showNodeId = showNodeId' . getNodeId
188showNodeId = showNodeId' . getNodeId
158showNodeId' :: Int -> String
190showNodeId' :: Int -> String
159−showNodeId' = ("#"<>) . show
191+showNodeId' = ("#"<>) . show
196+debugPropagate = False
197+debugInvalidateHeight = False
198+debugInvalidate = False
164-- 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
165{-# INLINE showNodeId #-}
201{-# INLINE showNodeId #-}
166showNodeId :: a -> String
202showNodeId :: a -> String
167showNodeId _ = ""
203showNodeId _ = ""
205+{-# INLINE showNodeId' #-}
206+showNodeId' :: Int -> String
171#ifdef DEBUG_NODEIDS
211#ifdef DEBUG_NODEIDS
@@ -213,11 +253,11 @@ subscribeAndRead = unEvent
213{-# INLINE [1] pushCheap #-}
253{-# INLINE [1] pushCheap #-}
214pushCheap :: HasSpiderTimeline x => (a -> ComputeM x (Maybe b)) -> Event x a -> Event x b
254pushCheap :: HasSpiderTimeline x => (a -> ComputeM x (Maybe b)) -> Event x a -> Event x b
215pushCheap !f e = Event $ \sub -> do
255pushCheap !f e = Event $ \sub -> do
216− (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" (sub
256+ (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" $ sub
217 { subscriberPropagate = \a -> do
257 { subscriberPropagate = \a -> do
218 mb <- f a
258 mb <- f a
219 mapM_ (subscriberPropagate sub) mb
259 mapM_ (subscriberPropagate sub) mb
221 occ' <- join <$> mapM f occ
261 occ' <- join <$> mapM f occ
222 return (subscription, occ')
262 return (subscription, occ')
@@ -253,9 +293,9 @@ headE originalE = do
253 let clearParent = liftIO $ writeIORef parent Nothing
293 let clearParent = liftIO $ writeIORef parent Nothing
254 (_, occ) <- subscribeAndReadHead originalE $ terminalSubscriber $ const clearParent
294 (_, occ) <- subscribeAndReadHead originalE $ terminalSubscriber $ const clearParent
255 when (isJust occ) clearParent
295 when (isJust occ) clearParent
256− return $ Event $ \sub ->
296+ return $ Event $ \sub ->
257 liftIO (readIORef parent) >>= \case
297 liftIO (readIORef parent) >>= \case
258− Nothing -> subscribeAndReadNever
298+ Nothing -> subscribeAndReadNever
259 Just e -> subscribeAndReadHead e sub
299 Just e -> subscribeAndReadHead e sub
261data CacheSubscribed x a
301data CacheSubscribed x a
@@ -267,7 +307,20 @@ data CacheSubscribed x a
270−
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
272-- | Construct an 'Event' whose value is guaranteed not to be recomputed
325-- | Construct an 'Event' whose value is guaranteed not to be recomputed
273-- repeatedly
326-- repeatedly
@@ -298,39 +351,18 @@ cacheEvent e =
299 subscribers <- liftIO FastWeakBag.empty
352 subscribers <- liftIO FastWeakBag.empty
300 occRef <- liftIO $ newIORef Nothing -- This should never be read prior to being set below
353 occRef <- liftIO $ newIORef Nothing -- This should never be read prior to being set below
301−<<<<<<< HEAD
354+#ifdef DEBUG_NODEIDS
302 (parentSub, occ) <- subscribeAndRead e $ debugSubscriber' ("cacheEvent" <> showNodeId' nodeId) $ Subscriber
355 (parentSub, occ) <- subscribeAndRead e $ debugSubscriber' ("cacheEvent" <> showNodeId' nodeId) $ Subscriber
357+ (parentSub, occ) <- subscribeAndRead e $ Subscriber
303 { subscriberPropagate = \a -> do
359 { subscriberPropagate = \a -> do
304− -- checkCycle (_eventSubscription_subscribed parentSub)
305 liftIO $ writeIORef occRef (Just a)
360 liftIO $ writeIORef occRef (Just a)
306 scheduleClear occRef
361 scheduleClear occRef
307 propagateFast a subscribers
362 propagateFast a subscribers
308− , subscriberInvalidateHeight = FastWeakBag.traverse subscribers . invalidateSubscriberHeight
363+ , subscriberInvalidateHeight = FastWeakBag.traverse_ subscribers . invalidateSubscriberHeight
309− , subscriberRecalculateHeight = FastWeakBag.traverse subscribers . recalculateSubscriberHeight
364+ , subscriberRecalculateHeight = FastWeakBag.traverse_ subscribers . recalculateSubscriberHeight
311−||||||| merged common ancestors
312− (parentSub, occ) <- subscribeAndRead e $ Subscriber
313− { subscriberPropagate = \a -> do
314− liftIO $ writeIORef occRef $ Just a
315− scheduleClear occRef
316− propagateFast a subscribers
317− , subscriberInvalidateHeight = \old -> do
318− FastWeakBag.traverse subscribers $ invalidateSubscriberHeight old
319− , subscriberRecalculateHeight = \new -> do
320− FastWeakBag.traverse subscribers $ recalculateSubscriberHeight new
323− (parentSub, occ) <- subscribeAndRead e $ Subscriber
324− { subscriberPropagate = \a -> do
325− liftIO $ writeIORef occRef $ Just a
326− scheduleClear occRef
327− propagateFast a subscribers
328− , subscriberInvalidateHeight = \old -> do
329− FastWeakBag.traverse_ subscribers $ invalidateSubscriberHeight old
330− , subscriberRecalculateHeight = \new -> do
331− FastWeakBag.traverse_ subscribers $ recalculateSubscriberHeight new
334 when (isJust occ) $ do
366 when (isJust occ) $ do
335 liftIO $ writeIORef occRef occ -- Set the initial value of occRef; we don't need to do this if occ is Nothing
367 liftIO $ writeIORef occRef occ -- Set the initial value of occRef; we don't need to do this if occ is Nothing
336 scheduleClear occRef
368 scheduleClear occRef
@@ -340,20 +372,20 @@ cacheEvent e =
340 , _cacheSubscribed_occurrence = occRef
372 , _cacheSubscribed_occurrence = occRef
341#ifdef DEBUG_NODEIDS
373#ifdef DEBUG_NODEIDS
342 , _cacheSubscribed_nodeId = nodeId
374 , _cacheSubscribed_nodeId = nodeId
345 subscribedTicket <- liftIO $ mkFastWeakTicket subscribed
377 subscribedTicket <- liftIO $ mkFastWeakTicket subscribed
346 liftIO $ writeIORef mSubscribedRef =<< getFastWeakTicketWeak subscribedTicket
378 liftIO $ writeIORef mSubscribedRef =<< getFastWeakTicketWeak subscribedTicket
347 return subscribedTicket
379 return subscribedTicket
348 liftIO $ cacheSubscription sub mSubscribedRef subscribedTicket
380 liftIO $ cacheSubscription sub mSubscribedRef subscribedTicket
350−cacheSubscription :: Subscriber x a -> IORef (FastWeak (CacheSubscribed x a))
382+cacheSubscription :: Subscriber x a -> IORef (FastWeak (CacheSubscribed x a))
351 -> FastWeakTicket (CacheSubscribed x a) -> IO (EventSubscription x, Maybe a)
383 -> FastWeakTicket (CacheSubscribed x a) -> IO (EventSubscription x, Maybe a)
352−cacheSubscription sub mSubscribedRef subscribedTicket = do
384+cacheSubscription sub mSubscribedRef subscribedTicket = do
353 subscribed <- getFastWeakTicketValue subscribedTicket
385 subscribed <- getFastWeakTicketValue subscribedTicket
354 ticket <- FastWeakBag.insert sub $ _cacheSubscribed_subscribers subscribed
386 ticket <- FastWeakBag.insert sub $ _cacheSubscribed_subscribers subscribed
355 occ <- readIORef $ _cacheSubscribed_occurrence subscribed
387 occ <- readIORef $ _cacheSubscribed_occurrence subscribed
357 let parentSub = _cacheSubscribed_parent subscribed
389 let parentSub = _cacheSubscribed_parent subscribed
358 es = EventSubscription
390 es = EventSubscription
359 { _eventSubscription_unsubscribe = do
391 { _eventSubscription_unsubscribe = do
@@ -372,9 +404,9 @@ cacheSubscription sub mSubscribedRef subscribedTicket = do
372 , eventSubscribedGetParents = return [_eventSubscription_subscribed parentSub]
404 , eventSubscribedGetParents = return [_eventSubscription_subscribed parentSub]
373 , eventSubscribedHasOwnHeightRef = False
405 , eventSubscribedHasOwnHeightRef = False
374 , eventSubscribedWhoCreated = whoCreatedIORef mSubscribedRef
406 , eventSubscribedWhoCreated = whoCreatedIORef mSubscribedRef
378 return (es, occ)
410 return (es, occ)
@@ -432,16 +464,6 @@ data Subscriber x a = Subscriber
432 , subscriberRecalculateHeight :: !(Height -> IO ())
464 , subscriberRecalculateHeight :: !(Height -> IO ())
435−--TODO: Move this comment to WeakBag
436−-- These function are constructor functions that are marked NOINLINE so they are
437−-- opaque to GHC. If we do not do this, then GHC will sometimes fuse the constructor away
438−-- so any weak references that are attached to the constructors will have their
439−-- finalizer run. Using the opaque constructor, does not see the
440−-- constructor application, so it behaves like an IORef and cannot be fused away.
442−-- The result is also evaluated to WHNF, since forcing a thunk invalidates
443−-- the weak pointer to it in some cases.
445newSubscriberHold :: (HasSpiderTimeline x, Patch p) => Hold x p -> IO (Subscriber x p)
467newSubscriberHold :: (HasSpiderTimeline x, Patch p) => Hold x p -> IO (Subscriber x p)
446newSubscriberHold h = return $ Subscriber
468newSubscriberHold h = return $ Subscriber
447 { subscriberPropagate = {-# SCC "traverseHold" #-} propagateSubscriberHold h
469 { subscriberPropagate = {-# SCC "traverseHold" #-} propagateSubscriberHold h
@@ -495,7 +517,7 @@ newSubscriberCoincidenceOuter subscribed = debugSubscriber ("SubscriberCoinciden
495 liftIO $ writeIORef (coincidenceSubscribedInnerParent subscribed) $ Just innerSubd
517 liftIO $ writeIORef (coincidenceSubscribedInnerParent subscribed) $ Just innerSubd
496 scheduleClear $ coincidenceSubscribedInnerParent subscribed
518 scheduleClear $ coincidenceSubscribedInnerParent subscribed
497 case occ of
519 case occ of
498− Nothing ->
520+ Nothing ->
499 when (innerHeight > outerHeight) $ liftIO $ do -- If the event fires, it will fire at a later height
521 when (innerHeight > outerHeight) $ liftIO $ do -- If the event fires, it will fire at a later height
500 writeIORef (coincidenceSubscribedHeight subscribed) $! innerHeight
522 writeIORef (coincidenceSubscribedHeight subscribed) $! innerHeight
501 WeakBag.traverse_ (coincidenceSubscribedSubscribers subscribed) $ invalidateSubscriberHeight outerHeight
523 WeakBag.traverse_ (coincidenceSubscribedSubscribers subscribed) $ invalidateSubscriberHeight outerHeight
@@ -505,7 +527,7 @@ newSubscriberCoincidenceOuter subscribed = debugSubscriber ("SubscriberCoinciden
505 scheduleClear $ coincidenceSubscribedOccurrence subscribed
527 scheduleClear $ coincidenceSubscribedOccurrence subscribed
506 propagate o $ coincidenceSubscribedSubscribers subscribed
528 propagate o $ coincidenceSubscribedSubscribers subscribed
507 , subscriberInvalidateHeight = \_ -> invalidateCoincidenceHeight subscribed
529 , subscriberInvalidateHeight = \_ -> invalidateCoincidenceHeight subscribed
508− , subscriberRecalculateHeight = \_ -> recalculateCoincidenceHeight subscribed
530+ , subscriberRecalculateHeight = \_ -> recalculateCoincidenceHeight subscribed
511newSubscriberCoincidenceInner :: forall x a. HasSpiderTimeline x => CoincidenceSubscribed x a -> IO (Subscriber x a)
533newSubscriberCoincidenceInner :: forall x a. HasSpiderTimeline x => CoincidenceSubscribed x a -> IO (Subscriber x a)
@@ -526,18 +548,18 @@ invalidateSubscriberHeight :: Height -> Subscriber x a -> IO ()
526invalidateSubscriberHeight = flip subscriberInvalidateHeight
548invalidateSubscriberHeight = flip subscriberInvalidateHeight
528recalculateSubscriberHeight :: Height -> Subscriber x a -> IO ()
550recalculateSubscriberHeight :: Height -> Subscriber x a -> IO ()
529−recalculateSubscriberHeight = flip subscriberRecalculateHeight
551+recalculateSubscriberHeight = flip subscriberRecalculateHeight
531-- | Propagate everything at the current height
553-- | Propagate everything at the current height
532propagate :: forall x a. HasSpiderTimeline x => a -> WeakBag (Subscriber x a) -> EventM x ()
554propagate :: forall x a. HasSpiderTimeline x => a -> WeakBag (Subscriber x a) -> EventM x ()
533−propagate a subscribers = withIncreasedDepth (Proxy::Proxy x) $
555+propagate a subscribers = withIncreasedDepth (Proxy::Proxy x) $
534 -- Note: in the following traversal, we do not visit nodes that are added to the list during our traversal; they are new events, which will necessarily have full information already, so there is no need to traverse them
556 -- Note: in the following traversal, we do not visit nodes that are added to the list during our traversal; they are new events, which will necessarily have full information already, so there is no need to traverse them
535 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
557 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
536 WeakBag.traverse_ subscribers $ \s -> subscriberPropagate s a
558 WeakBag.traverse_ subscribers $ \s -> subscriberPropagate s a
538-- | Propagate everything at the current height
560-- | Propagate everything at the current height
539propagateFast :: forall x a. HasSpiderTimeline x => a -> FastWeakBag (Subscriber x a) -> EventM x ()
561propagateFast :: forall x a. HasSpiderTimeline x => a -> FastWeakBag (Subscriber x a) -> EventM x ()
540−propagateFast a subscribers = withIncreasedDepth (Proxy::Proxy x) $
562+propagateFast a subscribers = withIncreasedDepth (Proxy::Proxy x) $
541 -- Note: in the following traversal, we do not visit nodes that are added to the list during our traversal; they are new events, which will necessarily have full information already, so there is no need to traverse them
563 -- Note: in the following traversal, we do not visit nodes that are added to the list during our traversal; they are new events, which will necessarily have full information already, so there is no need to traverse them
542 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
564 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
543 FastWeakBag.traverse_ subscribers $ \s -> subscriberPropagate s a
565 FastWeakBag.traverse_ subscribers $ \s -> subscriberPropagate s a
@@ -585,6 +607,16 @@ eventSubscribedNever = EventSubscribed
585 , 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"]
589eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
621eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
590eventSubscribedFan !subscribed = EventSubscribed
622eventSubscribedFan !subscribed = EventSubscribed
@@ -962,8 +994,10 @@ instance Monad (BehaviorM x) where
962 BehaviorM x >> BehaviorM y = BehaviorM $ x >> y
994 BehaviorM x >> BehaviorM y = BehaviorM $ x >> y
963 {-# INLINE return #-}
995 {-# INLINE return #-}
964 return x = BehaviorM $ return x
996 return x = BehaviorM $ return x
997+#if !MIN_VERSION_base(4,13,0)
965 {-# INLINE fail #-}
998 {-# INLINE fail #-}
966 fail s = BehaviorM $ fail s
999 fail s = BehaviorM $ fail s
968data BehaviorSubscribed x a
1002data BehaviorSubscribed x a
969 = forall p. BehaviorSubscribedHold (Hold x p)
1003 = forall p. BehaviorSubscribedHold (Hold x p)
@@ -1023,7 +1057,7 @@ data SomeMergeUpdate x = SomeMergeUpdate
1023newtype SomeMergeInit x = SomeMergeInit { unSomeMergeInit :: EventM x () }
1057newtype SomeMergeInit x = SomeMergeInit { unSomeMergeInit :: EventM x () }
1025-- EventM can do everything BehaviorM can, plus create holds
1059-- EventM can do everything BehaviorM can, plus create holds
1026−newtype EventM x a = EventM { unEventM :: IO a } deriving (Functor, Applicative, Monad, MonadIO, MonadFix, MonadException, MonadAsyncException)
1060+newtype EventM x a = EventM { unEventM :: IO a } deriving (Functor, Applicative, Monad, MonadIO, MonadFix, MonadException, MonadAsyncException)
1028newtype MergeSubscribedParent x a = MergeSubscribedParent { unMergeSubscribedParent :: EventSubscription x }
1062newtype MergeSubscribedParent x a = MergeSubscribedParent { unMergeSubscribedParent :: EventSubscription x }
@@ -1048,7 +1082,7 @@ heightBagFromList :: [Height] -> HeightBag
1048heightBagFromList heights = heightBagVerify $ foldl' (flip heightBagAdd) heightBagEmpty heights
1082heightBagFromList heights = heightBagVerify $ foldl' (flip heightBagAdd) heightBagEmpty heights
1050heightBagAdd :: Height -> HeightBag -> HeightBag
1084heightBagAdd :: Height -> HeightBag -> HeightBag
1051−heightBagAdd (Height h) (HeightBag s c) = heightBagVerify $ HeightBag (succ s) $
1085+heightBagAdd (Height h) (HeightBag s c) = heightBagVerify $ HeightBag (succ s) $
1052 IntMap.insertWithKey (\_ _ old -> succ old) h 0 c
1086 IntMap.insertWithKey (\_ _ old -> succ old) h 0 c
1054heightBagRemove :: Height -> HeightBag -> HeightBag
1088heightBagRemove :: Height -> HeightBag -> HeightBag
@@ -1059,11 +1093,11 @@ heightBagRemove (Height h) b@(HeightBag s c) = heightBagVerify $ case IntMap.loo
1059 _ -> IntMap.insert h (pred old) c
1093 _ -> IntMap.insert h (pred old) c
1061heightBagRemoveMaybe :: Height -> HeightBag -> Maybe HeightBag
1095heightBagRemoveMaybe :: Height -> HeightBag -> Maybe HeightBag
1062−heightBagRemoveMaybe (Height h) b@(HeightBag s c) = heightBagVerify . removed <$> IntMap.lookup h c where
1096+heightBagRemoveMaybe (Height h) b@(HeightBag s c) = heightBagVerify . removed <$> IntMap.lookup h c where
1063 removed old = HeightBag (pred s) $ case old of
1097 removed old = HeightBag (pred s) $ case old of
1064 0 -> IntMap.delete h c
1098 0 -> IntMap.delete h c
1065 _ -> IntMap.insert h (pred old) c
1099 _ -> IntMap.insert h (pred old) c
1068heightBagMax :: HeightBag -> Height
1102heightBagMax :: HeightBag -> Height
1069heightBagMax (HeightBag _ c) = case IntMap.maxViewWithKey c of
1103heightBagMax (HeightBag _ c) = case IntMap.maxViewWithKey c of
@@ -1312,7 +1346,7 @@ mkWeakPtrWithDebug x debugNote = do
1312 x' <- evaluate x
1346 x' <- evaluate x
1313 mkWeakPtr x' $
1347 mkWeakPtr x' $
1314 if debugFinalize
1348 if debugFinalize
1315− then Just $ putStrLn $ "finalizing: " ++ debugNote
1349+ then Just $ debugStrLn $ "finalizing: " ++ debugNote
1316 else Nothing
1350 else Nothing
1318type WeakList a = [Weak a]
1352type WeakList a = [Weak a]
@@ -1328,19 +1362,19 @@ debugSubscriber :: forall x a. HasSpiderTimeline x => String -> Subscriber x a -
1328debugSubscriber description = return . debugSubscriber' description
1362debugSubscriber description = return . debugSubscriber' description
1330debugSubscriber' :: forall x a. HasSpiderTimeline x => String -> Subscriber x a -> Subscriber x a
1364debugSubscriber' :: forall x a. HasSpiderTimeline x => String -> Subscriber x a -> Subscriber x a
1331−debugSubscriber' description subscribed = Subscriber
1365+debugSubscriber' description subscribed = Subscriber
1333 subscriberPropagate = \m -> do
1367 subscriberPropagate = \m -> do
1334 tracePropagate (Proxy :: Proxy x) ("subscriberPropagate: " <> description)
1368 tracePropagate (Proxy :: Proxy x) ("subscriberPropagate: " <> description)
1335 subscriberPropagate subscribed m
1369 subscriberPropagate subscribed m
1336 , subscriberInvalidateHeight = \old -> do
1370 , subscriberInvalidateHeight = \old -> do
1337− putStrLn $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
1371+ traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
1338 subscriberInvalidateHeight subscribed old
1372 subscriberInvalidateHeight subscribed old
1339− putStrLn $ "invalidateSubscriberHeight: " <> description <> ", done"
1373+ traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", done"
1340 , subscriberRecalculateHeight = \new -> do
1374 , subscriberRecalculateHeight = \new -> do
1341− putStrLn $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
1375+ traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
1342 subscriberRecalculateHeight subscribed new
1376 subscriberRecalculateHeight subscribed new
1343− putStrLn $ "subscriberRecalculateHeight: " <> description <> ", done"
1377+ traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", done"
@@ -1354,11 +1388,15 @@ withIncreasedDepth _ a = do
1355{-# INLINE tracePropagate #-}
1389{-# INLINE tracePropagate #-}
1356tracePropagate :: (CanTrace x m) => proxy x -> String -> m ()
1390tracePropagate :: (CanTrace x m) => proxy x -> String -> m ()
1357−tracePropagate p = trace p
1391+tracePropagate p = when debugPropagate . trace p
1359{-# INLINE traceInvalidate #-}
1393{-# INLINE traceInvalidate #-}
1360traceInvalidate :: String -> IO ()
1394traceInvalidate :: String -> IO ()
1361−traceInvalidate = liftIO . putStrLn
1395+traceInvalidate = when debugInvalidate . liftIO . debugStrLn
1397+{-# INLINE traceInvalidateHeight #-}
1398+traceInvalidateHeight :: String -> IO ()
1399+traceInvalidateHeight = when debugInvalidateHeight . liftIO . debugStrLn
1363{-# INLINE trace #-}
1401{-# INLINE trace #-}
1364trace :: (CanTrace x m) => proxy x -> String -> m ()
1402trace :: (CanTrace x m) => proxy x -> String -> m ()
@@ -1369,7 +1407,7 @@ traceM :: forall x proxy m. (CanTrace x m) => proxy x -> m String -> m ()
1369traceM _ getMessage = do
1407traceM _ getMessage = do
1370 message <- getMessage
1408 message <- getMessage
1371 d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
1409 d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
1372− liftIO $ putStrLn $ replicate d ' ' <> message
1410+ liftIO $ debugStrLn $ replicate d ' ' <> message
@@ -1385,6 +1423,10 @@ tracePropagate _ _ = return ()
1385traceInvalidate :: String -> IO ()
1423traceInvalidate :: String -> IO ()
1386traceInvalidate _ = return ()
1424traceInvalidate _ = return ()
1426+{-# INLINE traceInvalidateHeight #-}
1427+traceInvalidateHeight :: String -> IO ()
1428+traceInvalidateHeight _ = return ()
1388{-# INLINE debugSubscriber #-}
1430{-# INLINE debugSubscriber #-}
1389debugSubscriber :: String -> Subscriber x a -> IO (Subscriber x a)
1431debugSubscriber :: String -> Subscriber x a -> IO (Subscriber x a)
1390debugSubscriber _ = return
1432debugSubscriber _ = return
@@ -1425,17 +1467,20 @@ showStacks :: [[String]] -> String
1425showStacks = drawForest . listsToForest . fmap (filterStack "Reflex.Spider.Internal")
1467showStacks = drawForest . listsToForest . fmap (filterStack "Reflex.Spider.Internal")
1427filterStack :: String -> [String] -> [String]
1469filterStack :: String -> [String] -> [String]
1470+#ifdef DEBUG_HIDE_INTERNALS
1428filterStack prefix = filter (not . (prefix `isPrefixOf`))
1471filterStack prefix = filter (not . (prefix `isPrefixOf`))
1473+filterStack prefix = id
1431#ifdef DEBUG_CYCLES
1476#ifdef DEBUG_CYCLES
1433−data EventLoopException = EventLoopException [[String]]
1478+data EventLoopException = EventLoopException [[String]]
1434instance Exception EventLoopException
1479instance Exception EventLoopException
1436instance Show EventLoopException where
1481instance Show EventLoopException where
1437− show (EventLoopException stacks) = "causality loop detected:\n" <> if null stacks
1482+ show (EventLoopException stacks) = "causality loop detected:\n" <> if null stacks
1438− then "no location information, compile with profiling enabled for stack tree"
1483+ then "no location information, compile with profiling enabled for stack tree"
1439 else showStacks stacks
1484 else showStacks stacks
@@ -1453,7 +1498,7 @@ instance Show EventLoopException where
1453{-# INLINE propagateSubscriberHold #-}
1498{-# INLINE propagateSubscriberHold #-}
1454propagateSubscriberHold :: 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 ()
1455propagateSubscriberHold h a = do
1500propagateSubscriberHold h a = do
1456− traceM (Proxy :: Proxy x) $ liftIO $ do
1501+ {-# SCC "trace" #-} when debugPropagate $ traceM (Proxy :: Proxy x) $ liftIO $ do
1457 invalidators <- liftIO $ readIORef $ holdInvalidators h
1502 invalidators <- liftIO $ readIORef $ holdInvalidators h
1458 return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
1503 return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
@@ -1461,7 +1506,7 @@ propagateSubscriberHold h a = do
1461 case {-# SCC "apply" #-} apply a v of
1506 case {-# SCC "apply" #-} apply a v of
1462 Nothing -> return ()
1507 Nothing -> return ()
1463 Just v' -> do
1508 Just v' -> do
1464− withIncreasedDepth (Proxy :: Proxy x) $
1509+ {-# SCC "trace2" #-} withIncreasedDepth (Proxy :: Proxy x) $
1465 tracePropagate (Proxy :: Proxy x) ("propagateSubscriberHold: assigning Hold" <> showNodeId h)
1510 tracePropagate (Proxy :: Proxy x) ("propagateSubscriberHold: assigning Hold" <> showNodeId h)
1466 vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
1511 vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
1467 iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
1512 iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
@@ -1582,7 +1627,7 @@ data FanInt x a = FanInt
1582 { _fanInt_subscribers :: {-# UNPACK #-} !(FastMutableIntMap (FastWeakBag (Subscriber x a))) --TODO: Clean up the keys in here when their child weak bags get empty --TODO: Remove our own subscription when the subscribers list is completely empty
1627 { _fanInt_subscribers :: {-# UNPACK #-} !(FastMutableIntMap (FastWeakBag (Subscriber x a))) --TODO: Clean up the keys in here when their child weak bags get empty --TODO: Remove our own subscription when the subscribers list is completely empty
1583 , _fanInt_subscriptionRef :: {-# UNPACK #-} !(IORef (EventSubscription x)) -- This should have a valid subscription iff subscribers is non-empty
1628 , _fanInt_subscriptionRef :: {-# UNPACK #-} !(IORef (EventSubscription x)) -- This should have a valid subscription iff subscribers is non-empty
1584 , _fanInt_occRef :: {-# UNPACK #-} !(IORef (IntMap a))
1629 , _fanInt_occRef :: {-# UNPACK #-} !(IORef (IntMap a))
1585−#ifdef DEBUG_NODEIDS
1630+#ifdef DEBUG_NODEIDS
1586 , _fanInt_nodeId :: {-# UNPACK #-} !Int
1631 , _fanInt_nodeId :: {-# UNPACK #-} !Int
@@ -1592,15 +1637,15 @@ newFanInt = do
1592 subscribers <- FastMutableIntMap.newEmpty --TODO: Clean up the keys in here when their child weak bags get empty --TODO: Remove our own subscription when the subscribers list is completely empty
1637 subscribers <- FastMutableIntMap.newEmpty --TODO: Clean up the keys in here when their child weak bags get empty --TODO: Remove our own subscription when the subscribers list is completely empty
1593 subscriptionRef <- newIORef $ error "fanInt: no subscription"
1638 subscriptionRef <- newIORef $ error "fanInt: no subscription"
1594 occRef <- newIORef $ error "fanInt: no occurrence"
1639 occRef <- newIORef $ error "fanInt: no occurrence"
1595−#ifdef DEBUG_NODEIDS
1640+#ifdef DEBUG_NODEIDS
1596 nodeId <- newNodeId
1641 nodeId <- newNodeId
1598 return $ FanInt
1643 return $ FanInt
1599 { _fanInt_subscribers = subscribers
1644 { _fanInt_subscribers = subscribers
1600 , _fanInt_subscriptionRef = subscriptionRef
1645 , _fanInt_subscriptionRef = subscriptionRef
1601 , _fanInt_occRef = occRef
1646 , _fanInt_occRef = occRef
1602−#ifdef DEBUG_NODEIDS
1647+#ifdef DEBUG_NODEIDS
1603− , _fanInt_nodeId = nodeId
1648+ , _fanInt_nodeId = nodeId
@@ -1616,15 +1661,15 @@ fanInt p = unsafePerformIO $ do
1616 liftIO $ writeIORef (_fanInt_occRef self) m
1661 liftIO $ writeIORef (_fanInt_occRef self) m
1617 scheduleIntClear $ _fanInt_occRef self
1662 scheduleIntClear $ _fanInt_occRef self
1618 FastMutableIntMap.forIntersectionWithImmutable_ (_fanInt_subscribers self) m $ \b v -> --TODO: Do we need to know that no subscribers are being added as we traverse?
1663 FastMutableIntMap.forIntersectionWithImmutable_ (_fanInt_subscribers self) m $ \b v -> --TODO: Do we need to know that no subscribers are being added as we traverse?
1619− FastWeakBag.traverse_ b $ \s ->
1664+ FastWeakBag.traverse_ b $ \s ->
1620 subscriberPropagate s v
1665 subscriberPropagate s v
1621− , subscriberInvalidateHeight = \old ->
1666+ , subscriberInvalidateHeight = \old ->
1622− FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1667+ FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1623− FastWeakBag.traverse_ b $ \s ->
1668+ FastWeakBag.traverse_ b $ \s ->
1624 subscriberInvalidateHeight s old
1669 subscriberInvalidateHeight s old
1625 , subscriberRecalculateHeight = \new ->
1670 , subscriberRecalculateHeight = \new ->
1626− FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1671+ FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1627− FastWeakBag.traverse_ b $ \s ->
1672+ FastWeakBag.traverse_ b $ \s ->
1628 subscriberRecalculateHeight s new
1673 subscriberRecalculateHeight s new
1630 liftIO $ do
1675 liftIO $ do
@@ -1640,14 +1685,14 @@ fanInt p = unsafePerformIO $ do
1640 Just b -> return b
1685 Just b -> return b
1641 ticket <- liftIO $ FastWeakBag.insert sub b
1686 ticket <- liftIO $ FastWeakBag.insert sub b
1642 currentOcc <- readIORef (_fanInt_occRef self)
1687 currentOcc <- readIORef (_fanInt_occRef self)
1644 subscribed <- fanIntSubscribed ticket self
1689 subscribed <- fanIntSubscribed ticket self
1645 return (EventSubscription (FastWeakBag.remove ticket) subscribed, IntMap.lookup k currentOcc)
1690 return (EventSubscription (FastWeakBag.remove ticket) subscribed, IntMap.lookup k currentOcc)
1647fanIntSubscribed :: FastWeakBagTicket k -> FanInt x a -> IO (EventSubscribed x)
1692fanIntSubscribed :: FastWeakBagTicket k -> FanInt x a -> IO (EventSubscribed x)
1648−fanIntSubscribed ticket self = do
1693+fanIntSubscribed ticket self = do
1649 subscribedParent <- _eventSubscription_subscribed <$> readIORef (_fanInt_subscriptionRef self)
1694 subscribedParent <- _eventSubscription_subscribed <$> readIORef (_fanInt_subscriptionRef self)
1650− return $ EventSubscribed
1695+ return $ EventSubscribed
1651 { eventSubscribedHeightRef = eventSubscribedHeightRef subscribedParent
1696 { eventSubscribedHeightRef = eventSubscribedHeightRef subscribedParent
1652 , eventSubscribedRetained = toAny (_fanInt_subscriptionRef self, ticket)
1697 , eventSubscribedRetained = toAny (_fanInt_subscriptionRef self, ticket)
1653#ifdef DEBUG_CYCLES
1698#ifdef DEBUG_CYCLES
@@ -1656,7 +1701,7 @@ fanIntSubscribed ticket self = do
1656 , eventSubscribedWhoCreated = whoCreatedIORef $ _fanInt_subscriptionRef self
1701 , eventSubscribedWhoCreated = whoCreatedIORef $ _fanInt_subscriptionRef self
1661{-# INLINABLE getFanSubscribed #-}
1706{-# INLINABLE getFanSubscribed #-}
1662getFanSubscribed :: (HasSpiderTimeline x, GCompare k) => k a -> Fan x k v -> Subscriber x (v a) -> EventM x (WeakBagTicket, FanSubscribed x k v, Maybe (v a))
1707getFanSubscribed :: (HasSpiderTimeline x, GCompare k) => k a -> Fan x k v -> Subscriber x (v a) -> EventM x (WeakBagTicket, FanSubscribed x k v, Maybe (v a))
@@ -1869,7 +1914,7 @@ mergeCheap nt = mergeGCheap' unMergeSubscribedParent getInitialSubscribers updat
1869 liftIO $ modifyIORef' heightBagRef $ heightBagAdd newParentHeight
1914 liftIO $ modifyIORef' heightBagRef $ heightBagAdd newParentHeight
1870 return $ DMap.insertLookupWithKey' (\_ new _ -> new) k newParent ps
1915 return $ DMap.insertLookupWithKey' (\_ new _ -> new) k newParent ps
1871 forM_ mOldSubd $ \oldSubd -> do
1916 forM_ mOldSubd $ \oldSubd -> do
1872− oldHeight <- liftIO $ getEventSubscribedHeight $
1917+ oldHeight <- liftIO $ getEventSubscribedHeight $
1873 _eventSubscription_subscribed $ unMergeSubscribedParent oldSubd
1918 _eventSubscription_subscribed $ unMergeSubscribedParent oldSubd
1875 liftIO $ modifyIORef heightBagRef $ heightBagRemove oldHeight
1920 liftIO $ modifyIORef heightBagRef $ heightBagRemove oldHeight
@@ -1915,7 +1960,7 @@ mergeCheapWithMove nt = mergeGCheap' _mergeSubscribedParentWithMove_subscription
1915 let moveOrDelete :: forall a. k a -> PatchDMapWithMove.NodeInfo k q a -> MergeSubscribedParentWithMove x k a -> Constant (EventM x (Maybe (EventSubscription x))) a
1960 let moveOrDelete :: forall a. k a -> PatchDMapWithMove.NodeInfo k q a -> MergeSubscribedParentWithMove x k a -> Constant (EventM x (Maybe (EventSubscription x))) a
1916 moveOrDelete _ ni parent = Constant $ case getComposeMaybe $ PatchDMapWithMove._nodeInfo_to ni of
1961 moveOrDelete _ ni parent = Constant $ case getComposeMaybe $ PatchDMapWithMove._nodeInfo_to ni of
1917 Nothing -> do
1962 Nothing -> do
1918− oldHeight <- liftIO $ getEventSubscribedHeight $ _eventSubscription_subscribed $
1963+ oldHeight <- liftIO $ getEventSubscribedHeight $ _eventSubscription_subscribed $
1919 _mergeSubscribedParentWithMove_subscription parent
1964 _mergeSubscribedParentWithMove_subscription parent
1921 liftIO $ modifyIORef heightBagRef $ heightBagRemove oldHeight
1966 liftIO $ modifyIORef heightBagRef $ heightBagRemove oldHeight
@@ -1923,7 +1968,7 @@ mergeCheapWithMove nt = mergeGCheap' _mergeSubscribedParentWithMove_subscription
1923 Just toKey -> do
1968 Just toKey -> do
1924 liftIO $ writeIORef (_mergeSubscribedParentWithMove_key parent) $! toKey
1969 liftIO $ writeIORef (_mergeSubscribedParentWithMove_key parent) $! toKey
1925 return Nothing
1970 return Nothing
1926− toDelete <- fmap catMaybes $ mapM (\(_ :=> v) -> getConstant v) $ DMap.toList $
1971+ toDelete <- fmap catMaybes $ mapM (\(_ :=> v) -> getConstant v) $ DMap.toList $
1927 DMap.intersectionWithKey moveOrDelete (unPatchDMapWithMove p) oldParents
1972 DMap.intersectionWithKey moveOrDelete (unPatchDMapWithMove p) oldParents
1929 return (toDelete, applyAlways p' oldParents)
1974 return (toDelete, applyAlways p' oldParents)
@@ -1976,16 +2021,16 @@ invalidateMergeHeight' :: IORef Height -> Subscriber x a -> IO ()
1976invalidateMergeHeight' heightRef sub = do
2021invalidateMergeHeight' heightRef sub = do
1977 oldHeight <- readIORef heightRef
2022 oldHeight <- readIORef heightRef
1978 -- If the height used to be valid, it must be invalid now; we should never have *more* heights than we have parents
2023 -- If the height used to be valid, it must be invalid now; we should never have *more* heights than we have parents
1979− when (oldHeight /= invalidHeight) $ do
2024+ when (oldHeight /= invalidHeight) $ do
1980 writeIORef heightRef $! invalidHeight
2025 writeIORef heightRef $! invalidHeight
1981 subscriberInvalidateHeight sub oldHeight
2026 subscriberInvalidateHeight sub oldHeight
1983revalidateMergeHeight :: Merge x k v s -> IO ()
2028revalidateMergeHeight :: Merge x k v s -> IO ()
1984revalidateMergeHeight m = do
2029revalidateMergeHeight m = do
1985 currentHeight <- readIORef $ _merge_heightRef m
2030 currentHeight <- readIORef $ _merge_heightRef m
1987 -- revalidateMergeHeight may be called multiple times; perhaps the's a way to finesse it to avoid this check
2032 -- revalidateMergeHeight may be called multiple times; perhaps the's a way to finesse it to avoid this check
1988− when (currentHeight == invalidHeight) $ do
2033+ when (currentHeight == invalidHeight) $ do
1989 heights <- readIORef $ _merge_heightBagRef m
2034 heights <- readIORef $ _merge_heightBagRef m
1990 parents <- readIORef $ _merge_parentsRef m
2035 parents <- readIORef $ _merge_parentsRef m
1991 -- When the number of heights in the bag reaches the number of parents, we should have a valid height
2036 -- When the number of heights in the bag reaches the number of parents, we should have a valid height
@@ -1993,7 +2038,7 @@ revalidateMergeHeight m = do
1993 LT -> return ()
2038 LT -> return ()
1994 EQ -> do
2039 EQ -> do
1995 let height = succHeight $ heightBagMax heights
2040 let height = succHeight $ heightBagMax heights
1996− traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
2041+ traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
1997 writeIORef (_merge_heightRef m) $! height
2042 writeIORef (_merge_heightRef m) $! height
1998 subscriberRecalculateHeight (_merge_sub m) height
2043 subscriberRecalculateHeight (_merge_sub m) height
1999 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"
@@ -2001,22 +2046,22 @@ revalidateMergeHeight m = do
2001scheduleMergeSelf :: HasSpiderTimeline x => Merge x k v s -> Height -> EventM x ()
2046scheduleMergeSelf :: HasSpiderTimeline x => Merge x k v s -> Height -> EventM x ()
2002scheduleMergeSelf m height = scheduleMerge' height (_merge_heightRef m) $ do
2047scheduleMergeSelf m height = scheduleMerge' height (_merge_heightRef m) $ do
2003 vals <- liftIO $ readIORef $ _merge_accumRef m
2048 vals <- liftIO $ readIORef $ _merge_accumRef m
2004− -- Once we're done with this, we can clear it immediately, because if there's a cacheEvent in front of us,
2049+ -- Once we're done with this, we can clear it immediately, because if there's a cacheEvent in front of us,
2005 -- it'll handle subsequent subscribers, and if not, we won't get subsequent subscribers
2050 -- it'll handle subsequent subscribers, and if not, we won't get subsequent subscribers
2006− liftIO $ writeIORef (_merge_accumRef m) $! DMap.empty
2051+ liftIO $ writeIORef (_merge_accumRef m) $! DMap.empty
2007 --TODO: Assert that m is not empty
2052 --TODO: Assert that m is not empty
2008 subscriberPropagate (_merge_sub m) vals
2053 subscriberPropagate (_merge_sub m) vals
2010checkCycle :: HasSpiderTimeline x => EventSubscribed x -> EventM x ()
2055checkCycle :: HasSpiderTimeline x => EventSubscribed x -> EventM x ()
2011checkCycle subscribed = liftIO $ do
2056checkCycle subscribed = liftIO $ do
2012 height <- readIORef (eventSubscribedHeightRef subscribed)
2057 height <- readIORef (eventSubscribedHeightRef subscribed)
2014 -- currentHeight <- getCurrentHeight
2059 -- currentHeight <- getCurrentHeight
2015 -- when (height <= currentHeight) $ if height /= invalidHeight
2060 -- when (height <= currentHeight) $ if height /= invalidHeight
2016 -- then do
2061 -- then do
2017 -- myStack <- liftIO $ whoCreatedIORef undefined --TODO
2062 -- myStack <- liftIO $ whoCreatedIORef undefined --TODO
2018 -- error $ "Height (" ++ show height ++ ") is not greater than current height (" ++ show currentHeight ++ ")\n" ++ unlines (reverse myStack)
2063 -- error $ "Height (" ++ show height ++ ") is not greater than current height (" ++ show currentHeight ++ ")\n" ++ unlines (reverse myStack)
2019− -- else liftIO $
2064+ -- else liftIO $
2020 when (height == invalidHeight) $
2065 when (height == invalidHeight) $
2021#ifdef DEBUG_CYCLES
2066#ifdef DEBUG_CYCLES
@@ -2069,17 +2114,17 @@ mergeGCheap' getParent getInitialSubscribers updateFunc destroy d = Event $ \sub
2069 parentsRef :: IORef (DMap k s) <- liftIO $ newIORef $ error "merge: parentsRef not yet initialized"
2114 parentsRef :: IORef (DMap k s) <- liftIO $ newIORef $ error "merge: parentsRef not yet initialized"
2070 changeSubdRef <- liftIO $ newIORef $ error "getMergeSubscribed: changeSubdRef not yet initialized"
2115 changeSubdRef <- liftIO $ newIORef $ error "getMergeSubscribed: changeSubdRef not yet initialized"
2072− let subscribed = EventSubscribed
2117+ let subscribed = EventSubscribed
2073− { eventSubscribedHeightRef = heightRef
2118+ { eventSubscribedHeightRef = heightRef
2074 , eventSubscribedRetained = toAny (parentsRef, changeSubdRef)
2119 , eventSubscribedRetained = toAny (parentsRef, changeSubdRef)
2075#ifdef DEBUG_CYCLES
2120#ifdef DEBUG_CYCLES
2076 , eventSubscribedGetParents = do
2121 , eventSubscribedGetParents = do
2077 let getParent' (_ :=> v) = _eventSubscription_subscribed (getParent v)
2122 let getParent' (_ :=> v) = _eventSubscription_subscribed (getParent v)
2078− fmap getParent' . DMap.toList <$> readIORef parentsRef
2123+ fmap getParent' . DMap.toList <$> readIORef parentsRef
2079 , eventSubscribedHasOwnHeightRef = False
2124 , eventSubscribedHasOwnHeightRef = False
2080 , eventSubscribedWhoCreated = whoCreatedIORef heightRef
2125 , eventSubscribedWhoCreated = whoCreatedIORef heightRef
2084 m = Merge
2129 m = Merge
2085 { _merge_parentsRef = parentsRef
2130 { _merge_parentsRef = parentsRef
@@ -2117,9 +2162,9 @@ mergeGCheap' getParent getInitialSubscribers updateFunc destroy d = Event $ \sub
2117 -- its subscription.
2162 -- its subscription.
2118 liftIO $ writeIORef changeSubdRef (changeSubscriber, changeSubscription)
2163 liftIO $ writeIORef changeSubdRef (changeSubscriber, changeSubscription)
2119 let unsubscribeAll = destroy =<< readIORef parentsRef
2164 let unsubscribeAll = destroy =<< readIORef parentsRef
2121 return (EventSubscription unsubscribeAll subscribed, occ)
2166 return (EventSubscription unsubscribeAll subscribed, occ)
2124mergeInt :: forall x a. (HasSpiderTimeline x) => DynamicS x (PatchIntMap (Event x a)) -> Event x (IntMap a)
2169mergeInt :: forall x a. (HasSpiderTimeline x) => DynamicS x (PatchIntMap (Event x a)) -> Event x (IntMap a)
2125mergeInt = cacheEvent . mergeIntCheap
2170mergeInt = cacheEvent . mergeIntCheap
@@ -2133,14 +2178,14 @@ mergeIntCheap d = Event $ \sub -> do
2133 heightBagRef <- liftIO $ newIORef heightBagEmpty
2178 heightBagRef <- liftIO $ newIORef heightBagEmpty
2134 parents <- liftIO $ FastMutableIntMap.newEmpty
2179 parents <- liftIO $ FastMutableIntMap.newEmpty
2135 changeSubdRef <- liftIO $ newIORef $ error "getMergeSubscribed: changeSubdRef not yet initialized"
2180 changeSubdRef <- liftIO $ newIORef $ error "getMergeSubscribed: changeSubdRef not yet initialized"
2136− let subscribed = EventSubscribed
2181+ let subscribed = EventSubscribed
2137− { eventSubscribedHeightRef = heightRef
2182+ { eventSubscribedHeightRef = heightRef
2138 , eventSubscribedRetained = toAny (parents, changeSubdRef)
2183 , eventSubscribedRetained = toAny (parents, changeSubdRef)
2139#ifdef DEBUG_CYCLES
2184#ifdef DEBUG_CYCLES
2140 , eventSubscribedGetParents = fmap (_eventSubscription_subscribed . snd) <$> FastMutableIntMap.toList parents
2185 , eventSubscribedGetParents = fmap (_eventSubscription_subscribed . snd) <$> FastMutableIntMap.toList parents
2141 , eventSubscribedHasOwnHeightRef = False
2186 , eventSubscribedHasOwnHeightRef = False
2142 , eventSubscribedWhoCreated = whoCreatedIORef heightRef
2187 , eventSubscribedWhoCreated = whoCreatedIORef heightRef
2145 let scheduleSelf = do
2190 let scheduleSelf = do
2146 height <- liftIO $ readIORef $ heightRef
2191 height <- liftIO $ readIORef $ heightRef
@@ -2157,7 +2202,7 @@ mergeIntCheap d = Event $ \sub -> do
2157 LT -> return ()
2202 LT -> return ()
2158 EQ -> do
2203 EQ -> do
2159 let height = succHeight $ heightBagMax heights
2204 let height = succHeight $ heightBagMax heights
2160− traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
2205+ traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
2161 writeIORef heightRef $! height
2206 writeIORef heightRef $! height
2162 subscriberRecalculateHeight sub height
2207 subscriberRecalculateHeight sub height
2163 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"
@@ -2229,7 +2274,7 @@ mergeIntCheap d = Event $ \sub -> do
2229 -- its subscription.
2274 -- its subscription.
2230 liftIO $ writeIORef changeSubdRef (changeSubscriber, changeSubscription)
2275 liftIO $ writeIORef changeSubdRef (changeSubscriber, changeSubscription)
2231 let unsubscribeAll = traverse_ unsubscribe =<< FastMutableIntMap.getFrozenAndClear parents
2276 let unsubscribeAll = traverse_ unsubscribe =<< FastMutableIntMap.getFrozenAndClear parents
2234 return (EventSubscription unsubscribeAll subscribed, occ)
2279 return (EventSubscription unsubscribeAll subscribed, occ)
@@ -2340,7 +2385,7 @@ runFrame a = SpiderHost $ do
2340 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
2385 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
2342 stackTrace <- liftIO $ fmap renderStack $ ccsToStrings =<< (getCCSOf $! switchSubscribedParent subscribed)
2387 stackTrace <- liftIO $ fmap renderStack $ ccsToStrings =<< (getCCSOf $! switchSubscribedParent subscribed)
2343− liftIO $ putStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
2388+ liftIO $ debugStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
2344 EventRoot _ -> "EventRoot"
2389 EventRoot _ -> "EventRoot"
2345 EventNever -> "EventNever"
2390 EventNever -> "EventNever"
2346 _ -> "something else"
2391 _ -> "something else"
@@ -2475,6 +2520,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Event
2475 {-# INLINABLE headE #-}
2520 {-# INLINABLE headE #-}
2476 headE = R.slowHeadE
2521 headE = R.slowHeadE
2477-- 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
2479instance Reflex.Class.MonadSample (SpiderTimeline x) (SpiderPullM x) where
2526instance Reflex.Class.MonadSample (SpiderTimeline x) (SpiderPullM x) where
2480 {-# INLINABLE sample #-}
2527 {-# INLINABLE sample #-}
@@ -2496,6 +2543,9 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
2496 {-# INLINABLE headE #-}
2543 {-# INLINABLE headE #-}
2497 headE = R.slowHeadE
2544 headE = R.slowHeadE
2498-- 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
2500instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x)) where
2550instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x)) where
2501 {-# INLINE return #-}
2551 {-# INLINE return #-}
@@ -2504,8 +2554,10 @@ instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x))
2504 x >>= f = SpiderDynamic $ dynamicDynIdentity $ newJoinDyn $ newMapDyn (unSpiderDynamic . f) $ unSpiderDynamic x
2554 x >>= f = SpiderDynamic $ dynamicDynIdentity $ newJoinDyn $ newMapDyn (unSpiderDynamic . f) $ unSpiderDynamic x
2505 {-# INLINE (>>) #-}
2555 {-# INLINE (>>) #-}
2506 (>>) = (*>)
2556 (>>) = (*>)
2557+#if !MIN_VERSION_base(4,13,0)
2507 {-# INLINE fail #-}
2558 {-# INLINE fail #-}
2508 fail _ = error "Dynamic does not support 'fail'"
2559 fail _ = error "Dynamic does not support 'fail'"
2510{-# INLINABLE newJoinDyn #-}
2562{-# INLINABLE newJoinDyn #-}
2511newJoinDyn :: HasSpiderTimeline x => DynamicS x (Identity (DynamicS x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
2563newJoinDyn :: HasSpiderTimeline x => DynamicS x (Identity (DynamicS x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
@@ -2557,6 +2609,9 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
2557 buildDynamic getV0 e = runFrame . runSpiderHostFrame $ Reflex.Class.buildDynamic getV0 e
2609 buildDynamic getV0 e = runFrame . runSpiderHostFrame $ Reflex.Class.buildDynamic getV0 e
2558 {-# INLINABLE headE #-}
2610 {-# INLINABLE headE #-}
2559 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
2561instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHostFrame x) where
2616instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHostFrame x) where
2562 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
@@ -2573,6 +2628,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
2573 {-# INLINABLE headE #-}
2628 {-# INLINABLE headE #-}
2574 headE = R.slowHeadE
2629 headE = R.slowHeadE
2575-- 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
2577instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHost x) where
2634instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHost x) where
2578 {-# INLINABLE sample #-}
2635 {-# INLINABLE sample #-}
@@ -2593,6 +2650,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Refle
2593 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
2594 {-# INLINABLE headE #-}
2651 {-# INLINABLE headE #-}
2595 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
2597--------------------------------------------------------------------------------
2656--------------------------------------------------------------------------------
2598-- Deprecated items
2657-- Deprecated items
@@ -2716,7 +2775,7 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2716 fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
2775 fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
2717 {-# INLINABLE mergeG #-}
2776 {-# INLINABLE mergeG #-}
2719− :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2778+ :: forall k2 (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2720 => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
2779 => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
2721 -> DMap k q
2780 -> DMap k q
2722 -> R.Event (SpiderTimeline x) (DMap k v)
2781 -> R.Event (SpiderTimeline x) (DMap k v)
@@ -2784,8 +2843,14 @@ instance Monad (SpiderHost x) where
2784 SpiderHost x >> SpiderHost y = SpiderHost $ x >> y
2843 SpiderHost x >> SpiderHost y = SpiderHost $ x >> y
2785 {-# INLINABLE return #-}
2844 {-# INLINABLE return #-}
2786 return x = SpiderHost $ return x
2845 return x = SpiderHost $ return x
2846+#if !MIN_VERSION_base(4,13,0)
2787 {-# INLINABLE fail #-}
2847 {-# INLINABLE fail #-}
2848+ fail = MonadFail.fail
2851+instance MonadFail (SpiderHost x) where
2852+ {-# INLINABLE fail #-}
2788− fail s = SpiderHost $ fail s
2853+ fail s = SpiderHost $ MonadFail.fail s
2790-- | Run an action affecting the global Spider timeline; this will be guarded by
2855-- | Run an action affecting the global Spider timeline; this will be guarded by
2791-- a mutex for that timeline
2856-- a mutex for that timeline
@@ -2807,8 +2872,10 @@ instance Monad (SpiderHostFrame x) where
2807 SpiderHostFrame x >> SpiderHostFrame y = SpiderHostFrame $ x >> y
2872 SpiderHostFrame x >> SpiderHostFrame y = SpiderHostFrame $ x >> y
2808 {-# INLINABLE return #-}
2873 {-# INLINABLE return #-}
2809 return x = SpiderHostFrame $ return x
2874 return x = SpiderHostFrame $ return x
2875+#if !MIN_VERSION_base(4,13,0)
2810 {-# INLINABLE fail #-}
2876 {-# INLINABLE fail #-}
2811 fail s = SpiderHostFrame $ fail s
2877 fail s = SpiderHostFrame $ fail s
2813instance NotReady (SpiderTimeline x) (SpiderHostFrame x) where
2880instance NotReady (SpiderTimeline x) (SpiderHostFrame x) where
2814 notReadyUntil _ = pure ()
2881 notReadyUntil _ = pure ()