@@ -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,7 @@
1920{-# LANGUAGE DeriveFunctor #-}
2021{-# LANGUAGE StandaloneDeriving #-}
2122{-# LANGUAGE InstanceSigs #-}
22−{-# LANGUAGE MultiWayIf #-}
23+{-# LANGUAGE RecursiveDo #-}
2324
2425#ifdef USE_REFLEX_OPTIMIZER
2526{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
@@ -38,17 +39,15 @@ import Control.Monad hiding (forM, forM_, mapM, mapM_)
3839import Control.Monad.Exception
3940import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
4041import Control.Monad.Primitive
42+--import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_)
4143import Control.Monad.Reader.Class
4244import Control.Monad.IO.Class
4345import Control.Monad.ReaderIO
4446import Control.Monad.Ref
45−import Control.Monad.Fail (MonadFail)
46−import qualified Control.Monad.Fail as MonadFail
4747import Data.Align
4848import Data.Coerce
49−import Data.Dependent.Map (DMap)
49+import Data.Dependent.Map (DMap, DSum (..))
5050import qualified Data.Dependent.Map as DMap
51−import Data.Dependent.Sum (DSum (..))
5251import Data.FastMutableIntMap (FastMutableIntMap, PatchIntMap (..))
5352import qualified Data.FastMutableIntMap as FastMutableIntMap
5453import Data.Foldable hiding (concat, elem, sequence_)
@@ -65,7 +64,6 @@ import Data.Monoid (mempty, (<>))
6564import Data.Proxy
6665import Data.These
6766import Data.Traversable
68−import Data.Type.Equality ((:~:)(Refl))
6967import Data.Witherable (Filterable, mapMaybe)
7068import GHC.Exts hiding (toList)
7169import GHC.IORef (IORef (..))
@@ -86,9 +84,9 @@ import Data.Zip (Zip (..))
8684
8785#ifdef DEBUG_CYCLES
8886import Control.Monad.State hiding (forM, forM_, mapM, mapM_, sequence)
87+import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
8988#endif
9089
91−import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
9290import qualified Data.List.NonEmpty as NonEmpty
9391import Data.Tree (Forest, Tree (..), drawForest)
9492import Data.List (isPrefixOf)
@@ -106,21 +104,15 @@ import qualified Reflex.Class
106104import qualified Reflex.Class as R
107105import qualified Reflex.Host.Class
108106import Reflex.NotReady.Class
109−import Data.Patch
110−import qualified Data.Patch.DMapWithMove as PatchDMapWithMove
107+import Reflex.Patch
108+import qualified Reflex.Patch.DMapWithMove as PatchDMapWithMove
111109import Reflex.PerformEvent.Base (PerformEventT)
112110
113111#ifdef DEBUG_TRACE_EVENTS
114112import qualified Data.ByteString.Char8 as BS8
115113import System.IO (stderr)
116114import Data.List (isPrefixOf)
117−#endif
118115
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
124116withStackOneLine :: (BS8.ByteString -> a) -> a
125117withStackOneLine expr = unsafePerformIO $ do
126118 stack <- currentCallStack
@@ -129,39 +121,15 @@ withStackOneLine expr = unsafePerformIO $ do
129121
130122#endif
131123
132−debugPropagate :: Bool
133−
134−debugInvalidateHeight :: Bool
135−
136−debugInvalidate :: Bool
137−
138124#ifdef DEBUG
139125#define DEBUG_NODEIDS
140126
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−
159127class HasNodeId a where
160128 getNodeId :: a -> Int
161129
162130instance HasNodeId (CacheSubscribed x a) where
163131 getNodeId = _cacheSubscribed_nodeId
164−
132+
165133instance HasNodeId (FanInt x a) where
166134 getNodeId = _fanInt_nodeId
167135
@@ -188,24 +156,16 @@ showNodeId :: HasNodeId a => a -> String
188156showNodeId = showNodeId' . getNodeId
189157
190158showNodeId' :: Int -> String
191−showNodeId' = ("#"<>) . show
159+showNodeId' = ("#"<>) . show
192160
193161
194162#else
195163
196−debugPropagate = False
197−debugInvalidateHeight = False
198−debugInvalidate = False
199−
200164-- This must be inline, or error messages will cause memory leaks due to retaining the node in question
201165{-# INLINE showNodeId #-}
202166showNodeId :: a -> String
203167showNodeId _ = ""
204168
205−{-# INLINE showNodeId' #-}
206−showNodeId' :: Int -> String
207−showNodeId' _ = ""
208−
209169#endif
210170
211171#ifdef DEBUG_NODEIDS
@@ -253,11 +213,11 @@ subscribeAndRead = unEvent
253213{-# INLINE [1] pushCheap #-}
254214pushCheap :: HasSpiderTimeline x => (a -> ComputeM x (Maybe b)) -> Event x a -> Event x b
255215pushCheap !f e = Event $ \sub -> do
256− (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" $ sub
216+ (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" (sub
257217 { subscriberPropagate = \a -> do
258218 mb <- f a
259219 mapM_ (subscriberPropagate sub) mb
260− }
220+ })
261221 occ' <- join <$> mapM f occ
262222 return (subscription, occ')
263223
@@ -293,9 +253,9 @@ headE originalE = do
293253 let clearParent = liftIO $ writeIORef parent Nothing
294254 (_, occ) <- subscribeAndReadHead originalE $ terminalSubscriber $ const clearParent
295255 when (isJust occ) clearParent
296− return $ Event $ \sub ->
256+ return $ Event $ \sub ->
297257 liftIO (readIORef parent) >>= \case
298− Nothing -> subscribeAndReadNever
258+ Nothing -> subscribeAndReadNever
299259 Just e -> subscribeAndReadHead e sub
300260
301261data CacheSubscribed x a
@@ -307,20 +267,7 @@ data CacheSubscribed x a
307267#endif
308268 }
309269
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− )
270+
324271
325272-- | Construct an 'Event' whose value is guaranteed not to be recomputed
326273-- repeatedly
@@ -351,18 +298,39 @@ cacheEvent e =
351298#endif
352299 subscribers <- liftIO FastWeakBag.empty
353300 occRef <- liftIO $ newIORef Nothing -- This should never be read prior to being set below
354−#ifdef DEBUG_NODEIDS
301+<<<<<<< HEAD
355302 (parentSub, occ) <- subscribeAndRead e $ debugSubscriber' ("cacheEvent" <> showNodeId' nodeId) $ Subscriber
356−#else
357− (parentSub, occ) <- subscribeAndRead e $ Subscriber
358−#endif
359303 { subscriberPropagate = \a -> do
304+ -- checkCycle (_eventSubscription_subscribed parentSub)
360305 liftIO $ writeIORef occRef (Just a)
361306 scheduleClear occRef
362307 propagateFast a subscribers
363− , subscriberInvalidateHeight = FastWeakBag.traverse_ subscribers . invalidateSubscriberHeight
364− , subscriberRecalculateHeight = FastWeakBag.traverse_ subscribers . recalculateSubscriberHeight
308+ , subscriberInvalidateHeight = FastWeakBag.traverse subscribers . invalidateSubscriberHeight
309+ , subscriberRecalculateHeight = FastWeakBag.traverse subscribers . recalculateSubscriberHeight
365310 }
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
321+ }
322+=======
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
332+ }
333+>>>>>>> develop
366334 when (isJust occ) $ do
367335 liftIO $ writeIORef occRef occ -- Set the initial value of occRef; we don't need to do this if occ is Nothing
368336 scheduleClear occRef
@@ -372,20 +340,20 @@ cacheEvent e =
372340 , _cacheSubscribed_occurrence = occRef
373341#ifdef DEBUG_NODEIDS
374342 , _cacheSubscribed_nodeId = nodeId
375−#endif
343+#endif
376344 }
377345 subscribedTicket <- liftIO $ mkFastWeakTicket subscribed
378346 liftIO $ writeIORef mSubscribedRef =<< getFastWeakTicketWeak subscribedTicket
379347 return subscribedTicket
380348 liftIO $ cacheSubscription sub mSubscribedRef subscribedTicket
381349
382−cacheSubscription :: Subscriber x a -> IORef (FastWeak (CacheSubscribed x a))
350+cacheSubscription :: Subscriber x a -> IORef (FastWeak (CacheSubscribed x a))
383351 -> FastWeakTicket (CacheSubscribed x a) -> IO (EventSubscription x, Maybe a)
384−cacheSubscription sub mSubscribedRef subscribedTicket = do
352+cacheSubscription sub mSubscribedRef subscribedTicket = do
385353 subscribed <- getFastWeakTicketValue subscribedTicket
386354 ticket <- FastWeakBag.insert sub $ _cacheSubscribed_subscribers subscribed
387355 occ <- readIORef $ _cacheSubscribed_occurrence subscribed
388−
356+
389357 let parentSub = _cacheSubscribed_parent subscribed
390358 es = EventSubscription
391359 { _eventSubscription_unsubscribe = do
@@ -404,9 +372,9 @@ cacheSubscription sub mSubscribedRef subscribedTicket = do
404372 , eventSubscribedGetParents = return [_eventSubscription_subscribed parentSub]
405373 , eventSubscribedHasOwnHeightRef = False
406374 , eventSubscribedWhoCreated = whoCreatedIORef mSubscribedRef
407−#endif
375+#endif
408376 }
409− }
377+ }
410378 return (es, occ)
411379
412380
@@ -464,6 +432,16 @@ data Subscriber x a = Subscriber
464432 , subscriberRecalculateHeight :: !(Height -> IO ())
465433 }
466434
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.
441+--
442+-- The result is also evaluated to WHNF, since forcing a thunk invalidates
443+-- the weak pointer to it in some cases.
444+
467445newSubscriberHold :: (HasSpiderTimeline x, Patch p) => Hold x p -> IO (Subscriber x p)
468446newSubscriberHold h = return $ Subscriber
469447 { subscriberPropagate = {-# SCC "traverseHold" #-} propagateSubscriberHold h
@@ -517,7 +495,7 @@ newSubscriberCoincidenceOuter subscribed = debugSubscriber ("SubscriberCoinciden
517495 liftIO $ writeIORef (coincidenceSubscribedInnerParent subscribed) $ Just innerSubd
518496 scheduleClear $ coincidenceSubscribedInnerParent subscribed
519497 case occ of
520− Nothing ->
498+ Nothing ->
521499 when (innerHeight > outerHeight) $ liftIO $ do -- If the event fires, it will fire at a later height
522500 writeIORef (coincidenceSubscribedHeight subscribed) $! innerHeight
523501 WeakBag.traverse_ (coincidenceSubscribedSubscribers subscribed) $ invalidateSubscriberHeight outerHeight
@@ -527,7 +505,7 @@ newSubscriberCoincidenceOuter subscribed = debugSubscriber ("SubscriberCoinciden
527505 scheduleClear $ coincidenceSubscribedOccurrence subscribed
528506 propagate o $ coincidenceSubscribedSubscribers subscribed
529507 , subscriberInvalidateHeight = \_ -> invalidateCoincidenceHeight subscribed
530− , subscriberRecalculateHeight = \_ -> recalculateCoincidenceHeight subscribed
508+ , subscriberRecalculateHeight = \_ -> recalculateCoincidenceHeight subscribed
531509 }
532510
533511newSubscriberCoincidenceInner :: forall x a. HasSpiderTimeline x => CoincidenceSubscribed x a -> IO (Subscriber x a)
@@ -548,18 +526,18 @@ invalidateSubscriberHeight :: Height -> Subscriber x a -> IO ()
548526invalidateSubscriberHeight = flip subscriberInvalidateHeight
549527
550528recalculateSubscriberHeight :: Height -> Subscriber x a -> IO ()
551−recalculateSubscriberHeight = flip subscriberRecalculateHeight
529+recalculateSubscriberHeight = flip subscriberRecalculateHeight
552530
553531-- | Propagate everything at the current height
554532propagate :: forall x a. HasSpiderTimeline x => a -> WeakBag (Subscriber x a) -> EventM x ()
555−propagate a subscribers = withIncreasedDepth (Proxy::Proxy x) $
533+propagate a subscribers = withIncreasedDepth (Proxy::Proxy x) $
556534 -- 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
557535 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
558536 WeakBag.traverse_ subscribers $ \s -> subscriberPropagate s a
559537
560538-- | Propagate everything at the current height
561539propagateFast :: forall x a. HasSpiderTimeline x => a -> FastWeakBag (Subscriber x a) -> EventM x ()
562−propagateFast a subscribers = withIncreasedDepth (Proxy::Proxy x) $
540+propagateFast a subscribers = withIncreasedDepth (Proxy::Proxy x) $
563541 -- 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
564542 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
565543 FastWeakBag.traverse_ subscribers $ \s -> subscriberPropagate s a
@@ -607,16 +585,6 @@ eventSubscribedNever = EventSubscribed
607585 , eventSubscribedWhoCreated = return ["never"]
608586#endif
609587 }
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− }
620588
621589eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
622590eventSubscribedFan !subscribed = EventSubscribed
@@ -994,10 +962,8 @@ instance Monad (BehaviorM x) where
994962 BehaviorM x >> BehaviorM y = BehaviorM $ x >> y
995963 {-# INLINE return #-}
996964 return x = BehaviorM $ return x
997−#if !MIN_VERSION_base(4,13,0)
998965 {-# INLINE fail #-}
999966 fail s = BehaviorM $ fail s
1000−#endif
1001967
1002968data BehaviorSubscribed x a
1003969 = forall p. BehaviorSubscribedHold (Hold x p)
@@ -1057,7 +1023,7 @@ data SomeMergeUpdate x = SomeMergeUpdate
10571023newtype SomeMergeInit x = SomeMergeInit { unSomeMergeInit :: EventM x () }
10581024
10591025-- EventM can do everything BehaviorM can, plus create holds
1060−newtype EventM x a = EventM { unEventM :: IO a } deriving (Functor, Applicative, Monad, MonadIO, MonadFix, MonadException, MonadAsyncException)
1026+newtype EventM x a = EventM { unEventM :: IO a } deriving (Functor, Applicative, Monad, MonadIO, MonadFix, MonadException, MonadAsyncException)
10611027
10621028newtype MergeSubscribedParent x a = MergeSubscribedParent { unMergeSubscribedParent :: EventSubscription x }
10631029
@@ -1082,7 +1048,7 @@ heightBagFromList :: [Height] -> HeightBag
10821048heightBagFromList heights = heightBagVerify $ foldl' (flip heightBagAdd) heightBagEmpty heights
10831049
10841050heightBagAdd :: Height -> HeightBag -> HeightBag
1085−heightBagAdd (Height h) (HeightBag s c) = heightBagVerify $ HeightBag (succ s) $
1051+heightBagAdd (Height h) (HeightBag s c) = heightBagVerify $ HeightBag (succ s) $
10861052 IntMap.insertWithKey (\_ _ old -> succ old) h 0 c
10871053
10881054heightBagRemove :: Height -> HeightBag -> HeightBag
@@ -1093,11 +1059,11 @@ heightBagRemove (Height h) b@(HeightBag s c) = heightBagVerify $ case IntMap.loo
10931059 _ -> IntMap.insert h (pred old) c
10941060
10951061heightBagRemoveMaybe :: Height -> HeightBag -> Maybe HeightBag
1096−heightBagRemoveMaybe (Height h) b@(HeightBag s c) = heightBagVerify . removed <$> IntMap.lookup h c where
1062+heightBagRemoveMaybe (Height h) b@(HeightBag s c) = heightBagVerify . removed <$> IntMap.lookup h c where
10971063 removed old = HeightBag (pred s) $ case old of
10981064 0 -> IntMap.delete h c
10991065 _ -> IntMap.insert h (pred old) c
1100−
1066+
11011067
11021068heightBagMax :: HeightBag -> Height
11031069heightBagMax (HeightBag _ c) = case IntMap.maxViewWithKey c of
@@ -1346,7 +1312,7 @@ mkWeakPtrWithDebug x debugNote = do
13461312 x' <- evaluate x
13471313 mkWeakPtr x' $
13481314 if debugFinalize
1349− then Just $ debugStrLn $ "finalizing: " ++ debugNote
1315+ then Just $ putStrLn $ "finalizing: " ++ debugNote
13501316 else Nothing
13511317
13521318type WeakList a = [Weak a]
@@ -1362,19 +1328,19 @@ debugSubscriber :: forall x a. HasSpiderTimeline x => String -> Subscriber x a -
13621328debugSubscriber description = return . debugSubscriber' description
13631329
13641330debugSubscriber' :: forall x a. HasSpiderTimeline x => String -> Subscriber x a -> Subscriber x a
1365−debugSubscriber' description subscribed = Subscriber
1331+debugSubscriber' description subscribed = Subscriber
13661332 {
13671333 subscriberPropagate = \m -> do
13681334 tracePropagate (Proxy :: Proxy x) ("subscriberPropagate: " <> description)
13691335 subscriberPropagate subscribed m
13701336 , subscriberInvalidateHeight = \old -> do
1371− traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
1337+ putStrLn $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
13721338 subscriberInvalidateHeight subscribed old
1373− traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", done"
1339+ putStrLn $ "invalidateSubscriberHeight: " <> description <> ", done"
13741340 , subscriberRecalculateHeight = \new -> do
1375− traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
1341+ putStrLn $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
13761342 subscriberRecalculateHeight subscribed new
1377− traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", done"
1343+ putStrLn $ "subscriberRecalculateHeight: " <> description <> ", done"
13781344 }
13791345
13801346
@@ -1388,15 +1354,11 @@ withIncreasedDepth _ a = do
13881354
13891355{-# INLINE tracePropagate #-}
13901356tracePropagate :: (CanTrace x m) => proxy x -> String -> m ()
1391−tracePropagate p = when debugPropagate . trace p
1357+tracePropagate p = trace p
13921358
13931359{-# INLINE traceInvalidate #-}
13941360traceInvalidate :: String -> IO ()
1395−traceInvalidate = when debugInvalidate . liftIO . debugStrLn
1396−
1397−{-# INLINE traceInvalidateHeight #-}
1398−traceInvalidateHeight :: String -> IO ()
1399−traceInvalidateHeight = when debugInvalidateHeight . liftIO . debugStrLn
1361+traceInvalidate = liftIO . putStrLn
14001362
14011363{-# INLINE trace #-}
14021364trace :: (CanTrace x m) => proxy x -> String -> m ()
@@ -1407,7 +1369,7 @@ traceM :: forall x proxy m. (CanTrace x m) => proxy x -> m String -> m ()
14071369traceM _ getMessage = do
14081370 message <- getMessage
14091371 d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
1410− liftIO $ debugStrLn $ replicate d ' ' <> message
1372+ liftIO $ putStrLn $ replicate d ' ' <> message
14111373
14121374#else
14131375
@@ -1423,10 +1385,6 @@ tracePropagate _ _ = return ()
14231385traceInvalidate :: String -> IO ()
14241386traceInvalidate _ = return ()
14251387
1426−{-# INLINE traceInvalidateHeight #-}
1427−traceInvalidateHeight :: String -> IO ()
1428−traceInvalidateHeight _ = return ()
1429−
14301388{-# INLINE debugSubscriber #-}
14311389debugSubscriber :: String -> Subscriber x a -> IO (Subscriber x a)
14321390debugSubscriber _ = return
@@ -1467,20 +1425,17 @@ showStacks :: [[String]] -> String
14671425showStacks = drawForest . listsToForest . fmap (filterStack "Reflex.Spider.Internal")
14681426
14691427filterStack :: String -> [String] -> [String]
1470−#ifdef DEBUG_HIDE_INTERNALS
14711428filterStack prefix = filter (not . (prefix `isPrefixOf`))
1472−#else
1473−filterStack prefix = id
1474−#endif
1429+
14751430
14761431#ifdef DEBUG_CYCLES
14771432
1478−data EventLoopException = EventLoopException [[String]]
1433+data EventLoopException = EventLoopException [[String]]
14791434instance Exception EventLoopException
14801435
14811436instance Show EventLoopException where
1482− show (EventLoopException stacks) = "causality loop detected:\n" <> if null stacks
1483− then "no location information, compile with profiling enabled for stack tree"
1437+ show (EventLoopException stacks) = "causality loop detected:\n" <> if null stacks
1438+ then "no location information, compile with profiling enabled for stack tree"
14841439 else showStacks stacks
14851440
14861441#else
@@ -1498,7 +1453,7 @@ instance Show EventLoopException where
14981453{-# INLINE propagateSubscriberHold #-}
14991454propagateSubscriberHold :: forall x p. (HasSpiderTimeline x, Patch p) => Hold x p -> p -> EventM x ()
15001455propagateSubscriberHold h a = do
1501− {-# SCC "trace" #-} when debugPropagate $ traceM (Proxy :: Proxy x) $ liftIO $ do
1456+ traceM (Proxy :: Proxy x) $ liftIO $ do
15021457 invalidators <- liftIO $ readIORef $ holdInvalidators h
15031458 return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
15041459
@@ -1506,7 +1461,7 @@ propagateSubscriberHold h a = do
15061461 case {-# SCC "apply" #-} apply a v of
15071462 Nothing -> return ()
15081463 Just v' -> do
1509− {-# SCC "trace2" #-} withIncreasedDepth (Proxy :: Proxy x) $
1464+ withIncreasedDepth (Proxy :: Proxy x) $
15101465 tracePropagate (Proxy :: Proxy x) ("propagateSubscriberHold: assigning Hold" <> showNodeId h)
15111466 vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
15121467 iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
@@ -1627,7 +1582,7 @@ data FanInt x a = FanInt
16271582 { _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
16281583 , _fanInt_subscriptionRef :: {-# UNPACK #-} !(IORef (EventSubscription x)) -- This should have a valid subscription iff subscribers is non-empty
16291584 , _fanInt_occRef :: {-# UNPACK #-} !(IORef (IntMap a))
1630−#ifdef DEBUG_NODEIDS
1585+#ifdef DEBUG_NODEIDS
16311586 , _fanInt_nodeId :: {-# UNPACK #-} !Int
16321587#endif
16331588 }
@@ -1637,15 +1592,15 @@ newFanInt = do
16371592 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
16381593 subscriptionRef <- newIORef $ error "fanInt: no subscription"
16391594 occRef <- newIORef $ error "fanInt: no occurrence"
1640−#ifdef DEBUG_NODEIDS
1595+#ifdef DEBUG_NODEIDS
16411596 nodeId <- newNodeId
16421597#endif
16431598 return $ FanInt
16441599 { _fanInt_subscribers = subscribers
16451600 , _fanInt_subscriptionRef = subscriptionRef
16461601 , _fanInt_occRef = occRef
1647−#ifdef DEBUG_NODEIDS
1648− , _fanInt_nodeId = nodeId
1602+#ifdef DEBUG_NODEIDS
1603+ , _fanInt_nodeId = nodeId
16491604#endif
16501605 }
16511606
@@ -1661,15 +1616,15 @@ fanInt p = unsafePerformIO $ do
16611616 liftIO $ writeIORef (_fanInt_occRef self) m
16621617 scheduleIntClear $ _fanInt_occRef self
16631618 FastMutableIntMap.forIntersectionWithImmutable_ (_fanInt_subscribers self) m $ \b v -> --TODO: Do we need to know that no subscribers are being added as we traverse?
1664− FastWeakBag.traverse_ b $ \s ->
1619+ FastWeakBag.traverse_ b $ \s ->
16651620 subscriberPropagate s v
1666− , subscriberInvalidateHeight = \old ->
1667− FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1668− FastWeakBag.traverse_ b $ \s ->
1621+ , subscriberInvalidateHeight = \old ->
1622+ FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1623+ FastWeakBag.traverse_ b $ \s ->
16691624 subscriberInvalidateHeight s old
16701625 , subscriberRecalculateHeight = \new ->
1671− FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1672− FastWeakBag.traverse_ b $ \s ->
1626+ FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1627+ FastWeakBag.traverse_ b $ \s ->
16731628 subscriberRecalculateHeight s new
16741629 }
16751630 liftIO $ do
@@ -1685,14 +1640,14 @@ fanInt p = unsafePerformIO $ do
16851640 Just b -> return b
16861641 ticket <- liftIO $ FastWeakBag.insert sub b
16871642 currentOcc <- readIORef (_fanInt_occRef self)
1688−
1643+
16891644 subscribed <- fanIntSubscribed ticket self
16901645 return (EventSubscription (FastWeakBag.remove ticket) subscribed, IntMap.lookup k currentOcc)
16911646
16921647fanIntSubscribed :: FastWeakBagTicket k -> FanInt x a -> IO (EventSubscribed x)
1693−fanIntSubscribed ticket self = do
1648+fanIntSubscribed ticket self = do
16941649 subscribedParent <- _eventSubscription_subscribed <$> readIORef (_fanInt_subscriptionRef self)
1695− return $ EventSubscribed
1650+ return $ EventSubscribed
16961651 { eventSubscribedHeightRef = eventSubscribedHeightRef subscribedParent
16971652 , eventSubscribedRetained = toAny (_fanInt_subscriptionRef self, ticket)
16981653#ifdef DEBUG_CYCLES
@@ -1701,7 +1656,7 @@ fanIntSubscribed ticket self = do
17011656 , eventSubscribedWhoCreated = whoCreatedIORef $ _fanInt_subscriptionRef self
17021657#endif
17031658 }
1704−
1659+
17051660
17061661{-# INLINABLE getFanSubscribed #-}
17071662getFanSubscribed :: (HasSpiderTimeline x, GCompare k) => k a -> Fan x k v -> Subscriber x (v a) -> EventM x (WeakBagTicket, FanSubscribed x k v, Maybe (v a))
@@ -1914,7 +1869,7 @@ mergeCheap nt = mergeGCheap' unMergeSubscribedParent getInitialSubscribers updat
19141869 liftIO $ modifyIORef' heightBagRef $ heightBagAdd newParentHeight
19151870 return $ DMap.insertLookupWithKey' (\_ new _ -> new) k newParent ps
19161871 forM_ mOldSubd $ \oldSubd -> do
1917− oldHeight <- liftIO $ getEventSubscribedHeight $
1872+ oldHeight <- liftIO $ getEventSubscribedHeight $
19181873 _eventSubscription_subscribed $ unMergeSubscribedParent oldSubd
19191874
19201875 liftIO $ modifyIORef heightBagRef $ heightBagRemove oldHeight
@@ -1960,7 +1915,7 @@ mergeCheapWithMove nt = mergeGCheap' _mergeSubscribedParentWithMove_subscription
19601915 let moveOrDelete :: forall a. k a -> PatchDMapWithMove.NodeInfo k q a -> MergeSubscribedParentWithMove x k a -> Constant (EventM x (Maybe (EventSubscription x))) a
19611916 moveOrDelete _ ni parent = Constant $ case getComposeMaybe $ PatchDMapWithMove._nodeInfo_to ni of
19621917 Nothing -> do
1963− oldHeight <- liftIO $ getEventSubscribedHeight $ _eventSubscription_subscribed $
1918+ oldHeight <- liftIO $ getEventSubscribedHeight $ _eventSubscription_subscribed $
19641919 _mergeSubscribedParentWithMove_subscription parent
19651920
19661921 liftIO $ modifyIORef heightBagRef $ heightBagRemove oldHeight
@@ -1968,7 +1923,7 @@ mergeCheapWithMove nt = mergeGCheap' _mergeSubscribedParentWithMove_subscription
19681923 Just toKey -> do
19691924 liftIO $ writeIORef (_mergeSubscribedParentWithMove_key parent) $! toKey
19701925 return Nothing
1971− toDelete <- fmap catMaybes $ mapM (\(_ :=> v) -> getConstant v) $ DMap.toList $
1926+ toDelete <- fmap catMaybes $ mapM (\(_ :=> v) -> getConstant v) $ DMap.toList $
19721927 DMap.intersectionWithKey moveOrDelete (unPatchDMapWithMove p) oldParents
19731928
19741929 return (toDelete, applyAlways p' oldParents)
@@ -2021,16 +1976,16 @@ invalidateMergeHeight' :: IORef Height -> Subscriber x a -> IO ()
20211976invalidateMergeHeight' heightRef sub = do
20221977 oldHeight <- readIORef heightRef
20231978 -- If the height used to be valid, it must be invalid now; we should never have *more* heights than we have parents
2024− when (oldHeight /= invalidHeight) $ do
1979+ when (oldHeight /= invalidHeight) $ do
20251980 writeIORef heightRef $! invalidHeight
20261981 subscriberInvalidateHeight sub oldHeight
20271982
20281983revalidateMergeHeight :: Merge x k v s -> IO ()
20291984revalidateMergeHeight m = do
20301985 currentHeight <- readIORef $ _merge_heightRef m
2031−
1986+
20321987 -- revalidateMergeHeight may be called multiple times; perhaps the's a way to finesse it to avoid this check
2033− when (currentHeight == invalidHeight) $ do
1988+ when (currentHeight == invalidHeight) $ do
20341989 heights <- readIORef $ _merge_heightBagRef m
20351990 parents <- readIORef $ _merge_parentsRef m
20361991 -- When the number of heights in the bag reaches the number of parents, we should have a valid height
@@ -2038,7 +1993,7 @@ revalidateMergeHeight m = do
20381993 LT -> return ()
20391994 EQ -> do
20401995 let height = succHeight $ heightBagMax heights
2041− traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
1996+ traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
20421997 writeIORef (_merge_heightRef m) $! height
20431998 subscriberRecalculateHeight (_merge_sub m) height
20441999 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
@@ -2046,22 +2001,22 @@ revalidateMergeHeight m = do
20462001scheduleMergeSelf :: HasSpiderTimeline x => Merge x k v s -> Height -> EventM x ()
20472002scheduleMergeSelf m height = scheduleMerge' height (_merge_heightRef m) $ do
20482003 vals <- liftIO $ readIORef $ _merge_accumRef m
2049− -- Once we're done with this, we can clear it immediately, because if there's a cacheEvent in front of us,
2004+ -- Once we're done with this, we can clear it immediately, because if there's a cacheEvent in front of us,
20502005 -- it'll handle subsequent subscribers, and if not, we won't get subsequent subscribers
2051− liftIO $ writeIORef (_merge_accumRef m) $! DMap.empty
2006+ liftIO $ writeIORef (_merge_accumRef m) $! DMap.empty
20522007 --TODO: Assert that m is not empty
20532008 subscriberPropagate (_merge_sub m) vals
20542009
20552010checkCycle :: HasSpiderTimeline x => EventSubscribed x -> EventM x ()
20562011checkCycle subscribed = liftIO $ do
20572012 height <- readIORef (eventSubscribedHeightRef subscribed)
2058−
2013+
20592014 -- currentHeight <- getCurrentHeight
20602015 -- when (height <= currentHeight) $ if height /= invalidHeight
20612016 -- then do
20622017 -- myStack <- liftIO $ whoCreatedIORef undefined --TODO
20632018 -- error $ "Height (" ++ show height ++ ") is not greater than current height (" ++ show currentHeight ++ ")\n" ++ unlines (reverse myStack)
2064− -- else liftIO $
2019+ -- else liftIO $
20652020 when (height == invalidHeight) $
20662021#ifdef DEBUG_CYCLES
20672022 do
@@ -2114,17 +2069,17 @@ mergeGCheap' getParent getInitialSubscribers updateFunc destroy d = Event $ \sub
21142069 parentsRef :: IORef (DMap k s) <- liftIO $ newIORef $ error "merge: parentsRef not yet initialized"
21152070 changeSubdRef <- liftIO $ newIORef $ error "getMergeSubscribed: changeSubdRef not yet initialized"
21162071
2117− let subscribed = EventSubscribed
2118− { eventSubscribedHeightRef = heightRef
2072+ let subscribed = EventSubscribed
2073+ { eventSubscribedHeightRef = heightRef
21192074 , eventSubscribedRetained = toAny (parentsRef, changeSubdRef)
21202075#ifdef DEBUG_CYCLES
21212076 , eventSubscribedGetParents = do
21222077 let getParent' (_ :=> v) = _eventSubscription_subscribed (getParent v)
2123− fmap getParent' . DMap.toList <$> readIORef parentsRef
2078+ fmap getParent' . DMap.toList <$> readIORef parentsRef
21242079 , eventSubscribedHasOwnHeightRef = False
21252080 , eventSubscribedWhoCreated = whoCreatedIORef heightRef
2126−#endif
2127− }
2081+#endif
2082+ }
21282083
21292084 m = Merge
21302085 { _merge_parentsRef = parentsRef
@@ -2162,9 +2117,9 @@ mergeGCheap' getParent getInitialSubscribers updateFunc destroy d = Event $ \sub
21622117 -- its subscription.
21632118 liftIO $ writeIORef changeSubdRef (changeSubscriber, changeSubscription)
21642119 let unsubscribeAll = destroy =<< readIORef parentsRef
2165−
2120+
21662121 return (EventSubscription unsubscribeAll subscribed, occ)
2167−
2122+
21682123
21692124mergeInt :: forall x a. (HasSpiderTimeline x) => DynamicS x (PatchIntMap (Event x a)) -> Event x (IntMap a)
21702125mergeInt = cacheEvent . mergeIntCheap
@@ -2178,14 +2133,14 @@ mergeIntCheap d = Event $ \sub -> do
21782133 heightBagRef <- liftIO $ newIORef heightBagEmpty
21792134 parents <- liftIO $ FastMutableIntMap.newEmpty
21802135 changeSubdRef <- liftIO $ newIORef $ error "getMergeSubscribed: changeSubdRef not yet initialized"
2181− let subscribed = EventSubscribed
2182− { eventSubscribedHeightRef = heightRef
2136+ let subscribed = EventSubscribed
2137+ { eventSubscribedHeightRef = heightRef
21832138 , eventSubscribedRetained = toAny (parents, changeSubdRef)
21842139#ifdef DEBUG_CYCLES
21852140 , eventSubscribedGetParents = fmap (_eventSubscription_subscribed . snd) <$> FastMutableIntMap.toList parents
21862141 , eventSubscribedHasOwnHeightRef = False
21872142 , eventSubscribedWhoCreated = whoCreatedIORef heightRef
2188−#endif
2143+#endif
21892144 }
21902145 let scheduleSelf = do
21912146 height <- liftIO $ readIORef $ heightRef
@@ -2202,7 +2157,7 @@ mergeIntCheap d = Event $ \sub -> do
22022157 LT -> return ()
22032158 EQ -> do
22042159 let height = succHeight $ heightBagMax heights
2205− traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
2160+ traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
22062161 writeIORef heightRef $! height
22072162 subscriberRecalculateHeight sub height
22082163 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show numParents <> ") for Merge"
@@ -2274,7 +2229,7 @@ mergeIntCheap d = Event $ \sub -> do
22742229 -- its subscription.
22752230 liftIO $ writeIORef changeSubdRef (changeSubscriber, changeSubscription)
22762231 let unsubscribeAll = traverse_ unsubscribe =<< FastMutableIntMap.getFrozenAndClear parents
2277−
2232+
22782233
22792234 return (EventSubscription unsubscribeAll subscribed, occ)
22802235
@@ -2385,7 +2340,7 @@ runFrame a = SpiderHost $ do
23852340 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
23862341 {-
23872342 stackTrace <- liftIO $ fmap renderStack $ ccsToStrings =<< (getCCSOf $! switchSubscribedParent subscribed)
2388− liftIO $ debugStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
2343+ liftIO $ putStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
23892344 EventRoot _ -> "EventRoot"
23902345 EventNever -> "EventNever"
23912346 _ -> "something else"
@@ -2520,8 +2475,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Event
25202475 {-# INLINABLE headE #-}
25212476 headE = R.slowHeadE
25222477-- headE (SpiderEvent e) = SpiderEvent <$> Reflex.Spider.Internal.headE e
2523− {-# INLINABLE now #-}
2524− now = nowSpiderEventM
25252478
25262479instance Reflex.Class.MonadSample (SpiderTimeline x) (SpiderPullM x) where
25272480 {-# INLINABLE sample #-}
@@ -2543,9 +2496,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
25432496 {-# INLINABLE headE #-}
25442497 headE = R.slowHeadE
25452498-- headE (SpiderEvent e) = SpiderPushM $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2546− {-# INLINABLE now #-}
2547− now = SpiderPushM nowSpiderEventM
2548−
25492499
25502500instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x)) where
25512501 {-# INLINE return #-}
@@ -2554,10 +2504,8 @@ instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x))
25542504 x >>= f = SpiderDynamic $ dynamicDynIdentity $ newJoinDyn $ newMapDyn (unSpiderDynamic . f) $ unSpiderDynamic x
25552505 {-# INLINE (>>) #-}
25562506 (>>) = (*>)
2557−#if !MIN_VERSION_base(4,13,0)
25582507 {-# INLINE fail #-}
25592508 fail _ = error "Dynamic does not support 'fail'"
2560−#endif
25612509
25622510{-# INLINABLE newJoinDyn #-}
25632511newJoinDyn :: HasSpiderTimeline x => DynamicS x (Identity (DynamicS x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
@@ -2609,9 +2557,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
26092557 buildDynamic getV0 e = runFrame . runSpiderHostFrame $ Reflex.Class.buildDynamic getV0 e
26102558 {-# INLINABLE headE #-}
26112559 headE e = runFrame . runSpiderHostFrame $ Reflex.Class.headE e
2612− {-# INLINABLE now #-}
2613− now = runFrame . runSpiderHostFrame $ Reflex.Class.now
2614−
26152560
26162561instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHostFrame x) where
26172562 sample = SpiderHostFrame . readBehaviorUntracked . unSpiderBehavior --TODO: This can cause problems with laziness, so we should get rid of it if we can
@@ -2628,8 +2573,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
26282573 {-# INLINABLE headE #-}
26292574 headE = R.slowHeadE
26302575-- headE (SpiderEvent e) = SpiderHostFrame $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2631− {-# INLINABLE now #-}
2632− now = SpiderHostFrame Reflex.Class.now
26332576
26342577instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHost x) where
26352578 {-# INLINABLE sample #-}
@@ -2650,8 +2593,6 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Refle
26502593 buildDynamic getV0 e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.buildDynamic getV0 e
26512594 {-# INLINABLE headE #-}
26522595 headE e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.headE e
2653− {-# INLINABLE now #-}
2654− now = Reflex.Spider.Internal.ReadPhase Reflex.Class.now
26552596
26562597--------------------------------------------------------------------------------
26572598-- Deprecated items
@@ -2775,7 +2716,7 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
27752716 fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
27762717 {-# INLINABLE mergeG #-}
27772718 mergeG
2778− :: forall k2 (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2719+ :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
27792720 => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
27802721 -> DMap k q
27812722 -> R.Event (SpiderTimeline x) (DMap k v)
@@ -2843,14 +2784,8 @@ instance Monad (SpiderHost x) where
28432784 SpiderHost x >> SpiderHost y = SpiderHost $ x >> y
28442785 {-# INLINABLE return #-}
28452786 return x = SpiderHost $ return x
2846−#if !MIN_VERSION_base(4,13,0)
28472787 {-# 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
2788+ fail s = SpiderHost $ fail s
28542789
28552790-- | Run an action affecting the global Spider timeline; this will be guarded by
28562791-- a mutex for that timeline
@@ -2872,10 +2807,8 @@ instance Monad (SpiderHostFrame x) where
28722807 SpiderHostFrame x >> SpiderHostFrame y = SpiderHostFrame $ x >> y
28732808 {-# INLINABLE return #-}
28742809 return x = SpiderHostFrame $ return x
2875−#if !MIN_VERSION_base(4,13,0)
28762810 {-# INLINABLE fail #-}
28772811 fail s = SpiderHostFrame $ fail s
2878−#endif
28792812
28802813instance NotReady (SpiderTimeline x) (SpiderHostFrame x) where
28812814 notReadyUntil _ = pure ()