@@ -9,7 +9,6 @@
99{-# LANGUAGE GADTs #-}
1010{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1111{-# LANGUAGE LambdaCase #-}
12−{-# LANGUAGE MultiWayIf #-}
1312{-# LANGUAGE RankNTypes #-}
1413{-# LANGUAGE RoleAnnotations #-}
1514{-# LANGUAGE ScopedTypeVariables #-}
@@ -20,7 +19,7 @@
2019{-# LANGUAGE DeriveFunctor #-}
2120{-# LANGUAGE StandaloneDeriving #-}
2221{-# LANGUAGE InstanceSigs #-}
23−{-# LANGUAGE RecursiveDo #-}
22+{-# LANGUAGE MultiWayIf #-}
2423
2524#ifdef USE_REFLEX_OPTIMIZER
2625{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
@@ -39,15 +38,17 @@ import Control.Monad hiding (forM, forM_, mapM, mapM_)
3938import Control.Monad.Exception
4039import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
4140import Control.Monad.Primitive
42−--import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_)
4341import Control.Monad.Reader.Class
4442import Control.Monad.IO.Class
4543import Control.Monad.ReaderIO
4644import 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, DSum (..))
49+import Data.Dependent.Map (DMap)
5050import qualified Data.Dependent.Map as DMap
51+import Data.Dependent.Sum (DSum (..))
5152import Data.FastMutableIntMap (FastMutableIntMap, PatchIntMap (..))
5253import qualified Data.FastMutableIntMap as FastMutableIntMap
5354import Data.Foldable hiding (concat, elem, sequence_)
@@ -64,6 +65,7 @@ import Data.Monoid (mempty, (<>))
6465import Data.Proxy
6566import Data.These
6667import Data.Traversable
68+import Data.Type.Equality ((:~:)(Refl))
6769import Data.Witherable (Filterable, mapMaybe)
6870import GHC.Exts hiding (toList)
6971import GHC.IORef (IORef (..))
@@ -84,9 +86,9 @@ import Data.Zip (Zip (..))
8486
8587#ifdef DEBUG_CYCLES
8688import Control.Monad.State hiding (forM, forM_, mapM, mapM_, sequence)
87−import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
8889#endif
8990
91+import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
9092import qualified Data.List.NonEmpty as NonEmpty
9193import Data.Tree (Forest, Tree (..), drawForest)
9294import Data.List (isPrefixOf)
@@ -104,15 +106,21 @@ import qualified Reflex.Class
104106import qualified Reflex.Class as R
105107import qualified Reflex.Host.Class
106108import Reflex.NotReady.Class
107−import Reflex.Patch
108−import qualified Reflex.Patch.DMapWithMove as PatchDMapWithMove
109+import Data.Patch
110+import qualified Data.Patch.DMapWithMove as PatchDMapWithMove
109111import Reflex.PerformEvent.Base (PerformEventT)
110112
111113#ifdef DEBUG_TRACE_EVENTS
112114import qualified Data.ByteString.Char8 as BS8
113115import System.IO (stderr)
114116import Data.List (isPrefixOf)
117+#endif
115118
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
116124withStackOneLine :: (BS8.ByteString -> a) -> a
117125withStackOneLine expr = unsafePerformIO $ do
118126 stack <- currentCallStack
@@ -121,15 +129,39 @@ withStackOneLine expr = unsafePerformIO $ do
121129
122130#endif
123131
132+debugPropagate :: Bool
133+
134+debugInvalidateHeight :: Bool
135+
136+debugInvalidate :: Bool
137+
124138#ifdef DEBUG
125139#define DEBUG_NODEIDS
126140
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+
127159class HasNodeId a where
128160 getNodeId :: a -> Int
129161
130162instance HasNodeId (CacheSubscribed x a) where
131163 getNodeId = _cacheSubscribed_nodeId
132−
164+
133165instance HasNodeId (FanInt x a) where
134166 getNodeId = _fanInt_nodeId
135167
@@ -156,16 +188,24 @@ showNodeId :: HasNodeId a => a -> String
156188showNodeId = showNodeId' . getNodeId
157189
158190showNodeId' :: Int -> String
159−showNodeId' = ("#"<>) . show
191+showNodeId' = ("#"<>) . show
160192
161193
162194#else
163195
196+debugPropagate = False
197+debugInvalidateHeight = False
198+debugInvalidate = False
199+
164200-- This must be inline, or error messages will cause memory leaks due to retaining the node in question
165201{-# INLINE showNodeId #-}
166202showNodeId :: a -> String
167203showNodeId _ = ""
168204
205+{-# INLINE showNodeId' #-}
206+showNodeId' :: Int -> String
207+showNodeId' _ = ""
208+
169209#endif
170210
171211#ifdef DEBUG_NODEIDS
@@ -213,11 +253,11 @@ subscribeAndRead = unEvent
213253{-# INLINE [1] pushCheap #-}
214254pushCheap :: HasSpiderTimeline x => (a -> ComputeM x (Maybe b)) -> Event x a -> Event x b
215255pushCheap !f e = Event $ \sub -> do
216− (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" (sub
256+ (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" $ sub
217257 { subscriberPropagate = \a -> do
218258 mb <- f a
219259 mapM_ (subscriberPropagate sub) mb
220− })
260+ }
221261 occ' <- join <$> mapM f occ
222262 return (subscription, occ')
223263
@@ -253,9 +293,9 @@ headE originalE = do
253293 let clearParent = liftIO $ writeIORef parent Nothing
254294 (_, occ) <- subscribeAndReadHead originalE $ terminalSubscriber $ const clearParent
255295 when (isJust occ) clearParent
256− return $ Event $ \sub ->
296+ return $ Event $ \sub ->
257297 liftIO (readIORef parent) >>= \case
258− Nothing -> subscribeAndReadNever
298+ Nothing -> subscribeAndReadNever
259299 Just e -> subscribeAndReadHead e sub
260300
261301data CacheSubscribed x a
@@ -267,7 +307,20 @@ data CacheSubscribed x a
267307#endif
268308 }
269309
270−
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+ )
271324
272325-- | Construct an 'Event' whose value is guaranteed not to be recomputed
273326-- repeatedly
@@ -298,39 +351,18 @@ cacheEvent e =
298351#endif
299352 subscribers <- liftIO FastWeakBag.empty
300353 occRef <- liftIO $ newIORef Nothing -- This should never be read prior to being set below
301−<<<<<<< HEAD
354+#ifdef DEBUG_NODEIDS
302355 (parentSub, occ) <- subscribeAndRead e $ debugSubscriber' ("cacheEvent" <> showNodeId' nodeId) $ Subscriber
356+#else
357+ (parentSub, occ) <- subscribeAndRead e $ Subscriber
358+#endif
303359 { subscriberPropagate = \a -> do
304− -- checkCycle (_eventSubscription_subscribed parentSub)
305360 liftIO $ writeIORef occRef (Just a)
306361 scheduleClear occRef
307362 propagateFast a subscribers
308− , subscriberInvalidateHeight = FastWeakBag.traverse subscribers . invalidateSubscriberHeight
309− , subscriberRecalculateHeight = FastWeakBag.traverse subscribers . recalculateSubscriberHeight
363+ , subscriberInvalidateHeight = FastWeakBag.traverse_ subscribers . invalidateSubscriberHeight
364+ , subscriberRecalculateHeight = FastWeakBag.traverse_ subscribers . recalculateSubscriberHeight
310365 }
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
334366 when (isJust occ) $ do
335367 liftIO $ writeIORef occRef occ -- Set the initial value of occRef; we don't need to do this if occ is Nothing
336368 scheduleClear occRef
@@ -340,20 +372,20 @@ cacheEvent e =
340372 , _cacheSubscribed_occurrence = occRef
341373#ifdef DEBUG_NODEIDS
342374 , _cacheSubscribed_nodeId = nodeId
343−#endif
375+#endif
344376 }
345377 subscribedTicket <- liftIO $ mkFastWeakTicket subscribed
346378 liftIO $ writeIORef mSubscribedRef =<< getFastWeakTicketWeak subscribedTicket
347379 return subscribedTicket
348380 liftIO $ cacheSubscription sub mSubscribedRef subscribedTicket
349381
350−cacheSubscription :: Subscriber x a -> IORef (FastWeak (CacheSubscribed x a))
382+cacheSubscription :: Subscriber x a -> IORef (FastWeak (CacheSubscribed x a))
351383 -> FastWeakTicket (CacheSubscribed x a) -> IO (EventSubscription x, Maybe a)
352−cacheSubscription sub mSubscribedRef subscribedTicket = do
384+cacheSubscription sub mSubscribedRef subscribedTicket = do
353385 subscribed <- getFastWeakTicketValue subscribedTicket
354386 ticket <- FastWeakBag.insert sub $ _cacheSubscribed_subscribers subscribed
355387 occ <- readIORef $ _cacheSubscribed_occurrence subscribed
356−
388+
357389 let parentSub = _cacheSubscribed_parent subscribed
358390 es = EventSubscription
359391 { _eventSubscription_unsubscribe = do
@@ -372,9 +404,9 @@ cacheSubscription sub mSubscribedRef subscribedTicket = do
372404 , eventSubscribedGetParents = return [_eventSubscription_subscribed parentSub]
373405 , eventSubscribedHasOwnHeightRef = False
374406 , eventSubscribedWhoCreated = whoCreatedIORef mSubscribedRef
375−#endif
407+#endif
376408 }
377− }
409+ }
378410 return (es, occ)
379411
380412
@@ -432,16 +464,6 @@ data Subscriber x a = Subscriber
432464 , subscriberRecalculateHeight :: !(Height -> IO ())
433465 }
434466
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−
445467newSubscriberHold :: (HasSpiderTimeline x, Patch p) => Hold x p -> IO (Subscriber x p)
446468newSubscriberHold h = return $ Subscriber
447469 { subscriberPropagate = {-# SCC "traverseHold" #-} propagateSubscriberHold h
@@ -495,7 +517,7 @@ newSubscriberCoincidenceOuter subscribed = debugSubscriber ("SubscriberCoinciden
495517 liftIO $ writeIORef (coincidenceSubscribedInnerParent subscribed) $ Just innerSubd
496518 scheduleClear $ coincidenceSubscribedInnerParent subscribed
497519 case occ of
498− Nothing ->
520+ Nothing ->
499521 when (innerHeight > outerHeight) $ liftIO $ do -- If the event fires, it will fire at a later height
500522 writeIORef (coincidenceSubscribedHeight subscribed) $! innerHeight
501523 WeakBag.traverse_ (coincidenceSubscribedSubscribers subscribed) $ invalidateSubscriberHeight outerHeight
@@ -505,7 +527,7 @@ newSubscriberCoincidenceOuter subscribed = debugSubscriber ("SubscriberCoinciden
505527 scheduleClear $ coincidenceSubscribedOccurrence subscribed
506528 propagate o $ coincidenceSubscribedSubscribers subscribed
507529 , subscriberInvalidateHeight = \_ -> invalidateCoincidenceHeight subscribed
508− , subscriberRecalculateHeight = \_ -> recalculateCoincidenceHeight subscribed
530+ , subscriberRecalculateHeight = \_ -> recalculateCoincidenceHeight subscribed
509531 }
510532
511533newSubscriberCoincidenceInner :: forall x a. HasSpiderTimeline x => CoincidenceSubscribed x a -> IO (Subscriber x a)
@@ -526,18 +548,18 @@ invalidateSubscriberHeight :: Height -> Subscriber x a -> IO ()
526548invalidateSubscriberHeight = flip subscriberInvalidateHeight
527549
528550recalculateSubscriberHeight :: Height -> Subscriber x a -> IO ()
529−recalculateSubscriberHeight = flip subscriberRecalculateHeight
551+recalculateSubscriberHeight = flip subscriberRecalculateHeight
530552
531553-- | Propagate everything at the current height
532554propagate :: 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) $
534556 -- 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
535557 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
536558 WeakBag.traverse_ subscribers $ \s -> subscriberPropagate s a
537559
538560-- | Propagate everything at the current height
539561propagateFast :: 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) $
541563 -- 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
542564 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
543565 FastWeakBag.traverse_ subscribers $ \s -> subscriberPropagate s a
@@ -585,6 +607,16 @@ eventSubscribedNever = EventSubscribed
585607 , eventSubscribedWhoCreated = return ["never"]
586608#endif
587609 }
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+ }
588620
589621eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
590622eventSubscribedFan !subscribed = EventSubscribed
@@ -962,8 +994,10 @@ instance Monad (BehaviorM x) where
962994 BehaviorM x >> BehaviorM y = BehaviorM $ x >> y
963995 {-# INLINE return #-}
964996 return x = BehaviorM $ return x
997+#if !MIN_VERSION_base(4,13,0)
965998 {-# INLINE fail #-}
966999 fail s = BehaviorM $ fail s
1000+#endif
9671001
9681002data BehaviorSubscribed x a
9691003 = forall p. BehaviorSubscribedHold (Hold x p)
@@ -1023,7 +1057,7 @@ data SomeMergeUpdate x = SomeMergeUpdate
10231057newtype SomeMergeInit x = SomeMergeInit { unSomeMergeInit :: EventM x () }
10241058
10251059-- 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)
10271061
10281062newtype MergeSubscribedParent x a = MergeSubscribedParent { unMergeSubscribedParent :: EventSubscription x }
10291063
@@ -1048,7 +1082,7 @@ heightBagFromList :: [Height] -> HeightBag
10481082heightBagFromList heights = heightBagVerify $ foldl' (flip heightBagAdd) heightBagEmpty heights
10491083
10501084heightBagAdd :: 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) $
10521086 IntMap.insertWithKey (\_ _ old -> succ old) h 0 c
10531087
10541088heightBagRemove :: Height -> HeightBag -> HeightBag
@@ -1059,11 +1093,11 @@ heightBagRemove (Height h) b@(HeightBag s c) = heightBagVerify $ case IntMap.loo
10591093 _ -> IntMap.insert h (pred old) c
10601094
10611095heightBagRemoveMaybe :: 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
10631097 removed old = HeightBag (pred s) $ case old of
10641098 0 -> IntMap.delete h c
10651099 _ -> IntMap.insert h (pred old) c
1066−
1100+
10671101
10681102heightBagMax :: HeightBag -> Height
10691103heightBagMax (HeightBag _ c) = case IntMap.maxViewWithKey c of
@@ -1312,7 +1346,7 @@ mkWeakPtrWithDebug x debugNote = do
13121346 x' <- evaluate x
13131347 mkWeakPtr x' $
13141348 if debugFinalize
1315− then Just $ putStrLn $ "finalizing: " ++ debugNote
1349+ then Just $ debugStrLn $ "finalizing: " ++ debugNote
13161350 else Nothing
13171351
13181352type WeakList a = [Weak a]
@@ -1328,19 +1362,19 @@ debugSubscriber :: forall x a. HasSpiderTimeline x => String -> Subscriber x a -
13281362debugSubscriber description = return . debugSubscriber' description
13291363
13301364debugSubscriber' :: forall x a. HasSpiderTimeline x => String -> Subscriber x a -> Subscriber x a
1331−debugSubscriber' description subscribed = Subscriber
1365+debugSubscriber' description subscribed = Subscriber
13321366 {
13331367 subscriberPropagate = \m -> do
13341368 tracePropagate (Proxy :: Proxy x) ("subscriberPropagate: " <> description)
13351369 subscriberPropagate subscribed m
13361370 , subscriberInvalidateHeight = \old -> do
1337− putStrLn $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
1371+ traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
13381372 subscriberInvalidateHeight subscribed old
1339− putStrLn $ "invalidateSubscriberHeight: " <> description <> ", done"
1373+ traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", done"
13401374 , subscriberRecalculateHeight = \new -> do
1341− putStrLn $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
1375+ traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
13421376 subscriberRecalculateHeight subscribed new
1343− putStrLn $ "subscriberRecalculateHeight: " <> description <> ", done"
1377+ traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", done"
13441378 }
13451379
13461380
@@ -1354,11 +1388,15 @@ withIncreasedDepth _ a = do
13541388
13551389{-# INLINE tracePropagate #-}
13561390tracePropagate :: (CanTrace x m) => proxy x -> String -> m ()
1357−tracePropagate p = trace p
1391+tracePropagate p = when debugPropagate . trace p
13581392
13591393{-# INLINE traceInvalidate #-}
13601394traceInvalidate :: String -> IO ()
1361−traceInvalidate = liftIO . putStrLn
1395+traceInvalidate = when debugInvalidate . liftIO . debugStrLn
1396+
1397+{-# INLINE traceInvalidateHeight #-}
1398+traceInvalidateHeight :: String -> IO ()
1399+traceInvalidateHeight = when debugInvalidateHeight . liftIO . debugStrLn
13621400
13631401{-# INLINE trace #-}
13641402trace :: (CanTrace x m) => proxy x -> String -> m ()
@@ -1369,7 +1407,7 @@ traceM :: forall x proxy m. (CanTrace x m) => proxy x -> m String -> m ()
13691407traceM _ getMessage = do
13701408 message <- getMessage
13711409 d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
1372− liftIO $ putStrLn $ replicate d ' ' <> message
1410+ liftIO $ debugStrLn $ replicate d ' ' <> message
13731411
13741412#else
13751413
@@ -1385,6 +1423,10 @@ tracePropagate _ _ = return ()
13851423traceInvalidate :: String -> IO ()
13861424traceInvalidate _ = return ()
13871425
1426+{-# INLINE traceInvalidateHeight #-}
1427+traceInvalidateHeight :: String -> IO ()
1428+traceInvalidateHeight _ = return ()
1429+
13881430{-# INLINE debugSubscriber #-}
13891431debugSubscriber :: String -> Subscriber x a -> IO (Subscriber x a)
13901432debugSubscriber _ = return
@@ -1425,17 +1467,20 @@ showStacks :: [[String]] -> String
14251467showStacks = drawForest . listsToForest . fmap (filterStack "Reflex.Spider.Internal")
14261468
14271469filterStack :: String -> [String] -> [String]
1470+#ifdef DEBUG_HIDE_INTERNALS
14281471filterStack prefix = filter (not . (prefix `isPrefixOf`))
1429−
1472+#else
1473+filterStack prefix = id
1474+#endif
14301475
14311476#ifdef DEBUG_CYCLES
14321477
1433−data EventLoopException = EventLoopException [[String]]
1478+data EventLoopException = EventLoopException [[String]]
14341479instance Exception EventLoopException
14351480
14361481instance Show EventLoopException where
1437− show (EventLoopException stacks) = "causality loop detected:\n" <> if null stacks
1438− then "no location information, compile with profiling enabled for stack tree"
1482+ show (EventLoopException stacks) = "causality loop detected:\n" <> if null stacks
1483+ then "no location information, compile with profiling enabled for stack tree"
14391484 else showStacks stacks
14401485
14411486#else
@@ -1453,7 +1498,7 @@ instance Show EventLoopException where
14531498{-# INLINE propagateSubscriberHold #-}
14541499propagateSubscriberHold :: forall x p. (HasSpiderTimeline x, Patch p) => Hold x p -> p -> EventM x ()
14551500propagateSubscriberHold h a = do
1456− traceM (Proxy :: Proxy x) $ liftIO $ do
1501+ {-# SCC "trace" #-} when debugPropagate $ traceM (Proxy :: Proxy x) $ liftIO $ do
14571502 invalidators <- liftIO $ readIORef $ holdInvalidators h
14581503 return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
14591504
@@ -1461,7 +1506,7 @@ propagateSubscriberHold h a = do
14611506 case {-# SCC "apply" #-} apply a v of
14621507 Nothing -> return ()
14631508 Just v' -> do
1464− withIncreasedDepth (Proxy :: Proxy x) $
1509+ {-# SCC "trace2" #-} withIncreasedDepth (Proxy :: Proxy x) $
14651510 tracePropagate (Proxy :: Proxy x) ("propagateSubscriberHold: assigning Hold" <> showNodeId h)
14661511 vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
14671512 iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
@@ -1582,7 +1627,7 @@ data FanInt x a = FanInt
15821627 { _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
15831628 , _fanInt_subscriptionRef :: {-# UNPACK #-} !(IORef (EventSubscription x)) -- This should have a valid subscription iff subscribers is non-empty
15841629 , _fanInt_occRef :: {-# UNPACK #-} !(IORef (IntMap a))
1585−#ifdef DEBUG_NODEIDS
1630+#ifdef DEBUG_NODEIDS
15861631 , _fanInt_nodeId :: {-# UNPACK #-} !Int
15871632#endif
15881633 }
@@ -1592,15 +1637,15 @@ newFanInt = do
15921637 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
15931638 subscriptionRef <- newIORef $ error "fanInt: no subscription"
15941639 occRef <- newIORef $ error "fanInt: no occurrence"
1595−#ifdef DEBUG_NODEIDS
1640+#ifdef DEBUG_NODEIDS
15961641 nodeId <- newNodeId
15971642#endif
15981643 return $ FanInt
15991644 { _fanInt_subscribers = subscribers
16001645 , _fanInt_subscriptionRef = subscriptionRef
16011646 , _fanInt_occRef = occRef
1602−#ifdef DEBUG_NODEIDS
1603− , _fanInt_nodeId = nodeId
1647+#ifdef DEBUG_NODEIDS
1648+ , _fanInt_nodeId = nodeId
16041649#endif
16051650 }
16061651
@@ -1616,15 +1661,15 @@ fanInt p = unsafePerformIO $ do
16161661 liftIO $ writeIORef (_fanInt_occRef self) m
16171662 scheduleIntClear $ _fanInt_occRef self
16181663 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 ->
16201665 subscriberPropagate s v
1621− , subscriberInvalidateHeight = \old ->
1622− FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1623− FastWeakBag.traverse_ b $ \s ->
1666+ , subscriberInvalidateHeight = \old ->
1667+ FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1668+ FastWeakBag.traverse_ b $ \s ->
16241669 subscriberInvalidateHeight s old
16251670 , subscriberRecalculateHeight = \new ->
1626− FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1627− FastWeakBag.traverse_ b $ \s ->
1671+ FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b ->
1672+ FastWeakBag.traverse_ b $ \s ->
16281673 subscriberRecalculateHeight s new
16291674 }
16301675 liftIO $ do
@@ -1640,14 +1685,14 @@ fanInt p = unsafePerformIO $ do
16401685 Just b -> return b
16411686 ticket <- liftIO $ FastWeakBag.insert sub b
16421687 currentOcc <- readIORef (_fanInt_occRef self)
1643−
1688+
16441689 subscribed <- fanIntSubscribed ticket self
16451690 return (EventSubscription (FastWeakBag.remove ticket) subscribed, IntMap.lookup k currentOcc)
16461691
16471692fanIntSubscribed :: FastWeakBagTicket k -> FanInt x a -> IO (EventSubscribed x)
1648−fanIntSubscribed ticket self = do
1693+fanIntSubscribed ticket self = do
16491694 subscribedParent <- _eventSubscription_subscribed <$> readIORef (_fanInt_subscriptionRef self)
1650− return $ EventSubscribed
1695+ return $ EventSubscribed
16511696 { eventSubscribedHeightRef = eventSubscribedHeightRef subscribedParent
16521697 , eventSubscribedRetained = toAny (_fanInt_subscriptionRef self, ticket)
16531698#ifdef DEBUG_CYCLES
@@ -1656,7 +1701,7 @@ fanIntSubscribed ticket self = do
16561701 , eventSubscribedWhoCreated = whoCreatedIORef $ _fanInt_subscriptionRef self
16571702#endif
16581703 }
1659−
1704+
16601705
16611706{-# INLINABLE getFanSubscribed #-}
16621707getFanSubscribed :: (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
18691914 liftIO $ modifyIORef' heightBagRef $ heightBagAdd newParentHeight
18701915 return $ DMap.insertLookupWithKey' (\_ new _ -> new) k newParent ps
18711916 forM_ mOldSubd $ \oldSubd -> do
1872− oldHeight <- liftIO $ getEventSubscribedHeight $
1917+ oldHeight <- liftIO $ getEventSubscribedHeight $
18731918 _eventSubscription_subscribed $ unMergeSubscribedParent oldSubd
18741919
18751920 liftIO $ modifyIORef heightBagRef $ heightBagRemove oldHeight
@@ -1915,7 +1960,7 @@ mergeCheapWithMove nt = mergeGCheap' _mergeSubscribedParentWithMove_subscription
19151960 let moveOrDelete :: forall a. k a -> PatchDMapWithMove.NodeInfo k q a -> MergeSubscribedParentWithMove x k a -> Constant (EventM x (Maybe (EventSubscription x))) a
19161961 moveOrDelete _ ni parent = Constant $ case getComposeMaybe $ PatchDMapWithMove._nodeInfo_to ni of
19171962 Nothing -> do
1918− oldHeight <- liftIO $ getEventSubscribedHeight $ _eventSubscription_subscribed $
1963+ oldHeight <- liftIO $ getEventSubscribedHeight $ _eventSubscription_subscribed $
19191964 _mergeSubscribedParentWithMove_subscription parent
19201965
19211966 liftIO $ modifyIORef heightBagRef $ heightBagRemove oldHeight
@@ -1923,7 +1968,7 @@ mergeCheapWithMove nt = mergeGCheap' _mergeSubscribedParentWithMove_subscription
19231968 Just toKey -> do
19241969 liftIO $ writeIORef (_mergeSubscribedParentWithMove_key parent) $! toKey
19251970 return Nothing
1926− toDelete <- fmap catMaybes $ mapM (\(_ :=> v) -> getConstant v) $ DMap.toList $
1971+ toDelete <- fmap catMaybes $ mapM (\(_ :=> v) -> getConstant v) $ DMap.toList $
19271972 DMap.intersectionWithKey moveOrDelete (unPatchDMapWithMove p) oldParents
19281973
19291974 return (toDelete, applyAlways p' oldParents)
@@ -1976,16 +2021,16 @@ invalidateMergeHeight' :: IORef Height -> Subscriber x a -> IO ()
19762021invalidateMergeHeight' heightRef sub = do
19772022 oldHeight <- readIORef heightRef
19782023 -- 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
19802025 writeIORef heightRef $! invalidHeight
19812026 subscriberInvalidateHeight sub oldHeight
19822027
19832028revalidateMergeHeight :: Merge x k v s -> IO ()
19842029revalidateMergeHeight m = do
19852030 currentHeight <- readIORef $ _merge_heightRef m
1986−
2031+
19872032 -- 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
19892034 heights <- readIORef $ _merge_heightBagRef m
19902035 parents <- readIORef $ _merge_parentsRef m
19912036 -- 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
19932038 LT -> return ()
19942039 EQ -> do
19952040 let height = succHeight $ heightBagMax heights
1996− traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
2041+ traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
19972042 writeIORef (_merge_heightRef m) $! height
19982043 subscriberRecalculateHeight (_merge_sub m) height
19992044 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
@@ -2001,22 +2046,22 @@ revalidateMergeHeight m = do
20012046scheduleMergeSelf :: HasSpiderTimeline x => Merge x k v s -> Height -> EventM x ()
20022047scheduleMergeSelf m height = scheduleMerge' height (_merge_heightRef m) $ do
20032048 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,
20052050 -- 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
20072052 --TODO: Assert that m is not empty
20082053 subscriberPropagate (_merge_sub m) vals
20092054
20102055checkCycle :: HasSpiderTimeline x => EventSubscribed x -> EventM x ()
20112056checkCycle subscribed = liftIO $ do
20122057 height <- readIORef (eventSubscribedHeightRef subscribed)
2013−
2058+
20142059 -- currentHeight <- getCurrentHeight
20152060 -- when (height <= currentHeight) $ if height /= invalidHeight
20162061 -- then do
20172062 -- myStack <- liftIO $ whoCreatedIORef undefined --TODO
20182063 -- error $ "Height (" ++ show height ++ ") is not greater than current height (" ++ show currentHeight ++ ")\n" ++ unlines (reverse myStack)
2019− -- else liftIO $
2064+ -- else liftIO $
20202065 when (height == invalidHeight) $
20212066#ifdef DEBUG_CYCLES
20222067 do
@@ -2069,17 +2114,17 @@ mergeGCheap' getParent getInitialSubscribers updateFunc destroy d = Event $ \sub
20692114 parentsRef :: IORef (DMap k s) <- liftIO $ newIORef $ error "merge: parentsRef not yet initialized"
20702115 changeSubdRef <- liftIO $ newIORef $ error "getMergeSubscribed: changeSubdRef not yet initialized"
20712116
2072− let subscribed = EventSubscribed
2073− { eventSubscribedHeightRef = heightRef
2117+ let subscribed = EventSubscribed
2118+ { eventSubscribedHeightRef = heightRef
20742119 , eventSubscribedRetained = toAny (parentsRef, changeSubdRef)
20752120#ifdef DEBUG_CYCLES
20762121 , eventSubscribedGetParents = do
20772122 let getParent' (_ :=> v) = _eventSubscription_subscribed (getParent v)
2078− fmap getParent' . DMap.toList <$> readIORef parentsRef
2123+ fmap getParent' . DMap.toList <$> readIORef parentsRef
20792124 , eventSubscribedHasOwnHeightRef = False
20802125 , eventSubscribedWhoCreated = whoCreatedIORef heightRef
2081−#endif
2082− }
2126+#endif
2127+ }
20832128
20842129 m = Merge
20852130 { _merge_parentsRef = parentsRef
@@ -2117,9 +2162,9 @@ mergeGCheap' getParent getInitialSubscribers updateFunc destroy d = Event $ \sub
21172162 -- its subscription.
21182163 liftIO $ writeIORef changeSubdRef (changeSubscriber, changeSubscription)
21192164 let unsubscribeAll = destroy =<< readIORef parentsRef
2120−
2165+
21212166 return (EventSubscription unsubscribeAll subscribed, occ)
2122−
2167+
21232168
21242169mergeInt :: forall x a. (HasSpiderTimeline x) => DynamicS x (PatchIntMap (Event x a)) -> Event x (IntMap a)
21252170mergeInt = cacheEvent . mergeIntCheap
@@ -2133,14 +2178,14 @@ mergeIntCheap d = Event $ \sub -> do
21332178 heightBagRef <- liftIO $ newIORef heightBagEmpty
21342179 parents <- liftIO $ FastMutableIntMap.newEmpty
21352180 changeSubdRef <- liftIO $ newIORef $ error "getMergeSubscribed: changeSubdRef not yet initialized"
2136− let subscribed = EventSubscribed
2137− { eventSubscribedHeightRef = heightRef
2181+ let subscribed = EventSubscribed
2182+ { eventSubscribedHeightRef = heightRef
21382183 , eventSubscribedRetained = toAny (parents, changeSubdRef)
21392184#ifdef DEBUG_CYCLES
21402185 , eventSubscribedGetParents = fmap (_eventSubscription_subscribed . snd) <$> FastMutableIntMap.toList parents
21412186 , eventSubscribedHasOwnHeightRef = False
21422187 , eventSubscribedWhoCreated = whoCreatedIORef heightRef
2143−#endif
2188+#endif
21442189 }
21452190 let scheduleSelf = do
21462191 height <- liftIO $ readIORef $ heightRef
@@ -2157,7 +2202,7 @@ mergeIntCheap d = Event $ \sub -> do
21572202 LT -> return ()
21582203 EQ -> do
21592204 let height = succHeight $ heightBagMax heights
2160− traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
2205+ traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
21612206 writeIORef heightRef $! height
21622207 subscriberRecalculateHeight sub height
21632208 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show numParents <> ") for Merge"
@@ -2229,7 +2274,7 @@ mergeIntCheap d = Event $ \sub -> do
22292274 -- its subscription.
22302275 liftIO $ writeIORef changeSubdRef (changeSubscriber, changeSubscription)
22312276 let unsubscribeAll = traverse_ unsubscribe =<< FastMutableIntMap.getFrozenAndClear parents
2232−
2277+
22332278
22342279 return (EventSubscription unsubscribeAll subscribed, occ)
22352280
@@ -2340,7 +2385,7 @@ runFrame a = SpiderHost $ do
23402385 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
23412386 {-
23422387 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
23442389 EventRoot _ -> "EventRoot"
23452390 EventNever -> "EventNever"
23462391 _ -> "something else"
@@ -2475,6 +2520,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Event
24752520 {-# INLINABLE headE #-}
24762521 headE = R.slowHeadE
24772522-- headE (SpiderEvent e) = SpiderEvent <$> Reflex.Spider.Internal.headE e
2523+ {-# INLINABLE now #-}
2524+ now = nowSpiderEventM
24782525
24792526instance Reflex.Class.MonadSample (SpiderTimeline x) (SpiderPullM x) where
24802527 {-# INLINABLE sample #-}
@@ -2496,6 +2543,9 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
24962543 {-# INLINABLE headE #-}
24972544 headE = R.slowHeadE
24982545-- headE (SpiderEvent e) = SpiderPushM $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2546+ {-# INLINABLE now #-}
2547+ now = SpiderPushM nowSpiderEventM
2548+
24992549
25002550instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x)) where
25012551 {-# INLINE return #-}
@@ -2504,8 +2554,10 @@ instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x))
25042554 x >>= f = SpiderDynamic $ dynamicDynIdentity $ newJoinDyn $ newMapDyn (unSpiderDynamic . f) $ unSpiderDynamic x
25052555 {-# INLINE (>>) #-}
25062556 (>>) = (*>)
2557+#if !MIN_VERSION_base(4,13,0)
25072558 {-# INLINE fail #-}
25082559 fail _ = error "Dynamic does not support 'fail'"
2560+#endif
25092561
25102562{-# INLINABLE newJoinDyn #-}
25112563newJoinDyn :: 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
25572609 buildDynamic getV0 e = runFrame . runSpiderHostFrame $ Reflex.Class.buildDynamic getV0 e
25582610 {-# INLINABLE headE #-}
25592611 headE e = runFrame . runSpiderHostFrame $ Reflex.Class.headE e
2612+ {-# INLINABLE now #-}
2613+ now = runFrame . runSpiderHostFrame $ Reflex.Class.now
2614+
25602615
25612616instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHostFrame x) where
25622617 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
25732628 {-# INLINABLE headE #-}
25742629 headE = R.slowHeadE
25752630-- headE (SpiderEvent e) = SpiderHostFrame $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2631+ {-# INLINABLE now #-}
2632+ now = SpiderHostFrame Reflex.Class.now
25762633
25772634instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHost x) where
25782635 {-# INLINABLE sample #-}
@@ -2593,6 +2650,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Refle
25932650 buildDynamic getV0 e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.buildDynamic getV0 e
25942651 {-# INLINABLE headE #-}
25952652 headE e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.headE e
2653+ {-# INLINABLE now #-}
2654+ now = Reflex.Spider.Internal.ReadPhase Reflex.Class.now
25962655
25972656--------------------------------------------------------------------------------
25982657-- Deprecated items
@@ -2716,7 +2775,7 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
27162775 fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
27172776 {-# INLINABLE mergeG #-}
27182777 mergeG
2719− :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2778+ :: forall k2 (k :: k2 -> *) q (v :: k2 -> *). GCompare k
27202779 => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
27212780 -> DMap k q
27222781 -> R.Event (SpiderTimeline x) (DMap k v)
@@ -2784,8 +2843,14 @@ instance Monad (SpiderHost x) where
27842843 SpiderHost x >> SpiderHost y = SpiderHost $ x >> y
27852844 {-# INLINABLE return #-}
27862845 return x = SpiderHost $ return x
2846+#if !MIN_VERSION_base(4,13,0)
27872847 {-# INLINABLE fail #-}
2788− fail s = SpiderHost $ fail s
2848+ fail = MonadFail.fail
2849+#endif
2850+
2851+instance MonadFail (SpiderHost x) where
2852+ {-# INLINABLE fail #-}
2853+ fail s = SpiderHost $ MonadFail.fail s
27892854
27902855-- | Run an action affecting the global Spider timeline; this will be guarded by
27912856-- a mutex for that timeline
@@ -2807,8 +2872,10 @@ instance Monad (SpiderHostFrame x) where
28072872 SpiderHostFrame x >> SpiderHostFrame y = SpiderHostFrame $ x >> y
28082873 {-# INLINABLE return #-}
28092874 return x = SpiderHostFrame $ return x
2875+#if !MIN_VERSION_base(4,13,0)
28102876 {-# INLINABLE fail #-}
28112877 fail s = SpiderHostFrame $ fail s
2878+#endif
28122879
28132880instance NotReady (SpiderTimeline x) (SpiderHostFrame x) where
28142881 notReadyUntil _ = pure ()