@@ -9,19 +9,20 @@ import System.Mem.Weak
9import Data.Foldable
9import Data.Foldable
10import Data.Traversable
10import Data.Traversable
11import Control.Monad hiding (mapM, mapM_, forM_, forM, sequence)
11import Control.Monad hiding (mapM, mapM_, forM_, forM, sequence)
12+import Control.Monad.Identity hiding (mapM, mapM_, forM_, forM, sequence)
12import Control.Monad.Reader hiding (mapM, mapM_, forM_, forM, sequence)
13import Control.Monad.Reader hiding (mapM, mapM_, forM_, forM, sequence)
13import GHC.Exts
14import GHC.Exts
14import Control.Applicative -- Unconditionally import, because otherwise it breaks on GHC 7.10.1RC2
15import Control.Applicative -- Unconditionally import, because otherwise it breaks on GHC 7.10.1RC2
15import Data.Dependent.Map (DMap, DSum (..))
16import Data.Dependent.Map (DMap, DSum (..))
16import qualified Data.Dependent.Map as DMap
17import qualified Data.Dependent.Map as DMap
17import Data.GADT.Compare
18import Data.GADT.Compare
18−import Data.Functor.Misc
19import Data.Maybe
19import Data.Maybe
20import Data.IntMap (IntMap)
20import Data.IntMap (IntMap)
21import qualified Data.IntMap as IntMap
21import qualified Data.IntMap as IntMap
22import Control.Monad.Ref
22import Control.Monad.Ref
23import Control.Monad.Exception
23import Control.Monad.Exception
24import Data.Monoid ((<>))
24import Data.Monoid ((<>))
26import System.IO.Unsafe
27import System.IO.Unsafe
27import Unsafe.Coerce
28import Unsafe.Coerce
@@ -139,7 +140,7 @@ scheduleClear r = EventM $ do
139 clears <- asks eventEnvClears
140 clears <- asks eventEnvClears
140 liftIO $ modifyIORef' clears (SomeMaybeIORef r :)
141 liftIO $ modifyIORef' clears (SomeMaybeIORef r :)
142−scheduleRootClear :: IORef (DMap k) -> EventM ()
143+scheduleRootClear :: IORef (DMap k Identity) -> EventM ()
143scheduleRootClear r = EventM $ do
144scheduleRootClear r = EventM $ do
144 clears <- asks eventEnvRootClears
145 clears <- asks eventEnvRootClears
145 liftIO $ modifyIORef' clears (SomeDMapIORef r :)
146 liftIO $ modifyIORef' clears (SomeDMapIORef r :)
@@ -223,8 +224,8 @@ data RootSubscribed a
225data Root (k :: * -> *)
226data Root (k :: * -> *)
226− = Root { rootOccurrence :: !(IORef (DMap k)) -- The currently-firing occurrence of this event
227+ = Root { rootOccurrence :: !(IORef (DMap k Identity)) -- The currently-firing occurrence of this event
227− , rootSubscribed :: !(IORef (DMap (WrapArg RootSubscribed k)))
228+ , rootSubscribed :: !(IORef (DMap k RootSubscribed))
228 , rootInit :: !(forall a. k a -> RootTrigger a -> IO (IO ()))
229 , rootInit :: !(forall a. k a -> RootTrigger a -> IO (IO ()))
@@ -251,12 +252,12 @@ data Push a b
253data MergeSubscribed k
254data MergeSubscribed k
254− = MergeSubscribed { mergeSubscribedOccurrence :: !(IORef (Maybe (DMap k)))
255+ = MergeSubscribed { mergeSubscribedOccurrence :: !(IORef (Maybe (DMap k Identity)))
255− , mergeSubscribedAccum :: !(IORef (DMap k)) -- This will accumulate occurrences until our height is reached, at which point it will be transferred to mergeSubscribedOccurrence
256+ , mergeSubscribedAccum :: !(IORef (DMap k Identity)) -- This will accumulate occurrences until our height is reached, at which point it will be transferred to mergeSubscribedOccurrence
256 , mergeSubscribedHeight :: !(IORef Int)
257 , mergeSubscribedHeight :: !(IORef Int)
257− , mergeSubscribedSubscribers :: !(IORef [WeakSubscriber (DMap k)])
258+ , mergeSubscribedSubscribers :: !(IORef [WeakSubscriber (DMap k Identity)])
258 , mergeSubscribedSelf :: !Any -- Hold all our Subscribers in memory
259 , mergeSubscribedSelf :: !Any -- Hold all our Subscribers in memory
259− , mergeSubscribedParents :: !(DMap (WrapArg EventSubscribed k))
260+ , mergeSubscribedParents :: !(DMap k EventSubscribed)
260#ifdef DEBUG_NODEIDS
261#ifdef DEBUG_NODEIDS
261 , mergeSubscribedNodeId :: Int
262 , mergeSubscribedNodeId :: Int
@@ -264,7 +265,7 @@ data MergeSubscribed k
265--TODO: DMap sucks; we should really write something better (with a functor for the value as well as the key)
266--TODO: DMap sucks; we should really write something better (with a functor for the value as well as the key)
266data Merge k
267data Merge k
267− = Merge { mergeParents :: !(DMap (WrapArg Event k))
268+ = Merge { mergeParents :: !(DMap k Event)
268 , mergeSubscribed :: !(IORef (Maybe (MergeSubscribed k))) --TODO: Can we replace this with an unsafePerformIO thunk?
269 , mergeSubscribed :: !(IORef (Maybe (MergeSubscribed k))) --TODO: Can we replace this with an unsafePerformIO thunk?
@@ -283,16 +284,16 @@ instance GCompare k => GCompare (FanSubscriberKey k) where
283 GGT -> GGT
284 GGT -> GGT
285data FanSubscribed k
286data FanSubscribed k
286− = FanSubscribed { fanSubscribedSubscribers :: !(IORef (DMap (FanSubscriberKey k)))
287+ = FanSubscribed { fanSubscribedSubscribers :: !(IORef (DMap (FanSubscriberKey k) Identity))
287− , fanSubscribedParent :: !(EventSubscribed (DMap k))
288+ , fanSubscribedParent :: !(EventSubscribed (DMap k Identity))
288− , fanSubscribedSelf :: {-# NOUNPACK #-} (Subscriber (DMap k))
289+ , fanSubscribedSelf :: {-# NOUNPACK #-} (Subscriber (DMap k Identity))
289#ifdef DEBUG_NODEIDS
290#ifdef DEBUG_NODEIDS
290 , fanSubscribedNodeId :: Int
291 , fanSubscribedNodeId :: Int
294data Fan k
295data Fan k
295− = Fan { fanParent :: !(Event (DMap k))
296+ = Fan { fanParent :: !(Event (DMap k Identity))
296 , fanSubscribed :: !(IORef (Maybe (FanSubscribed k)))
297 , fanSubscribed :: !(IORef (Maybe (FanSubscribed k)))
@@ -354,7 +355,7 @@ deRefWeakSubscriber ws = case ws of
354data Subscriber a
355data Subscriber a
355 = forall b. SubscriberPush !(a -> EventM (Maybe b)) (PushSubscribed a b)
356 = forall b. SubscriberPush !(a -> EventM (Maybe b)) (PushSubscribed a b)
356 | forall k. GCompare k => SubscriberMerge !(k a) (MergeSubscribed k) --TODO: Can we inline the GCompare?
357 | forall k. GCompare k => SubscriberMerge !(k a) (MergeSubscribed k) --TODO: Can we inline the GCompare?
357− | forall k. (GCompare k, a ~ DMap k) => SubscriberFan (FanSubscribed k)
358+ | forall k. (GCompare k, a ~ DMap k Identity) => SubscriberFan (FanSubscribed k)
358 | SubscriberHold !(Hold a)
359 | SubscriberHold !(Hold a)
359 | SubscriberSwitch (SwitchSubscribed a)
360 | SubscriberSwitch (SwitchSubscribed a)
360 | forall b. a ~ Event b => SubscriberCoincidenceOuter (CoincidenceSubscribed b)
361 | forall b. a ~ Event b => SubscriberCoincidenceOuter (CoincidenceSubscribed b)
@@ -374,7 +375,7 @@ data Event a
374 = forall k. GCompare k => EventRoot !(k a) !(Root k)
375 = forall k. GCompare k => EventRoot !(k a) !(Root k)
375 | EventNever
376 | EventNever
376 | forall b. EventPush !(Push b a)
377 | forall b. EventPush !(Push b a)
377− | forall k. (GCompare k, a ~ DMap k) => EventMerge !(Merge k)
378+ | forall k. (GCompare k, a ~ DMap k Identity) => EventMerge !(Merge k)
378 | forall k. GCompare k => EventFan !(k a) !(Fan k)
379 | forall k. GCompare k => EventFan !(k a) !(Fan k)
379 | EventSwitch !(Switch a)
380 | EventSwitch !(Switch a)
380 | EventCoincidence !(Coincidence a)
381 | EventCoincidence !(Coincidence a)
@@ -393,7 +394,7 @@ data EventSubscribed a
393 = EventSubscribedRoot {-# NOUNPACK #-} (RootSubscribed a)
394 = EventSubscribedRoot {-# NOUNPACK #-} (RootSubscribed a)
394 | EventSubscribedNever
395 | EventSubscribedNever
395 | forall b. EventSubscribedPush !(PushSubscribed b a)
396 | forall b. EventSubscribedPush !(PushSubscribed b a)
396− | forall k. (GCompare k, a ~ DMap k) => EventSubscribedMerge !(MergeSubscribed k)
397+ | forall k. (GCompare k, a ~ DMap k Identity) => EventSubscribedMerge !(MergeSubscribed k)
397 | forall k. GCompare k => EventSubscribedFan !(k a) !(FanSubscribed k)
398 | forall k. GCompare k => EventSubscribedFan !(k a) !(FanSubscribed k)
398 | EventSubscribedSwitch !(SwitchSubscribed a)
399 | EventSubscribedSwitch !(SwitchSubscribed a)
399 | EventSubscribedCoincidence !(CoincidenceSubscribed a)
400 | EventSubscribedCoincidence !(CoincidenceSubscribed a)
@@ -424,7 +425,7 @@ newSubscriberHold :: Hold a -> IO (Subscriber a)
424newSubscriberHold h = return $! SubscriberHold h
425newSubscriberHold h = return $! SubscriberHold h
426{-# NOINLINE newSubscriberFan #-}
427{-# NOINLINE newSubscriberFan #-}
427−newSubscriberFan :: GCompare k => FanSubscribed k -> IO (Subscriber (DMap k))
428+newSubscriberFan :: GCompare k => FanSubscribed k -> IO (Subscriber (DMap k Identity))
428newSubscriberFan subd = return $! SubscriberFan subd
429newSubscriberFan subd = return $! SubscriberFan subd
430{-# NOINLINE newSubscriberSwitch #-}
431{-# NOINLINE newSubscriberSwitch #-}
@@ -519,7 +520,7 @@ propagateAndUpdateSubscribersRef subscribersRef a = do
519 liftIO $ modifyIORef' subscribersRef (++stillAlive)
520 liftIO $ modifyIORef' subscribersRef (++stillAlive)
521-- Propagate the given event occurrence; before cleaning up, run the given action, which may read the state of events and behaviors
522-- Propagate the given event occurrence; before cleaning up, run the given action, which may read the state of events and behaviors
522−run :: [DSum RootTrigger] -> ResultM b -> IO b
523+run :: [DSum RootTrigger Identity] -> ResultM b -> IO b
523run roots after = do
524run roots after = do
524 when debugPropagate $ putStrLn "Running an event frame"
525 when debugPropagate $ putStrLn "Running an event frame"
525 result <- runFrame $ do
526 result <- runFrame $ do
@@ -532,7 +533,7 @@ run roots after = do
532 then do scheduleRootClear occRef
533 then do scheduleRootClear occRef
533 return $ Just r
534 return $ Just r
534 else return Nothing
535 else return Nothing
535− forM_ (catMaybes rootsToPropagate) $ \(RootTrigger (subscribersRef, _, _) :=> a) -> do
536+ forM_ (catMaybes rootsToPropagate) $ \(RootTrigger (subscribersRef, _, _) :=> Identity a) -> do
536 propagateAndUpdateSubscribersRef subscribersRef a
537 propagateAndUpdateSubscribersRef subscribersRef a
537 delayedRef <- EventM $ asks eventEnvDelayedMerges
538 delayedRef <- EventM $ asks eventEnvDelayedMerges
538 let go = do
539 let go = do
@@ -565,7 +566,7 @@ run roots after = do
566data SomeMaybeIORef = forall a. SomeMaybeIORef (IORef (Maybe a))
567data SomeMaybeIORef = forall a. SomeMaybeIORef (IORef (Maybe a))
568−data SomeDMapIORef = forall k. SomeDMapIORef (IORef (DMap k))
569+data SomeDMapIORef = forall k. SomeDMapIORef (IORef (DMap k Identity))
570data SomeAssignment = forall a. SomeAssignment (Hold a) a
571data SomeAssignment = forall a. SomeAssignment (Hold a) a
@@ -599,7 +600,7 @@ traverseAndCleanWeakList_ deRef ws f = go ws
599propagate :: a -> [WeakSubscriber a] -> EventM [WeakSubscriber a]
600propagate :: a -> [WeakSubscriber a] -> EventM [WeakSubscriber a]
600propagate a subscribers = do
601propagate a subscribers = do
601 traverseAndCleanWeakList_ (liftIO . deRefWeakSubscriber) subscribers $ \s -> case s of
602 traverseAndCleanWeakList_ (liftIO . deRefWeakSubscriber) subscribers $ \s -> case s of
602− SubscriberPush compute subscribed -> do
603+ SubscriberPush compute subscribed -> {-# SCC "pg->SubscriberPush" #-} do
603 when debugPropagate $ liftIO $ putStrLn $ "SubscriberPush" <> showNodeId subscribed
604 when debugPropagate $ liftIO $ putStrLn $ "SubscriberPush" <> showNodeId subscribed
604 occ <- compute a
605 occ <- compute a
605 case occ of
606 case occ of
@@ -608,44 +609,46 @@ propagate a subscribers = do
608 liftIO $ writeIORef (pushSubscribedOccurrence subscribed) occ
609 liftIO $ writeIORef (pushSubscribedOccurrence subscribed) occ
609 scheduleClear $ pushSubscribedOccurrence subscribed
610 scheduleClear $ pushSubscribedOccurrence subscribed
610 liftIO . writeIORef (pushSubscribedSubscribers subscribed) =<< propagate o =<< liftIO (readIORef (pushSubscribedSubscribers subscribed))
611 liftIO . writeIORef (pushSubscribedSubscribers subscribed) =<< propagate o =<< liftIO (readIORef (pushSubscribedSubscribers subscribed))
611− SubscriberMerge k subscribed -> do
612+ SubscriberMerge k subscribed -> {-# SCC "pg->SubscriberMerge" #-} do
612 when debugPropagate $ liftIO $ putStrLn $ "SubscriberMerge" <> showNodeId subscribed
613 when debugPropagate $ liftIO $ putStrLn $ "SubscriberMerge" <> showNodeId subscribed
613 oldM <- liftIO $ readIORef $ mergeSubscribedAccum subscribed
614 oldM <- liftIO $ readIORef $ mergeSubscribedAccum subscribed
614− liftIO $ writeIORef (mergeSubscribedAccum subscribed) $ DMap.insertWith (error "Same key fired multiple times for") k a oldM
615+ liftIO $ writeIORef (mergeSubscribedAccum subscribed) $ {-# SCC "pg->sm->insertWith" #-} DMap.insertWith (error "Same key fired multiple times for") k (Identity a) oldM
615 when (DMap.null oldM) $ do -- Only schedule the firing once
616 when (DMap.null oldM) $ do -- Only schedule the firing once
616 height <- liftIO $ readIORef $ mergeSubscribedHeight subscribed
617 height <- liftIO $ readIORef $ mergeSubscribedHeight subscribed
617 --TODO: assertions about height
618 --TODO: assertions about height
618− currentHeight <- getCurrentHeight
619+ currentHeight <- {-# SCC "getCurrentHeight" #-} getCurrentHeight
619 when (height <= currentHeight) $ error $ "Height (" ++ show height ++ ") is not greater than current height (" ++ show currentHeight ++ ")"
620 when (height <= currentHeight) $ error $ "Height (" ++ show height ++ ") is not greater than current height (" ++ show currentHeight ++ ")"
620− scheduleMerge height subscribed
621+ {-# SCC "scheduleMerge" #-} scheduleMerge height subscribed
621− SubscriberFan subscribed -> do
622+ SubscriberFan subscribed -> {-# SCC "pg->SubscriberFan" #-} do
622 subs <- liftIO $ readIORef $ fanSubscribedSubscribers subscribed
623 subs <- liftIO $ readIORef $ fanSubscribedSubscribers subscribed
623 when debugPropagate $ liftIO $ putStrLn $ "SubscriberFan" <> showNodeId subscribed <> ": " ++ show (DMap.size subs) ++ " keys subscribed, " ++ show (DMap.size a) ++ " keys firing"
624 when debugPropagate $ liftIO $ putStrLn $ "SubscriberFan" <> showNodeId subscribed <> ": " ++ show (DMap.size subs) ++ " keys subscribed, " ++ show (DMap.size a) ++ " keys firing"
624 --TODO: We need a better DMap intersection; here, we are assuming that the number of firing keys is small and the number of subscribers is large
625 --TODO: We need a better DMap intersection; here, we are assuming that the number of firing keys is small and the number of subscribers is large
625− forM_ (DMap.toList a) $ \(k :=> v) -> case DMap.lookup (FanSubscriberKey k) subs of
626+ forM_ (DMap.toList a) $ \(k :=> Identity v) -> case DMap.lookup (FanSubscriberKey k) subs of
626 Nothing -> do
627 Nothing -> do
627 when debugPropagate $ liftIO $ putStrLn "No subscriber for key"
628 when debugPropagate $ liftIO $ putStrLn "No subscriber for key"
628 return ()
629 return ()
629− Just subsubs -> do
630+ Just (Identity subsubs) -> do
630 _ <- propagate v subsubs --TODO: use the value of this
631 _ <- propagate v subsubs --TODO: use the value of this
631 return ()
632 return ()
632 --TODO: The following is way too slow to do all the time
633 --TODO: The following is way too slow to do all the time
633− subs' <- liftIO $ forM (DMap.toList subs) $ ((\(FanSubscriberKey k :=> subsubs) -> do
634+ subs' <- liftIO $ forM (DMap.toList subs) $ ((\(FanSubscriberKey k :=> Identity subsubs) -> do
634 subsubs' <- traverseAndCleanWeakList_ (liftIO . deRefWeakSubscriber) subsubs (const $ return ())
635 subsubs' <- traverseAndCleanWeakList_ (liftIO . deRefWeakSubscriber) subsubs (const $ return ())
636+ return $ if null subsubs'
635− return $ if null subsubs' then Nothing else Just $ FanSubscriberKey k :=> subsubs') :: DSum (FanSubscriberKey k) -> IO (Maybe (DSum (FanSubscriberKey k))))
638+ else Just $ FanSubscriberKey k :=> Identity subsubs') :: DSum (FanSubscriberKey k) Identity -> IO (Maybe (DSum (FanSubscriberKey k) Identity)))
636 liftIO $ writeIORef (fanSubscribedSubscribers subscribed) $ DMap.fromDistinctAscList $ catMaybes subs'
639 liftIO $ writeIORef (fanSubscribedSubscribers subscribed) $ DMap.fromDistinctAscList $ catMaybes subs'
637− SubscriberHold h -> do
640+ SubscriberHold h -> {-# SCC "pg->SubscriberHold" #-} do
638 invalidators <- liftIO $ readIORef $ holdInvalidators h
641 invalidators <- liftIO $ readIORef $ holdInvalidators h
639 when debugPropagate $ liftIO $ putStrLn $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
642 when debugPropagate $ liftIO $ putStrLn $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
640 toAssignRef <- askToAssignRef
643 toAssignRef <- askToAssignRef
641 liftIO $ modifyIORef' toAssignRef (SomeAssignment h a :)
644 liftIO $ modifyIORef' toAssignRef (SomeAssignment h a :)
642− SubscriberSwitch subscribed -> do
645+ SubscriberSwitch subscribed -> {-# SCC "pg->SubscriberSwitch" #-} do
643 when debugPropagate $ liftIO $ putStrLn $ "SubscriberSwitch" <> showNodeId subscribed
646 when debugPropagate $ liftIO $ putStrLn $ "SubscriberSwitch" <> showNodeId subscribed
644 liftIO $ writeIORef (switchSubscribedOccurrence subscribed) $ Just a
647 liftIO $ writeIORef (switchSubscribedOccurrence subscribed) $ Just a
645 scheduleClear $ switchSubscribedOccurrence subscribed
648 scheduleClear $ switchSubscribedOccurrence subscribed
646 subs <- liftIO $ readIORef $ switchSubscribedSubscribers subscribed
649 subs <- liftIO $ readIORef $ switchSubscribedSubscribers subscribed
647 liftIO . writeIORef (switchSubscribedSubscribers subscribed) =<< propagate a subs
650 liftIO . writeIORef (switchSubscribedSubscribers subscribed) =<< propagate a subs
648− SubscriberCoincidenceOuter subscribed -> do
651+ SubscriberCoincidenceOuter subscribed -> {-# SCC "pg->SubscriberCoincidenceOuter" #-} do
649 when debugPropagate $ liftIO $ putStrLn $ "SubscriberCoincidenceOuter" <> showNodeId subscribed
652 when debugPropagate $ liftIO $ putStrLn $ "SubscriberCoincidenceOuter" <> showNodeId subscribed
650 outerHeight <- liftIO $ readIORef $ coincidenceSubscribedHeight subscribed
653 outerHeight <- liftIO $ readIORef $ coincidenceSubscribedHeight subscribed
651 when debugPropagate $ liftIO $ putStrLn $ " outerHeight = " <> show outerHeight
654 when debugPropagate $ liftIO $ putStrLn $ " outerHeight = " <> show outerHeight
@@ -664,7 +667,7 @@ propagate a subscribers = do
664 liftIO $ writeIORef (coincidenceSubscribedOccurrence subscribed) occ
667 liftIO $ writeIORef (coincidenceSubscribedOccurrence subscribed) occ
665 scheduleClear $ coincidenceSubscribedOccurrence subscribed
668 scheduleClear $ coincidenceSubscribedOccurrence subscribed
666 liftIO . writeIORef (coincidenceSubscribedSubscribers subscribed) =<< propagate o =<< liftIO (readIORef (coincidenceSubscribedSubscribers subscribed))
669 liftIO . writeIORef (coincidenceSubscribedSubscribers subscribed) =<< propagate o =<< liftIO (readIORef (coincidenceSubscribedSubscribers subscribed))
667− SubscriberCoincidenceInner subscribed -> do
670+ SubscriberCoincidenceInner subscribed -> {-# SCC "pg->SubscriberCoincidenceInner" #-} do
668 when debugPropagate $ liftIO $ putStrLn $ "SubscriberCoincidenceInner" <> showNodeId subscribed
671 when debugPropagate $ liftIO $ putStrLn $ "SubscriberCoincidenceInner" <> showNodeId subscribed
669 liftIO $ writeIORef (coincidenceSubscribedOccurrence subscribed) $ Just a
672 liftIO $ writeIORef (coincidenceSubscribedOccurrence subscribed) $ Just a
670 scheduleClear $ coincidenceSubscribedOccurrence subscribed
673 scheduleClear $ coincidenceSubscribedOccurrence subscribed
@@ -731,7 +734,7 @@ readBehaviorTracked b = case b of
732readEvent :: Event a -> ResultM (Maybe a)
735readEvent :: Event a -> ResultM (Maybe a)
733readEvent e = case e of
736readEvent e = case e of
734− EventRoot k r -> liftIO $ liftM (DMap.lookup k) $ readIORef $ rootOccurrence r
737+ EventRoot k r -> liftIO . liftM (coerce . DMap.lookup k) . readIORef $ rootOccurrence r
735 EventNever -> return Nothing
738 EventNever -> return Nothing
736 EventPush p -> do
739 EventPush p -> do
737 subscribed <- getPushSubscribed p
740 subscribed <- getPushSubscribed p
@@ -747,7 +750,7 @@ readEvent e = case e of
747 return result
750 return result
748 EventFan k f -> do
751 EventFan k f -> do
749 parentOcc <- readEvent $ fanParent f
752 parentOcc <- readEvent $ fanParent f
750− return $ DMap.lookup k =<< parentOcc
753+ return . coerce $ DMap.lookup k =<< parentOcc
751 EventSwitch s -> do
754 EventSwitch s -> do
752 subscribed <- getSwitchSubscribed s
755 subscribed <- getSwitchSubscribed s
753 liftIO $ do
756 liftIO $ do
@@ -794,7 +797,7 @@ subscribeEventSubscribed es ws = case es of
794 modifyIORef' (pushSubscribedSubscribers subscribed) (ws:)
797 modifyIORef' (pushSubscribedSubscribers subscribed) (ws:)
795 EventSubscribedFan k subscribed -> do
798 EventSubscribedFan k subscribed -> do
796 when debugSubscribe $ liftIO $ putStrLn $ "subscribeEventSubscribed Fan"
799 when debugSubscribe $ liftIO $ putStrLn $ "subscribeEventSubscribed Fan"
797− modifyIORef' (fanSubscribedSubscribers subscribed) $ DMap.insertWith (++) (FanSubscriberKey k) [ws]
800+ modifyIORef' (fanSubscribedSubscribers subscribed) $ DMap.insertWith (liftA2 (++)) (FanSubscriberKey k) (Identity [ws])
798 EventSubscribedMerge subscribed -> do
801 EventSubscribedMerge subscribed -> do
799 when debugSubscribe $ liftIO $ putStrLn $ "subscribeEventSubscribed Merge"
802 when debugSubscribe $ liftIO $ putStrLn $ "subscribeEventSubscribed Merge"
800 modifyIORef' (mergeSubscribedSubscribers subscribed) (ws:)
803 modifyIORef' (mergeSubscribedSubscribers subscribed) (ws:)
@@ -812,7 +815,7 @@ getEventSubscribedOcc es = case es of
812 EventSubscribedPush subscribed -> readIORef $ pushSubscribedOccurrence subscribed
815 EventSubscribedPush subscribed -> readIORef $ pushSubscribedOccurrence subscribed
813 EventSubscribedFan k subscribed -> do
816 EventSubscribedFan k subscribed -> do
814 parentOcc <- getEventSubscribedOcc $ fanSubscribedParent subscribed
817 parentOcc <- getEventSubscribedOcc $ fanSubscribedParent subscribed
815− let occ = DMap.lookup k =<< parentOcc
818+ let occ = coerce $ DMap.lookup k =<< parentOcc
816 return occ
819 return occ
817 EventSubscribedMerge subscribed -> readIORef $ mergeSubscribedOccurrence subscribed
820 EventSubscribedMerge subscribed -> readIORef $ mergeSubscribedOccurrence subscribed
818 EventSubscribedSwitch subscribed -> readIORef $ switchSubscribedOccurrence subscribed
821 EventSubscribedSwitch subscribed -> readIORef $ switchSubscribedOccurrence subscribed
@@ -841,18 +844,18 @@ noinlineFalse = False
841getRootSubscribed :: GCompare k => k a -> Root k -> EventM (RootSubscribed a)
844getRootSubscribed :: GCompare k => k a -> Root k -> EventM (RootSubscribed a)
842getRootSubscribed k r = do
845getRootSubscribed k r = do
843 mSubscribed <- liftIO $ readIORef $ rootSubscribed r
846 mSubscribed <- liftIO $ readIORef $ rootSubscribed r
844− case DMap.lookup (WrapArg k) mSubscribed of
847+ case DMap.lookup k mSubscribed of
845 Just subscribed -> return subscribed
848 Just subscribed -> return subscribed
846 Nothing -> liftIO $ do
849 Nothing -> liftIO $ do
847 subscribersRef <- newIORef []
850 subscribersRef <- newIORef []
848− subscribed <- newRootSubscribed (liftM (DMap.lookup k) $ readIORef $ rootOccurrence r) subscribersRef
851+ subscribed <- newRootSubscribed (liftM (coerce . DMap.lookup k) $ readIORef $ rootOccurrence r) subscribersRef
849 -- Strangely, init needs the same stuff as a RootSubscribed has, but it must not be the same as the one that everyone's subscribing to, or it'll leak memory
852 -- Strangely, init needs the same stuff as a RootSubscribed has, but it must not be the same as the one that everyone's subscribing to, or it'll leak memory
850 uninit <- rootInit r k $ RootTrigger (subscribersRef, rootOccurrence r, k)
853 uninit <- rootInit r k $ RootTrigger (subscribersRef, rootOccurrence r, k)
851 addFinalizer subscribed $ do
854 addFinalizer subscribed $ do
852 when noinlineFalse $ putStr "" -- For some reason, without this line, the finalizer will run earlier than it should
855 when noinlineFalse $ putStr "" -- For some reason, without this line, the finalizer will run earlier than it should
853-- putStrLn "Uninit root"
856-- putStrLn "Uninit root"
855− liftIO $ modifyIORef' (rootSubscribed r) $ DMap.insert (WrapArg k) subscribed
858+ liftIO $ modifyIORef' (rootSubscribed r) $ DMap.insert k subscribed
856 return subscribed
859 return subscribed
858-- When getPushSubscribed returns, the PushSubscribed returned will have a fully filled-in
861-- When getPushSubscribed returns, the PushSubscribed returned will have a fully filled-in
@@ -894,11 +897,11 @@ getMergeSubscribed m = {-# SCC "getMergeSubscribed.entire" #-} do
894 subscribedUnsafe <- liftIO $ unsafeInterleaveIO $ readIORef subscribedRef
897 subscribedUnsafe <- liftIO $ unsafeInterleaveIO $ readIORef subscribedRef
895 s <- liftIO $ newBox subscribedUnsafe
898 s <- liftIO $ newBox subscribedUnsafe
896 ws <- liftIO $ mkWeakPtrWithDebug s "SubscriberMerge"
899 ws <- liftIO $ mkWeakPtrWithDebug s "SubscriberMerge"
897− subscribers :: [(Any, Maybe (DSum k), Int, DSum (WrapArg EventSubscribed k))] <- forM (DMap.toList $ mergeParents m) $ {-# SCC "getMergeSubscribed.a" #-} \(WrapArg k :=> e) -> {-# SCC "getMergeSubscribed.a1" #-} do
900+ subscribers :: [(Any, Maybe (DSum k Identity), Int, DSum k EventSubscribed)] <- forM (DMap.toList $ mergeParents m) $ {-# SCC "getMergeSubscribed.a" #-} \(k :=> e) -> {-# SCC "getMergeSubscribed.a1" #-} do
898 parentSubd <- {-# SCC "getMergeSubscribed.a.parentSubd" #-} subscribe e $ WeakSubscriberMerge k ws
901 parentSubd <- {-# SCC "getMergeSubscribed.a.parentSubd" #-} subscribe e $ WeakSubscriberMerge k ws
899 parentOcc <- {-# SCC "getMergeSubscribed.a.parentOcc" #-} liftIO $ getEventSubscribedOcc parentSubd
902 parentOcc <- {-# SCC "getMergeSubscribed.a.parentOcc" #-} liftIO $ getEventSubscribedOcc parentSubd
900 height <- {-# SCC "getMergeSubscribed.a.height" #-} liftIO $ readIORef $ eventSubscribedHeightRef parentSubd
903 height <- {-# SCC "getMergeSubscribed.a.height" #-} liftIO $ readIORef $ eventSubscribedHeightRef parentSubd
901− return $ {-# SCC "getMergeSubscribed.a.returnVal" #-} (unsafeCoerce s :: Any, fmap (k :=>) parentOcc, height, WrapArg k :=> parentSubd)
904+ return $ {-# SCC "getMergeSubscribed.a.returnVal" #-} (unsafeCoerce s :: Any, fmap (\x -> k :=> Identity x) parentOcc, height, k :=> parentSubd)
902 let dm = DMap.fromDistinctAscList $ catMaybes $ map (\(_, x, _, _) -> x) subscribers
905 let dm = DMap.fromDistinctAscList $ catMaybes $ map (\(_, x, _, _) -> x) subscribers
903 subscriberHeights = map (\(_, _, x, _) -> x) subscribers
906 subscriberHeights = map (\(_, _, x, _) -> x) subscribers
904 myHeight =
907 myHeight =
@@ -1048,7 +1051,7 @@ getCoincidenceSubscribed c = do
1048 liftIO $ writeIORef (coincidenceSubscribed c) $ Just subscribed
1051 liftIO $ writeIORef (coincidenceSubscribed c) $ Just subscribed
1049 return subscribed
1052 return subscribed
1051−merge :: GCompare k => DMap (WrapArg Event k) -> Event (DMap k)
1054+merge :: GCompare k => DMap k Event -> Event (DMap k Identity)
1052merge m = EventMerge $ Merge
1055merge m = EventMerge $ Merge
1053 { mergeParents = m
1056 { mergeParents = m
1054 , mergeSubscribed = unsafeNewIORef m Nothing
1057 , mergeSubscribed = unsafeNewIORef m Nothing
@@ -1056,7 +1059,7 @@ merge m = EventMerge $ Merge
1057newtype EventSelector k = EventSelector { select :: forall a. k a -> Event a }
1060newtype EventSelector k = EventSelector { select :: forall a. k a -> Event a }
1059−fan :: GCompare k => Event (DMap k) -> EventSelector k
1062+fan :: GCompare k => Event (DMap k Identity) -> EventSelector k
1061 let f = Fan
1064 let f = Fan
1062 { fanParent = e
1065 { fanParent = e
@@ -1154,7 +1157,7 @@ invalidateSubscriberHeight ws = do
1154 SubscriberFan subscribed -> do
1157 SubscriberFan subscribed -> do
1155 when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberFan" <> showNodeId subscribed
1158 when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberFan" <> showNodeId subscribed
1156 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1159 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1157− forM_ (DMap.toList subscribers) $ ((\(FanSubscriberKey _ :=> v) -> mapM_ invalidateSubscriberHeight v) :: DSum (FanSubscriberKey k) -> IO ())
1160+ forM_ (DMap.toList subscribers) $ ((\(FanSubscriberKey _ :=> Identity v) -> mapM_ invalidateSubscriberHeight v) :: DSum (FanSubscriberKey k) Identity -> IO ())
1158 when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberFan" <> showNodeId subscribed <> " done"
1161 when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberFan" <> showNodeId subscribed <> " done"
1159 SubscriberHold _ -> return ()
1162 SubscriberHold _ -> return ()
1160 SubscriberSwitch subscribed -> do
1163 SubscriberSwitch subscribed -> do
@@ -1205,7 +1208,7 @@ recalculateSubscriberHeight ws = do
1205 SubscriberFan subscribed -> do
1208 SubscriberFan subscribed -> do
1206 when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberFan" <> showNodeId subscribed
1209 when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberFan" <> showNodeId subscribed
1207 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1210 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1208− forM_ (DMap.toList subscribers) $ ((\(FanSubscriberKey _ :=> v) -> mapM_ recalculateSubscriberHeight v) :: DSum (FanSubscriberKey k) -> IO ())
1211+ forM_ (DMap.toList subscribers) $ ((\(FanSubscriberKey _ :=> Identity v) -> mapM_ recalculateSubscriberHeight v) :: DSum (FanSubscriberKey k) Identity -> IO ())
1209 when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberFan" <> showNodeId subscribed <> " done"
1212 when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberFan" <> showNodeId subscribed <> " done"
1210 SubscriberHold _ -> return ()
1213 SubscriberHold _ -> return ()
1211 SubscriberSwitch subscribed -> do
1214 SubscriberSwitch subscribed -> do
@@ -1237,7 +1240,7 @@ recalculateCoincidenceHeight subscribed = do
1238calculateMergeHeight :: MergeSubscribed k -> IO Int
1241calculateMergeHeight :: MergeSubscribed k -> IO Int
1239calculateMergeHeight subscribed = if DMap.null (mergeSubscribedParents subscribed) then return 0 else do
1242calculateMergeHeight subscribed = if DMap.null (mergeSubscribedParents subscribed) then return 0 else do
1240− heights <- forM (DMap.toList $ mergeSubscribedParents subscribed) $ \(WrapArg _ :=> es) -> do
1243+ heights <- forM (DMap.toList $ mergeSubscribedParents subscribed) $ \(_ :=> es) -> do
1241 readIORef $ eventSubscribedHeightRef es
1244 readIORef $ eventSubscribedHeightRef es
1242 return $ if any (== invalidHeight) heights then invalidHeight else succ $ Prelude.maximum heights --TODO: Replace 'any' with invalidHeight-preserving 'maximum'
1245 return $ if any (== invalidHeight) heights then invalidHeight else succ $ Prelude.maximum heights --TODO: Replace 'any' with invalidHeight-preserving 'maximum'
@@ -1316,7 +1319,7 @@ instance R.Reflex Spider where
1316 constant = SpiderBehavior . BehaviorConst
1319 constant = SpiderBehavior . BehaviorConst
1317 push f = SpiderEvent. push f . unSpiderEvent
1320 push f = SpiderEvent. push f . unSpiderEvent
1318 pull = SpiderBehavior . pull
1321 pull = SpiderBehavior . pull
1319− merge = SpiderEvent . merge . (unsafeCoerce :: DMap (WrapArg (R.Event Spider) k) -> DMap (WrapArg Event k))
1322+ merge = SpiderEvent . merge . (unsafeCoerce :: DMap k (R.Event Spider) -> DMap k Event)
1320 fan e = R.EventSelector $ SpiderEvent . select (fan (unSpiderEvent e))
1323 fan e = R.EventSelector $ SpiderEvent . select (fan (unSpiderEvent e))
1321 switch = SpiderEvent . switch . (unsafeCoerce :: Behavior (R.Event Spider a) -> Behavior (Event a)) . unSpiderBehavior
1324 switch = SpiderEvent . switch . (unsafeCoerce :: Behavior (R.Event Spider a) -> Behavior (Event a)) . unSpiderBehavior
1322 coincidence = SpiderEvent . coincidence . (unsafeCoerce :: Event (R.Event Spider a) -> Event (Event a)) . unSpiderEvent
1325 coincidence = SpiderEvent . coincidence . (unsafeCoerce :: Event (R.Event Spider a) -> Event (Event a)) . unSpiderEvent
@@ -1340,7 +1343,7 @@ instance R.MonadHold Spider EventM where
1340 {-# INLINE hold #-}
1343 {-# INLINE hold #-}
1341 hold v0 e = SpiderBehavior <$> hold v0 (unSpiderEvent e)
1344 hold v0 e = SpiderBehavior <$> hold v0 (unSpiderEvent e)
1343−data RootTrigger a = forall k. GCompare k => RootTrigger (IORef [WeakSubscriber a], IORef (DMap k), k a)
1346+data RootTrigger a = forall k. GCompare k => RootTrigger (IORef [WeakSubscriber a], IORef (DMap k Identity), k a)
1344newtype SpiderEventHandle a = SpiderEventHandle { unEventHandle :: Event a }
1347newtype SpiderEventHandle a = SpiderEventHandle { unEventHandle :: Event a }
1346instance R.MonadSubscribeEvent Spider SpiderHostFrame where
1349instance R.MonadSubscribeEvent Spider SpiderHostFrame where