@@ -9,7 +9,6 @@
9{-# LANGUAGE GADTs #-}
9{-# LANGUAGE GADTs #-}
10{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10{-# LANGUAGE GeneralizedNewtypeDeriving #-}
11{-# LANGUAGE LambdaCase #-}
11{-# LANGUAGE LambdaCase #-}
12−{-# LANGUAGE MultiParamTypeClasses #-}
13{-# LANGUAGE MultiWayIf #-}
12{-# LANGUAGE MultiWayIf #-}
14{-# LANGUAGE RankNTypes #-}
13{-# LANGUAGE RankNTypes #-}
15{-# LANGUAGE RoleAnnotations #-}
14{-# LANGUAGE RoleAnnotations #-}
@@ -17,9 +16,15 @@
17{-# LANGUAGE TypeFamilies #-}
16{-# LANGUAGE TypeFamilies #-}
18{-# LANGUAGE TypeOperators #-}
17{-# LANGUAGE TypeOperators #-}
19{-# LANGUAGE UndecidableInstances #-}
18{-# LANGUAGE UndecidableInstances #-}
19+{-# LANGUAGE PolyKinds #-}
20+{-# LANGUAGE DeriveFunctor #-}
21+{-# LANGUAGE StandaloneDeriving #-}
22+{-# LANGUAGE InstanceSigs #-}
20#ifdef USE_REFLEX_OPTIMIZER
24#ifdef USE_REFLEX_OPTIMIZER
21{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
25{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
27+{-# OPTIONS_GHC -Wunused-binds #-}
23-- | This module is the implementation of the 'Spider' 'Reflex' engine. It uses
28-- | This module is the implementation of the 'Spider' 'Reflex' engine. It uses
24-- a graph traversal algorithm to propagate 'Event's and 'Behavior's.
29-- a graph traversal algorithm to propagate 'Event's and 'Behavior's.
25module Reflex.Spider.Internal (module Reflex.Spider.Internal) where
30module Reflex.Spider.Internal (module Reflex.Spider.Internal) where
@@ -33,7 +38,10 @@ import Control.Monad hiding (forM, forM_, mapM, mapM_)
33import Control.Monad.Exception
38import Control.Monad.Exception
34import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
39import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
35import Control.Monad.Primitive
40import Control.Monad.Primitive
36−import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_)
41+--import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_)
42+import Control.Monad.Reader.Class
43+import Control.Monad.IO.Class
44+import Control.Monad.ReaderIO
37import Control.Monad.Ref
45import Control.Monad.Ref
38import Data.Align
46import Data.Align
39import Data.Coerce
47import Data.Coerce
@@ -49,6 +57,7 @@ import Data.GADT.Compare
49import Data.IntMap.Strict (IntMap)
57import Data.IntMap.Strict (IntMap)
50import qualified Data.IntMap.Strict as IntMap
58import qualified Data.IntMap.Strict as IntMap
51import Data.IORef
59import Data.IORef
60+import Data.Kind (Type)
52import Data.Maybe hiding (mapMaybe)
61import Data.Maybe hiding (mapMaybe)
53import Data.Monoid ((<>))
62import Data.Monoid ((<>))
54import Data.Proxy
63import Data.Proxy
@@ -63,6 +72,15 @@ import System.IO.Unsafe
63import System.Mem.Weak
72import System.Mem.Weak
64import Unsafe.Coerce
73import Unsafe.Coerce
75+#ifdef MIN_VERSION_semialign
76+#if MIN_VERSION_these(0,8,0)
77+import Data.These.Combinators (justThese)
79+#if MIN_VERSION_semialign(1,1,0)
80+import Data.Zip (Zip (..))
66#ifdef DEBUG_CYCLES
84#ifdef DEBUG_CYCLES
67import Control.Monad.State hiding (forM, forM_, mapM, mapM_, sequence)
85import Control.Monad.State hiding (forM, forM_, mapM, mapM_, sequence)
68import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
86import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
@@ -76,6 +94,7 @@ import qualified Data.FastWeakBag as FastWeakBag
76import Data.Reflection
94import Data.Reflection
77import Data.Some (Some(Some))
95import Data.Some (Some(Some))
78import Data.Type.Coercion
96import Data.Type.Coercion
97+import Data.Profunctor.Unsafe ((#.), (.#))
79import Data.WeakBag (WeakBag, WeakBagTicket, _weakBag_children)
98import Data.WeakBag (WeakBag, WeakBagTicket, _weakBag_children)
80import qualified Data.WeakBag as WeakBag
99import qualified Data.WeakBag as WeakBag
81import qualified Reflex.Class
100import qualified Reflex.Class
@@ -192,12 +211,6 @@ nextNodeIdRef = unsafePerformIO $ newIORef 1
193newNodeId :: IO Int
212newNodeId :: IO Int
194newNodeId = atomicModifyIORef' nextNodeIdRef $ \n -> (succ n, n)
213newNodeId = atomicModifyIORef' nextNodeIdRef $ \n -> (succ n, n)
196−{-# NOINLINE unsafeNodeId #-}
197−unsafeNodeId :: a -> Int
198−unsafeNodeId a = unsafePerformIO $ do
203--------------------------------------------------------------------------------
216--------------------------------------------------------------------------------
@@ -301,9 +314,10 @@ cacheEvent e =
317+ unsafePerformIO $ do
304− let mSubscribedRef :: IORef (FastWeak (CacheSubscribed x a))
318+ mSubscribedRef :: IORef (FastWeak (CacheSubscribed x a))
305− !mSubscribedRef = unsafeNewIORef e emptyFastWeak
319+ <- newIORef emptyFastWeak
306− in \sub -> {-# SCC "cacheEvent" #-} do
320+ pure $ \sub -> {-# SCC "cacheEvent" #-} do
307#ifdef DEBUG_TRACE_EVENTS
321#ifdef DEBUG_TRACE_EVENTS
308 unless (BS8.null callSite) $ liftIO $ BS8.hPutStrLn stderr callSite
322 unless (BS8.null callSite) $ liftIO $ BS8.hPutStrLn stderr callSite
@@ -318,9 +332,9 @@ cacheEvent e =
318 scheduleClear occRef
332 scheduleClear occRef
319 propagateFast a subscribers
333 propagateFast a subscribers
320 , subscriberInvalidateHeight = \old -> do
334 , subscriberInvalidateHeight = \old -> do
321− FastWeakBag.traverse subscribers $ invalidateSubscriberHeight old
335+ FastWeakBag.traverse_ subscribers $ invalidateSubscriberHeight old
322 , subscriberRecalculateHeight = \new -> do
336 , subscriberRecalculateHeight = \new -> do
323− FastWeakBag.traverse subscribers $ recalculateSubscriberHeight new
337+ FastWeakBag.traverse_ subscribers $ recalculateSubscriberHeight new
325 when (isJust occ) $ do
339 when (isJust occ) $ do
326 liftIO $ writeIORef occRef occ -- Set the initial value of occRef; we don't need to do this if occ is Nothing
340 liftIO $ writeIORef occRef occ -- Set the initial value of occRef; we don't need to do this if occ is Nothing
@@ -369,7 +383,7 @@ eventRoot !k !r = Event $ wrap eventSubscribedRoot $ liftIO . getRootSubscribed
369eventNever :: Event x a
383eventNever :: Event x a
370eventNever = Event $ \_ -> return (EventSubscription (return ()) eventSubscribedNever, Nothing)
384eventNever = Event $ \_ -> return (EventSubscription (return ()) eventSubscribedNever, Nothing)
372−eventFan :: (GCompare k, HasSpiderTimeline x) => k a -> Fan x k -> Event x a
386+eventFan :: (GCompare k, HasSpiderTimeline x) => k a -> Fan x k v -> Event x (v a)
373eventFan !k !f = Event $ wrap eventSubscribedFan $ getFanSubscribed k f
387eventFan !k !f = Event $ wrap eventSubscribedFan $ getFanSubscribed k f
375eventSwitch :: HasSpiderTimeline x => Switch x a -> Event x a
389eventSwitch :: HasSpiderTimeline x => Switch x a -> Event x a
@@ -421,14 +435,14 @@ newSubscriberHold h = return $ Subscriber
421 , subscriberRecalculateHeight = \_ -> return ()
435 , subscriberRecalculateHeight = \_ -> return ()
424−newSubscriberFan :: forall x k. (HasSpiderTimeline x, GCompare k) => FanSubscribed x k -> IO (Subscriber x (DMap k Identity))
438+newSubscriberFan :: forall x k v. (HasSpiderTimeline x, GCompare k) => FanSubscribed x k v -> IO (Subscriber x (DMap k v))
425newSubscriberFan subscribed = return $ Subscriber
439newSubscriberFan subscribed = return $ Subscriber
426 { subscriberPropagate = \a -> {-# SCC "traverseFan" #-} do
440 { subscriberPropagate = \a -> {-# SCC "traverseFan" #-} do
427 subs <- liftIO $ readIORef $ fanSubscribedSubscribers subscribed
441 subs <- liftIO $ readIORef $ fanSubscribedSubscribers subscribed
428 tracePropagate (Proxy :: Proxy x) $ "SubscriberFan" <> showNodeId subscribed <> ": " ++ show (DMap.size subs) ++ " keys subscribed, " ++ show (DMap.size a) ++ " keys firing"
442 tracePropagate (Proxy :: Proxy x) $ "SubscriberFan" <> showNodeId subscribed <> ": " ++ show (DMap.size subs) ++ " keys subscribed, " ++ show (DMap.size a) ++ " keys firing"
429 liftIO $ writeIORef (fanSubscribedOccurrence subscribed) $ Just a
443 liftIO $ writeIORef (fanSubscribedOccurrence subscribed) $ Just a
430 scheduleClear $ fanSubscribedOccurrence subscribed
444 scheduleClear $ fanSubscribedOccurrence subscribed
431− let f _ (Pair (Identity v) subsubs) = do
445+ let f _ (Pair v subsubs) = do
432 propagate v $ _fanSubscribedChildren_list subsubs
446 propagate v $ _fanSubscribedChildren_list subsubs
433 return $ Constant ()
447 return $ Constant ()
434 _ <- DMap.traverseWithKey f $ DMap.intersectionWithKey (\_ -> Pair) a subs --TODO: Would be nice to have DMap.traverse_
448 _ <- DMap.traverseWithKey f $ DMap.intersectionWithKey (\_ -> Pair) a subs --TODO: Would be nice to have DMap.traverse_
@@ -436,12 +450,12 @@ newSubscriberFan subscribed = return $ Subscriber
436 , subscriberInvalidateHeight = \old -> do
450 , subscriberInvalidateHeight = \old -> do
437 when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberFan" <> showNodeId subscribed
451 when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberFan" <> showNodeId subscribed
438 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
452 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
439− forM_ (DMap.toList subscribers) $ \(_ :=> v) -> WeakBag.traverse (_fanSubscribedChildren_list v) $ invalidateSubscriberHeight old
453+ forM_ (DMap.toList subscribers) $ \(_ :=> v) -> WeakBag.traverse_ (_fanSubscribedChildren_list v) $ invalidateSubscriberHeight old
440 when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberFan" <> showNodeId subscribed <> " done"
454 when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberFan" <> showNodeId subscribed <> " done"
441 , subscriberRecalculateHeight = \new -> do
455 , subscriberRecalculateHeight = \new -> do
442 when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberFan" <> showNodeId subscribed
456 when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberFan" <> showNodeId subscribed
443 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
457 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
444− forM_ (DMap.toList subscribers) $ \(_ :=> v) -> WeakBag.traverse (_fanSubscribedChildren_list v) $ recalculateSubscriberHeight new
458+ forM_ (DMap.toList subscribers) $ \(_ :=> v) -> WeakBag.traverse_ (_fanSubscribedChildren_list v) $ recalculateSubscriberHeight new
445 when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberFan" <> showNodeId subscribed <> " done"
459 when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberFan" <> showNodeId subscribed <> " done"
@@ -457,7 +471,7 @@ newSubscriberSwitch subscribed = return $ Subscriber
457 oldHeight <- readIORef $ switchSubscribedHeight subscribed
471 oldHeight <- readIORef $ switchSubscribedHeight subscribed
458 when (oldHeight /= invalidHeight) $ do
472 when (oldHeight /= invalidHeight) $ do
459 writeIORef (switchSubscribedHeight subscribed) $! invalidHeight
473 writeIORef (switchSubscribedHeight subscribed) $! invalidHeight
460− WeakBag.traverse (switchSubscribedSubscribers subscribed) $ invalidateSubscriberHeight oldHeight
474+ WeakBag.traverse_ (switchSubscribedSubscribers subscribed) $ invalidateSubscriberHeight oldHeight
461 when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberSwitch" <> showNodeId subscribed <> " done"
475 when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberSwitch" <> showNodeId subscribed <> " done"
462 , subscriberRecalculateHeight = \new -> do
476 , subscriberRecalculateHeight = \new -> do
463 when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberSwitch" <> showNodeId subscribed
477 when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberSwitch" <> showNodeId subscribed
@@ -480,8 +494,8 @@ newSubscriberCoincidenceOuter subscribed = return $ Subscriber
480 Nothing -> do
494 Nothing -> do
481 when (innerHeight > outerHeight) $ liftIO $ do -- If the event fires, it will fire at a later height
495 when (innerHeight > outerHeight) $ liftIO $ do -- If the event fires, it will fire at a later height
482 writeIORef (coincidenceSubscribedHeight subscribed) $! innerHeight
496 writeIORef (coincidenceSubscribedHeight subscribed) $! innerHeight
483− WeakBag.traverse (coincidenceSubscribedSubscribers subscribed) $ invalidateSubscriberHeight outerHeight
497+ WeakBag.traverse_ (coincidenceSubscribedSubscribers subscribed) $ invalidateSubscriberHeight outerHeight
484− WeakBag.traverse (coincidenceSubscribedSubscribers subscribed) $ recalculateSubscriberHeight innerHeight
498+ WeakBag.traverse_ (coincidenceSubscribedSubscribers subscribed) $ recalculateSubscriberHeight innerHeight
485 Just o -> do -- Since it's already firing, no need to adjust height
499 Just o -> do -- Since it's already firing, no need to adjust height
486 liftIO $ writeIORef (coincidenceSubscribedOccurrence subscribed) occ
500 liftIO $ writeIORef (coincidenceSubscribedOccurrence subscribed) occ
487 scheduleClear $ coincidenceSubscribedOccurrence subscribed
501 scheduleClear $ coincidenceSubscribedOccurrence subscribed
@@ -528,14 +542,14 @@ propagate :: a -> WeakBag (Subscriber x a) -> EventM x ()
528propagate a subscribers = withIncreasedDepth $ do
542propagate a subscribers = withIncreasedDepth $ do
529 -- 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
543 -- 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
530 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
544 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
531− WeakBag.traverse subscribers $ \s -> subscriberPropagate s a
545+ WeakBag.traverse_ subscribers $ \s -> subscriberPropagate s a
533-- | Propagate everything at the current height
547-- | Propagate everything at the current height
534propagateFast :: a -> FastWeakBag (Subscriber x a) -> EventM x ()
548propagateFast :: a -> FastWeakBag (Subscriber x a) -> EventM x ()
535propagateFast a subscribers = withIncreasedDepth $ do
549propagateFast a subscribers = withIncreasedDepth $ do
536 -- 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
550 -- 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
537 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
551 --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
538− FastWeakBag.traverse subscribers $ \s -> subscriberPropagate s a
552+ FastWeakBag.traverse_ subscribers $ \s -> subscriberPropagate s a
540--------------------------------------------------------------------------------
554--------------------------------------------------------------------------------
541-- EventSubscribed
555-- EventSubscribed
@@ -544,6 +558,11 @@ propagateFast a subscribers = withIncreasedDepth $ do
544toAny :: a -> Any
558toAny :: a -> Any
545toAny = unsafeCoerce
559toAny = unsafeCoerce
561+-- Why do we use Any here, instead of just giving eventSubscribedRetained an
562+-- existential type? Sadly, GHC does not currently know how to unbox types
563+-- with existentially quantified fields. So instead we just coerce values
564+-- to type Any on the way in. Since we never coerce them back, this is
547data EventSubscribed x = EventSubscribed
566data EventSubscribed x = EventSubscribed
548 { eventSubscribedHeightRef :: {-# UNPACK #-} !(IORef Height)
567 { eventSubscribedHeightRef :: {-# UNPACK #-} !(IORef Height)
549 , eventSubscribedRetained :: {-# NOUNPACK #-} !Any
568 , eventSubscribedRetained :: {-# NOUNPACK #-} !Any
@@ -576,7 +595,7 @@ eventSubscribedNever = EventSubscribed
579−eventSubscribedFan :: FanSubscribed x k -> EventSubscribed x
598+eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
580eventSubscribedFan !subscribed = EventSubscribed
599eventSubscribedFan !subscribed = EventSubscribed
581 { eventSubscribedHeightRef = eventSubscribedHeightRef $ _eventSubscription_subscribed $ fanSubscribedParent subscribed
600 { eventSubscribedHeightRef = eventSubscribedHeightRef $ _eventSubscription_subscribed $ fanSubscribedParent subscribed
582 , eventSubscribedRetained = toAny subscribed
601 , eventSubscribedRetained = toAny subscribed
@@ -667,7 +686,7 @@ behaviorPull !p = Behavior $ do
667 wi <- liftIO $ mkWeakPtrWithDebug i "InvalidatorPull"
686 wi <- liftIO $ mkWeakPtrWithDebug i "InvalidatorPull"
668 parentsRef <- liftIO $ newIORef []
687 parentsRef <- liftIO $ newIORef []
669 holdInits <- askBehaviorHoldInits
688 holdInits <- askBehaviorHoldInits
670− a <- liftIO $ runReaderT (unBehaviorM $ pullCompute p) (Just (wi, parentsRef), holdInits)
689+ a <- liftIO $ runReaderIO (unBehaviorM $ pullCompute p) (Just (wi, parentsRef), holdInits)
671 invsRef <- liftIO . newIORef . maybeToList =<< askInvalidator
690 invsRef <- liftIO . newIORef . maybeToList =<< askInvalidator
672 parents <- liftIO $ readIORef parentsRef
691 parents <- liftIO $ readIORef parentsRef
673 let subscribed = PullSubscribed
692 let subscribed = PullSubscribed
@@ -702,42 +721,46 @@ readBehaviorUntracked b = do
702-- Dynamic
721-- Dynamic
703--------------------------------------------------------------------------------
722--------------------------------------------------------------------------------
724+type DynamicS x p = Dynamic x (PatchTarget p) p
705−data Dynamic x p = Dynamic
726+data Dynamic x target p = Dynamic
706− { dynamicCurrent :: !(Behavior x (PatchTarget p))
727+ { dynamicCurrent :: !(Behavior x target)
707 , dynamicUpdated :: Event x p -- This must be lazy; see the comment on holdEvent --TODO: Would this let us eliminate `Dyn`?
728 , dynamicUpdated :: Event x p -- This must be lazy; see the comment on holdEvent --TODO: Would this let us eliminate `Dyn`?
731+deriving instance (HasSpiderTimeline x) => Functor (Dynamic x target)
710−dynamicHold :: Hold x p -> Dynamic x p
736+dynamicHold :: Hold x p -> DynamicS x p
711dynamicHold !h = Dynamic
737dynamicHold !h = Dynamic
712 { dynamicCurrent = behaviorHold h
738 { dynamicCurrent = behaviorHold h
713 , dynamicUpdated = eventHold h
739 , dynamicUpdated = eventHold h
716−dynamicHoldIdentity :: Hold x (Identity a) -> Dynamic x (Identity a)
742+dynamicHoldIdentity :: Hold x (Identity a) -> DynamicS x (Identity a)
717dynamicHoldIdentity = dynamicHold
743dynamicHoldIdentity = dynamicHold
719−dynamicConst :: PatchTarget p -> Dynamic x p
745+dynamicConst :: PatchTarget p -> DynamicS x p
720dynamicConst !a = Dynamic
746dynamicConst !a = Dynamic
721 { dynamicCurrent = behaviorConst a
747 { dynamicCurrent = behaviorConst a
722 , dynamicUpdated = eventNever
748 , dynamicUpdated = eventNever
725−dynamicDyn :: (HasSpiderTimeline x, Patch p) => Dyn x p -> Dynamic x p
751+dynamicDyn :: (HasSpiderTimeline x, Patch p) => Dyn x p -> DynamicS x p
726dynamicDyn !d = Dynamic
752dynamicDyn !d = Dynamic
727 { dynamicCurrent = behaviorDyn d
753 { dynamicCurrent = behaviorDyn d
728 , dynamicUpdated = eventDyn d
754 , dynamicUpdated = eventDyn d
731−dynamicDynIdentity :: HasSpiderTimeline x => Dyn x (Identity a) -> Dynamic x (Identity a)
757+dynamicDynIdentity :: HasSpiderTimeline x => Dyn x (Identity a) -> DynamicS x (Identity a)
732dynamicDynIdentity = dynamicDyn
758dynamicDynIdentity = dynamicDyn
734--------------------------------------------------------------------------------
760--------------------------------------------------------------------------------
735-- Combinators
761-- Combinators
736--------------------------------------------------------------------------------
762--------------------------------------------------------------------------------
738−--TODO: Figure out why certain things are not 'representational', then make them
739−--representational so we can use coerce
741--type role Hold representational
764--type role Hold representational
742data Hold x p
765data Hold x p
743 = Hold { holdValue :: !(IORef (PatchTarget p))
766 = Hold { holdValue :: !(IORef (PatchTarget p))
@@ -758,20 +781,25 @@ globalSpiderTimelineEnv = unsafePerformIO unsafeNewSpiderTimelineEnv
759-- | Stores all global data relevant to a particular Spider timeline; only one
782-- | Stores all global data relevant to a particular Spider timeline; only one
760-- value should exist for each type @x@
783-- value should exist for each type @x@
784+newtype SpiderTimelineEnv x = STE {unSTE :: SpiderTimelineEnv' x}
785+-- We implement SpiderTimelineEnv with a newtype wrapper so
786+-- we can get the coercions we want safely.
787+type role SpiderTimelineEnv nominal
761−data SpiderTimelineEnv x = SpiderTimelineEnv
789+data SpiderTimelineEnv' x = SpiderTimelineEnv
762 { _spiderTimeline_lock :: {-# UNPACK #-} !(MVar ())
790 { _spiderTimeline_lock :: {-# UNPACK #-} !(MVar ())
763 , _spiderTimeline_eventEnv :: {-# UNPACK #-} !(EventEnv x)
791 , _spiderTimeline_eventEnv :: {-# UNPACK #-} !(EventEnv x)
764#ifdef DEBUG
792#ifdef DEBUG
765 , _spiderTimeline_depth :: {-# UNPACK #-} !(IORef Int)
793 , _spiderTimeline_depth :: {-# UNPACK #-} !(IORef Int)
768−type role SpiderTimelineEnv nominal
796+type role SpiderTimelineEnv' phantom
770instance Eq (SpiderTimelineEnv x) where
798instance Eq (SpiderTimelineEnv x) where
771 _ == _ = True -- Since only one exists of each type
799 _ == _ = True -- Since only one exists of each type
773instance GEq SpiderTimelineEnv where
801instance GEq SpiderTimelineEnv where
774− a `geq` b = if _spiderTimeline_lock a == _spiderTimeline_lock b
802+ a `geq` b = if _spiderTimeline_lock (unSTE a) == _spiderTimeline_lock (unSTE b)
775 then Just $ unsafeCoerce Refl -- This unsafeCoerce is safe because the same SpiderTimelineEnv can't have two different 'x' arguments
803 then Just $ unsafeCoerce Refl -- This unsafeCoerce is safe because the same SpiderTimelineEnv can't have two different 'x' arguments
776 else Nothing
804 else Nothing
@@ -794,7 +822,7 @@ runEventM :: EventM x a -> IO a
794runEventM = unEventM
822runEventM = unEventM
796asksEventEnv :: forall x a. HasSpiderTimeline x => (EventEnv x -> a) -> EventM x a
824asksEventEnv :: forall x a. HasSpiderTimeline x => (EventEnv x -> a) -> EventM x a
797−asksEventEnv f = return $ f $ _spiderTimeline_eventEnv (spiderTimeline :: SpiderTimelineEnv x)
825+asksEventEnv f = return $ f $ _spiderTimeline_eventEnv (unSTE (spiderTimeline :: SpiderTimelineEnv x))
799class MonadIO m => Defer a m where
827class MonadIO m => Defer a m where
800 getDeferralQueue :: m (IORef [a])
828 getDeferralQueue :: m (IORef [a])
@@ -932,9 +960,9 @@ getHoldEventSubscription h = do
933type BehaviorEnv x = (Maybe (Weak (Invalidator x), IORef [SomeBehaviorSubscribed x]), IORef [SomeHoldInit x])
961type BehaviorEnv x = (Maybe (Weak (Invalidator x), IORef [SomeBehaviorSubscribed x]), IORef [SomeHoldInit x])
935−--type role BehaviorM representational
936-- BehaviorM can sample behaviors
963-- BehaviorM can sample behaviors
937−newtype BehaviorM x a = BehaviorM { unBehaviorM :: ReaderT (BehaviorEnv x) IO a } deriving (Functor, Applicative, MonadIO, MonadFix)
964+newtype BehaviorM x a = BehaviorM { unBehaviorM :: ReaderIO (BehaviorEnv x) a }
965+ deriving (Functor, Applicative, MonadIO, MonadFix, MonadReader (BehaviorEnv x))
939instance Monad (BehaviorM x) where
967instance Monad (BehaviorM x) where
940 {-# INLINE (>>=) #-}
968 {-# INLINE (>>=) #-}
@@ -985,7 +1013,7 @@ data RootSubscribed x a = forall k. GCompare k => RootSubscribed
988−data Root x (k :: * -> *)
1016+data Root x k
989 = Root { rootOccurrence :: !(IORef (DMap k Identity)) -- The currently-firing occurrence of this event
1017 = Root { rootOccurrence :: !(IORef (DMap k Identity)) -- The currently-firing occurrence of this event
990 , rootSubscribed :: !(IORef (DMap k (RootSubscribed x)))
1018 , rootSubscribed :: !(IORef (DMap k (RootSubscribed x)))
991 , rootInit :: !(forall a. k a -> RootTrigger x a -> IO (IO ()))
1019 , rootInit :: !(forall a. k a -> RootTrigger x a -> IO (IO ()))
@@ -1055,25 +1083,25 @@ heightBagVerify b@(HeightBag s c) = if
1055heightBagVerify = id
1083heightBagVerify = id
1058−data FanSubscribedChildren (x :: *) k a = FanSubscribedChildren
1086+data FanSubscribedChildren x k v a = FanSubscribedChildren
1059− { _fanSubscribedChildren_list :: !(WeakBag (Subscriber x a))
1087+ { _fanSubscribedChildren_list :: !(WeakBag (Subscriber x (v a)))
1060− , _fanSubscribedChildren_self :: {-# NOUNPACK #-} !(k a, FanSubscribed x k)
1088+ , _fanSubscribedChildren_self :: {-# NOUNPACK #-} !(k a, FanSubscribed x k v)
1061− , _fanSubscribedChildren_weakSelf :: !(IORef (Weak (k a, FanSubscribed x k)))
1089+ , _fanSubscribedChildren_weakSelf :: !(IORef (Weak (k a, FanSubscribed x k v)))
1064−data FanSubscribed (x :: *) k
1092+data FanSubscribed x k v
1065− = FanSubscribed { fanSubscribedCachedSubscribed :: !(IORef (Maybe (FanSubscribed x k)))
1093+ = FanSubscribed { fanSubscribedCachedSubscribed :: !(IORef (Maybe (FanSubscribed x k v)))
1066− , fanSubscribedOccurrence :: !(IORef (Maybe (DMap k Identity)))
1094+ , fanSubscribedOccurrence :: !(IORef (Maybe (DMap k v)))
1067− , fanSubscribedSubscribers :: !(IORef (DMap k (FanSubscribedChildren x k))) -- This DMap should never be empty
1095+ , fanSubscribedSubscribers :: !(IORef (DMap k (FanSubscribedChildren x k v))) -- This DMap should never be empty
1068 , fanSubscribedParent :: !(EventSubscription x)
1096 , fanSubscribedParent :: !(EventSubscription x)
1069#ifdef DEBUG_NODEIDS
1097#ifdef DEBUG_NODEIDS
1070 , fanSubscribedNodeId :: Int
1098 , fanSubscribedNodeId :: Int
1074−data Fan x k
1102+data Fan x k v
1075− = Fan { fanParent :: !(Event x (DMap k Identity))
1103+ = Fan { fanParent :: !(Event x (DMap k v))
1076− , fanSubscribed :: !(IORef (Maybe (FanSubscribed x k)))
1104+ , fanSubscribed :: !(IORef (Maybe (FanSubscribed x k v)))
1079data SwitchSubscribed x a
1107data SwitchSubscribed x a
@@ -1135,19 +1163,27 @@ instance HasSpiderTimeline x => Align (Event x) where
1135#if MIN_VERSION_these(0, 8, 0)
1163#if MIN_VERSION_these(0, 8, 0)
1136instance HasSpiderTimeline x => Semialign (Event x) where
1164instance HasSpiderTimeline x => Semialign (Event x) where
1166+ align ea eb = mapMaybe dmapToThese $ mergeG coerce $ dynamicConst $
1138− align ea eb = mapMaybe dmapToThese $ merge $ dynamicConst $ DMap.fromDistinctAscList [LeftTag :=> ea, RightTag :=> eb]
1167+ DMap.fromDistinctAscList [LeftTag :=> ea, RightTag :=> eb]
1169+#ifdef MIN_VERSION_semialign
1170+#if MIN_VERSION_semialign(1,1,0)
1171+instance HasSpiderTimeline x => Zip (Event x) where
1173+ zip x y = mapMaybe justThese $ align x y
1140data DynType x p = UnsafeDyn !(BehaviorM x (PatchTarget p), Event x p)
1176data DynType x p = UnsafeDyn !(BehaviorM x (PatchTarget p), Event x p)
1141 | BuildDyn !(EventM x (PatchTarget p), Event x p)
1177 | BuildDyn !(EventM x (PatchTarget p), Event x p)
1142 | HoldDyn !(Hold x p)
1178 | HoldDyn !(Hold x p)
1144−newtype Dyn x p = Dyn { unDyn :: IORef (DynType x p) }
1180+newtype Dyn (x :: Type) p = Dyn { unDyn :: IORef (DynType x p) }
1146−newMapDyn :: HasSpiderTimeline x => (a -> b) -> Dynamic x (Identity a) -> Dynamic x (Identity b)
1182+newMapDyn :: HasSpiderTimeline x => (a -> b) -> DynamicS x (Identity a) -> DynamicS x (Identity b)
1147newMapDyn f d = dynamicDynIdentity $ unsafeBuildDynamic (fmap f $ readBehaviorTracked $ dynamicCurrent d) (Identity . f . runIdentity <$> dynamicUpdated d)
1183newMapDyn f d = dynamicDynIdentity $ unsafeBuildDynamic (fmap f $ readBehaviorTracked $ dynamicCurrent d) (Identity . f . runIdentity <$> dynamicUpdated d)
1149--TODO: Avoid the duplication between this and R.zipDynWith
1185--TODO: Avoid the duplication between this and R.zipDynWith
1150−zipDynWith :: HasSpiderTimeline x => (a -> b -> c) -> Dynamic x (Identity a) -> Dynamic x (Identity b) -> Dynamic x (Identity c)
1186+zipDynWith :: HasSpiderTimeline x => (a -> b -> c) -> DynamicS x (Identity a) -> DynamicS x (Identity b) -> DynamicS x (Identity c)
1151zipDynWith f da db =
1187zipDynWith f da db =
1152 let eab = align (dynamicUpdated da) (dynamicUpdated db)
1188 let eab = align (dynamicUpdated da) (dynamicUpdated db)
1153 ec = flip push eab $ \o -> do
1189 ec = flip push eab $ \o -> do
@@ -1170,18 +1206,12 @@ buildDynamic readV0 v' = do
1170 return d
1206 return d
1172unsafeBuildDynamic :: BehaviorM x (PatchTarget p) -> Event x p -> Dyn x p
1208unsafeBuildDynamic :: BehaviorM x (PatchTarget p) -> Event x p -> Dyn x p
1173−unsafeBuildDynamic readV0 v' = Dyn $ unsafeNewIORef x $ UnsafeDyn x
1209+unsafeBuildDynamic readV0 v' =
1174− where x = (readV0, v')
1210+ Dyn $ unsafePerformIO $ newIORef $ UnsafeDyn (readV0, v')
1176-- ResultM can read behaviors and events
1212-- ResultM can read behaviors and events
1177type ResultM = EventM
1213type ResultM = EventM
1179−{-# NOINLINE unsafeNewIORef #-}
1180−unsafeNewIORef :: a -> b -> IORef b
1181−unsafeNewIORef a b = unsafePerformIO $ do
1185instance HasSpiderTimeline x => Functor (Event x) where
1215instance HasSpiderTimeline x => Functor (Event x) where
1186 fmap f = push $ return . Just . f
1216 fmap f = push $ return . Just . f
@@ -1194,33 +1224,42 @@ push f e = cacheEvent (pushCheap f e)
1195{-# INLINABLE pull #-}
1225{-# INLINABLE pull #-}
1196pull :: BehaviorM x a -> Behavior x a
1226pull :: BehaviorM x a -> Behavior x a
1197−pull a = behaviorPull $ Pull
1227+pull a = unsafePerformIO $ do
1198− { pullCompute = a
1228+ ref <- newIORef Nothing
1199− , pullValue = unsafeNewIORef a Nothing
1200#ifdef DEBUG_NODEIDS
1229#ifdef DEBUG_NODEIDS
1201− , pullNodeId = unsafeNodeId a
1230+ nid <- newNodeId
1232+ pure $ behaviorPull $ Pull
1235+#ifdef DEBUG_NODEIDS
1205{-# INLINABLE switch #-}
1240{-# INLINABLE switch #-}
1206switch :: HasSpiderTimeline x => Behavior x (Event x a) -> Event x a
1241switch :: HasSpiderTimeline x => Behavior x (Event x a) -> Event x a
1242+switch a = unsafePerformIO $ do
1243+ ref <- newIORef Nothing
1207−switch a = eventSwitch $ Switch
1244+ pure $ eventSwitch $ Switch
1208− { switchParent = a
1245+ { switchParent = a
1209− , switchSubscribed = unsafeNewIORef a Nothing
1246+ , switchSubscribed = ref
1212coincidence :: HasSpiderTimeline x => Event x (Event x a) -> Event x a
1249coincidence :: HasSpiderTimeline x => Event x (Event x a) -> Event x a
1250+coincidence a = unsafePerformIO $ do
1251+ ref <- newIORef Nothing
1213−coincidence a = eventCoincidence $ Coincidence
1252+ pure $ eventCoincidence $ Coincidence
1214− { coincidenceParent = a
1253+ { coincidenceParent = a
1215− , coincidenceSubscribed = unsafeNewIORef a Nothing
1254+ , coincidenceSubscribed = ref
1218-- Propagate the given event occurrence; before cleaning up, run the given action, which may read the state of events and behaviors
1257-- Propagate the given event occurrence; before cleaning up, run the given action, which may read the state of events and behaviors
1219run :: forall x b. HasSpiderTimeline x => [DSum (RootTrigger x) Identity] -> ResultM x b -> SpiderHost x b
1258run :: forall x b. HasSpiderTimeline x => [DSum (RootTrigger x) Identity] -> ResultM x b -> SpiderHost x b
1220run roots after = do
1259run roots after = do
1221 tracePropagate (Proxy :: Proxy x) $ "Running an event frame with " <> show (length roots) <> " events"
1260 tracePropagate (Proxy :: Proxy x) $ "Running an event frame with " <> show (length roots) <> " events"
1222 let t = spiderTimeline :: SpiderTimelineEnv x
1261 let t = spiderTimeline :: SpiderTimelineEnv x
1223− result <- SpiderHost $ withMVar (_spiderTimeline_lock t) $ \_ -> unSpiderHost $ runFrame $ do
1262+ result <- SpiderHost $ withMVar (_spiderTimeline_lock (unSTE t)) $ \_ -> unSpiderHost $ runFrame $ do
1224 rootsToPropagate <- forM roots $ \r@(RootTrigger (_, occRef, k) :=> a) -> do
1263 rootsToPropagate <- forM roots $ \r@(RootTrigger (_, occRef, k) :=> a) -> do
1225 occBefore <- liftIO $ do
1264 occBefore <- liftIO $ do
1226 occBefore <- readIORef occRef
1265 occBefore <- readIORef occRef
@@ -1284,9 +1323,9 @@ type WeakList a = [Weak a]
1284withIncreasedDepth :: CanTrace x m => m a -> m a
1323withIncreasedDepth :: CanTrace x m => m a -> m a
1285withIncreasedDepth a = do
1324withIncreasedDepth a = do
1286 spiderTimeline <- askSpiderTimelineEnv
1325 spiderTimeline <- askSpiderTimelineEnv
1287− liftIO $ modifyIORef' (_spiderTimeline_depth spiderTimeline) succ
1326+ liftIO $ modifyIORef' (_spiderTimeline_depth (unSTE spiderTimeline)) succ
1288 result <- a
1327 result <- a
1289− liftIO $ modifyIORef' (_spiderTimeline_depth spiderTimeline) pred
1328+ liftIO $ modifyIORef' (_spiderTimeline_depth (unSTE spiderTimeline)) pred
1290 return result
1329 return result
1292withIncreasedDepth :: m a -> m a
1331withIncreasedDepth :: m a -> m a
@@ -1313,7 +1352,7 @@ traceMWhen _ b getMessage = when b $ do
1313 message <- getMessage
1352 message <- getMessage
1314#ifdef DEBUG
1353#ifdef DEBUG
1315 spiderTimeline <- askSpiderTimelineEnv
1354 spiderTimeline <- askSpiderTimelineEnv
1316− d <- liftIO $ readIORef $ _spiderTimeline_depth spiderTimeline
1355+ d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE spiderTimeline
1318 let d = 0
1357 let d = 0
@@ -1354,25 +1393,25 @@ propagateSubscriberHold h a = do
1354data SomeResetCoincidence x = forall a. SomeResetCoincidence !(EventSubscription x) !(Maybe (CoincidenceSubscribed x a)) -- The CoincidenceSubscriber will be present only if heights need to be reset
1393data SomeResetCoincidence x = forall a. SomeResetCoincidence !(EventSubscription x) !(Maybe (CoincidenceSubscribed x a)) -- The CoincidenceSubscriber will be present only if heights need to be reset
1356runBehaviorM :: BehaviorM x a -> Maybe (Weak (Invalidator x), IORef [SomeBehaviorSubscribed x]) -> IORef [SomeHoldInit x] -> IO a
1395runBehaviorM :: BehaviorM x a -> Maybe (Weak (Invalidator x), IORef [SomeBehaviorSubscribed x]) -> IORef [SomeHoldInit x] -> IO a
1357−runBehaviorM a mwi holdInits = runReaderT (unBehaviorM a) (mwi, holdInits)
1396+runBehaviorM a mwi holdInits = runReaderIO (unBehaviorM a) (mwi, holdInits)
1359askInvalidator :: BehaviorM x (Maybe (Weak (Invalidator x)))
1398askInvalidator :: BehaviorM x (Maybe (Weak (Invalidator x)))
1360askInvalidator = do
1399askInvalidator = do
1361− (!m, _) <- BehaviorM ask
1400+ (!m, _) <- ask
1362 case m of
1401 case m of
1363 Nothing -> return Nothing
1402 Nothing -> return Nothing
1364 Just (!wi, _) -> return $ Just wi
1403 Just (!wi, _) -> return $ Just wi
1366askParentsRef :: BehaviorM x (Maybe (IORef [SomeBehaviorSubscribed x]))
1405askParentsRef :: BehaviorM x (Maybe (IORef [SomeBehaviorSubscribed x]))
1367askParentsRef = do
1406askParentsRef = do
1368− (!m, _) <- BehaviorM ask
1407+ (!m, _) <- ask
1369 case m of
1408 case m of
1370 Nothing -> return Nothing
1409 Nothing -> return Nothing
1371 Just (_, !p) -> return $ Just p
1410 Just (_, !p) -> return $ Just p
1373askBehaviorHoldInits :: BehaviorM x (IORef [SomeHoldInit x])
1412askBehaviorHoldInits :: BehaviorM x (IORef [SomeHoldInit x])
1374askBehaviorHoldInits = do
1413askBehaviorHoldInits = do
1375− (_, !his) <- BehaviorM ask
1414+ (_, !his) <- ask
1376 return his
1415 return his
1378{-# INLINE getDynHold #-}
1417{-# INLINE getDynHold #-}
@@ -1417,6 +1456,9 @@ getRootSubscribed k r sub = do
1417 when debugPropagate $ putStrLn $ "getRootSubscribed: calling rootInit"
1456 when debugPropagate $ putStrLn $ "getRootSubscribed: calling rootInit"
1418 uninit <- rootInit r k $ RootTrigger (subs, rootOccurrence r, k)
1457 uninit <- rootInit r k $ RootTrigger (subs, rootOccurrence r, k)
1419 writeIORef uninitRef $! uninit
1458 writeIORef uninitRef $! uninit
1459+#ifdef DEBUG_NODEIDS
1420 let !subscribed = RootSubscribed
1462 let !subscribed = RootSubscribed
1421 { rootSubscribedKey = k
1463 { rootSubscribedKey = k
1422 , rootSubscribedCachedSubscribed = cached
1464 , rootSubscribedCachedSubscribed = cached
@@ -1425,7 +1467,7 @@ getRootSubscribed k r sub = do
1425 , rootSubscribedUninit = uninit
1467 , rootSubscribedUninit = uninit
1426 , rootSubscribedWeakSelf = weakSelf
1468 , rootSubscribedWeakSelf = weakSelf
1427#ifdef DEBUG_NODEIDS
1469#ifdef DEBUG_NODEIDS
1428− , rootSubscribedNodeId = unsafeNodeId (k, r, subs)
1470+ , rootSubscribedNodeId = nid
1431 -- If we die at the same moment that all our children die, they will
1473 -- If we die at the same moment that all our children die, they will
@@ -1474,16 +1516,10 @@ newFanInt = do
1474 , _fanInt_occRef = occRef
1516 , _fanInt_occRef = occRef
1477−{-# NOINLINE unsafeNewFanInt #-}
1478−unsafeNewFanInt :: b -> FanInt x a
1479−unsafeNewFanInt b = unsafePerformIO $ do
1483fanInt :: HasSpiderTimeline x => Event x (IntMap a) -> EventSelectorInt x a
1519fanInt :: HasSpiderTimeline x => Event x (IntMap a) -> EventSelectorInt x a
1484−fanInt p =
1520+fanInt p = unsafePerformIO $ do
1485− let self = unsafeNewFanInt p
1521+ self <- newFanInt
1486− in EventSelectorInt $ \k -> Event $ \sub -> do
1522+ pure $ EventSelectorInt $ \k -> Event $ \sub -> do
1487 isEmpty <- liftIO $ FastMutableIntMap.isEmpty (_fanInt_subscribers self)
1523 isEmpty <- liftIO $ FastMutableIntMap.isEmpty (_fanInt_subscribers self)
1488 when isEmpty $ do -- This is the first subscriber, so we need to subscribe to our input
1524 when isEmpty $ do -- This is the first subscriber, so we need to subscribe to our input
1489 (subscription, parentOcc) <- subscribeAndRead p $ Subscriber
1525 (subscription, parentOcc) <- subscribeAndRead p $ Subscriber
@@ -1491,15 +1527,15 @@ fanInt p =
1491 liftIO $ writeIORef (_fanInt_occRef self) m
1527 liftIO $ writeIORef (_fanInt_occRef self) m
1492 scheduleIntClear $ _fanInt_occRef self
1528 scheduleIntClear $ _fanInt_occRef self
1493 FastMutableIntMap.forIntersectionWithImmutable_ (_fanInt_subscribers self) m $ \b v -> do --TODO: Do we need to know that no subscribers are being added as we traverse?
1529 FastMutableIntMap.forIntersectionWithImmutable_ (_fanInt_subscribers self) m $ \b v -> do --TODO: Do we need to know that no subscribers are being added as we traverse?
1494− FastWeakBag.traverse b $ \s -> do
1530+ FastWeakBag.traverse_ b $ \s -> do
1495 subscriberPropagate s v
1531 subscriberPropagate s v
1496 , subscriberInvalidateHeight = \old -> do
1532 , subscriberInvalidateHeight = \old -> do
1497 FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b -> do
1533 FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b -> do
1498− FastWeakBag.traverse b $ \s -> do
1534+ FastWeakBag.traverse_ b $ \s -> do
1499 subscriberInvalidateHeight s old
1535 subscriberInvalidateHeight s old
1500 , subscriberRecalculateHeight = \new -> do
1536 , subscriberRecalculateHeight = \new -> do
1501 FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b -> do
1537 FastMutableIntMap.for_ (_fanInt_subscribers self) $ \b -> do
1502− FastWeakBag.traverse b $ \s -> do
1538+ FastWeakBag.traverse_ b $ \s -> do
1503 subscriberRecalculateHeight s new
1539 subscriberRecalculateHeight s new
1505 liftIO $ do
1541 liftIO $ do
@@ -1519,7 +1555,7 @@ fanInt p =
1519 return (EventSubscription (FastWeakBag.remove t) $! EventSubscribed heightRef $! toAny (_fanInt_subscriptionRef self, t), IntMap.lookup k currentOcc)
1555 return (EventSubscription (FastWeakBag.remove t) $! EventSubscribed heightRef $! toAny (_fanInt_subscriptionRef self, t), IntMap.lookup k currentOcc)
1521{-# INLINABLE getFanSubscribed #-}
1557{-# INLINABLE getFanSubscribed #-}
1522−getFanSubscribed :: (HasSpiderTimeline x, GCompare k) => k a -> Fan x k -> Subscriber x a -> EventM x (WeakBagTicket, FanSubscribed x k, Maybe a)
1558+getFanSubscribed :: (HasSpiderTimeline x, GCompare k) => k a -> Fan x k v -> Subscriber x (v a) -> EventM x (WeakBagTicket, FanSubscribed x k v, Maybe (v a))
1523getFanSubscribed k f sub = do
1559getFanSubscribed k f sub = do
1524 mSubscribed <- liftIO $ readIORef $ fanSubscribed f
1560 mSubscribed <- liftIO $ readIORef $ fanSubscribed f
1525 case mSubscribed of
1561 case mSubscribed of
@@ -1537,13 +1573,16 @@ getFanSubscribed k f sub = do
1537 subscribersRef <- liftIO $ newIORef $ error "getFanSubscribed: subscribersRef not yet initialized"
1573 subscribersRef <- liftIO $ newIORef $ error "getFanSubscribed: subscribersRef not yet initialized"
1538 occRef <- liftIO $ newIORef parentOcc
1574 occRef <- liftIO $ newIORef parentOcc
1539 when (isJust parentOcc) $ scheduleClear occRef
1575 when (isJust parentOcc) $ scheduleClear occRef
1576+#ifdef DEBUG_NODEIDS
1577+ nid <- liftIO newNodeId
1540 let subscribed = FanSubscribed
1579 let subscribed = FanSubscribed
1541 { fanSubscribedCachedSubscribed = fanSubscribed f
1580 { fanSubscribedCachedSubscribed = fanSubscribed f
1542 , fanSubscribedOccurrence = occRef
1581 , fanSubscribedOccurrence = occRef
1543 , fanSubscribedParent = subscription
1582 , fanSubscribedParent = subscription
1544 , fanSubscribedSubscribers = subscribersRef
1583 , fanSubscribedSubscribers = subscribersRef
1545#ifdef DEBUG_NODEIDS
1584#ifdef DEBUG_NODEIDS
1546− , fanSubscribedNodeId = unsafeNodeId f
1585+ , fanSubscribedNodeId = nid
1549 let !self = (k, subscribed)
1588 let !self = (k, subscribed)
@@ -1553,7 +1592,7 @@ getFanSubscribed k f sub = do
1553 liftIO $ writeIORef (fanSubscribed f) $ Just subscribed
1592 liftIO $ writeIORef (fanSubscribed f) $ Just subscribed
1554 return (slnForSub, subscribed, coerce $ DMap.lookup k =<< parentOcc)
1593 return (slnForSub, subscribed, coerce $ DMap.lookup k =<< parentOcc)
1556−cleanupFanSubscribed :: GCompare k => (k a, FanSubscribed x k) -> IO ()
1595+cleanupFanSubscribed :: GCompare k => (k a, FanSubscribed x k v) -> IO ()
1557cleanupFanSubscribed (k, subscribed) = do
1596cleanupFanSubscribed (k, subscribed) = do
1558 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1597 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1559 let reducedSubscribers = DMap.delete k subscribers
1598 let reducedSubscribers = DMap.delete k subscribers
@@ -1565,7 +1604,7 @@ cleanupFanSubscribed (k, subscribed) = do
1565 else writeIORef (fanSubscribedSubscribers subscribed) $! reducedSubscribers
1604 else writeIORef (fanSubscribedSubscribers subscribed) $! reducedSubscribers
1567{-# INLINE subscribeFanSubscribed #-}
1606{-# INLINE subscribeFanSubscribed #-}
1568−subscribeFanSubscribed :: GCompare k => k a -> FanSubscribed x k -> Subscriber x a -> IO WeakBagTicket
1607+subscribeFanSubscribed :: GCompare k => k a -> FanSubscribed x k v -> Subscriber x (v a) -> IO WeakBagTicket
1569subscribeFanSubscribed k subscribed sub = do
1608subscribeFanSubscribed k subscribed sub = do
1570 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1609 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1571 case DMap.lookup k subscribers of
1610 case DMap.lookup k subscribers of
@@ -1603,6 +1642,9 @@ getSwitchSubscribed s sub = do
1603 when (isJust parentOcc) $ scheduleClear occRef
1642 when (isJust parentOcc) $ scheduleClear occRef
1604 weakSelf <- liftIO $ newIORef $ error "getSwitchSubscribed: weakSelf not yet initialized"
1643 weakSelf <- liftIO $ newIORef $ error "getSwitchSubscribed: weakSelf not yet initialized"
1605 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupSwitchSubscribed
1644 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupSwitchSubscribed
1645+#ifdef DEBUG_NODEIDS
1646+ nid <- liftIO newNodeId
1606 let !subscribed = SwitchSubscribed
1648 let !subscribed = SwitchSubscribed
1607 { switchSubscribedCachedSubscribed = switchSubscribed s
1649 { switchSubscribedCachedSubscribed = switchSubscribed s
1608 , switchSubscribedOccurrence = occRef
1650 , switchSubscribedOccurrence = occRef
@@ -1615,7 +1657,7 @@ getSwitchSubscribed s sub = do
1615 , switchSubscribedCurrentParent = subscriptionRef
1657 , switchSubscribedCurrentParent = subscriptionRef
1616 , switchSubscribedWeakSelf = weakSelf
1658 , switchSubscribedWeakSelf = weakSelf
1617#ifdef DEBUG_NODEIDS
1659#ifdef DEBUG_NODEIDS
1618− , switchSubscribedNodeId = unsafeNodeId s
1660+ , switchSubscribedNodeId = nid
1621 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "switchSubscribedWeakSelf"
1663 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "switchSubscribedWeakSelf"
@@ -1660,6 +1702,9 @@ getCoincidenceSubscribed c sub = do
1660 scheduleClear innerSubdRef
1702 scheduleClear innerSubdRef
1661 weakSelf <- liftIO $ newIORef $ error "getCoincidenceSubscribed: weakSelf not yet implemented"
1703 weakSelf <- liftIO $ newIORef $ error "getCoincidenceSubscribed: weakSelf not yet implemented"
1662 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupCoincidenceSubscribed
1704 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupCoincidenceSubscribed
1705+#ifdef DEBUG_NODEIDS
1706+ nid <- liftIO newNodeId
1663 let subscribed = CoincidenceSubscribed
1708 let subscribed = CoincidenceSubscribed
1664 { coincidenceSubscribedCachedSubscribed = coincidenceSubscribed c
1709 { coincidenceSubscribedCachedSubscribed = coincidenceSubscribed c
1665 , coincidenceSubscribedOccurrence = occRef
1710 , coincidenceSubscribedOccurrence = occRef
@@ -1670,7 +1715,7 @@ getCoincidenceSubscribed c sub = do
1670 , coincidenceSubscribedInnerParent = innerSubdRef
1715 , coincidenceSubscribedInnerParent = innerSubdRef
1671 , coincidenceSubscribedWeakSelf = weakSelf
1716 , coincidenceSubscribedWeakSelf = weakSelf
1672#ifdef DEBUG_NODEIDS
1717#ifdef DEBUG_NODEIDS
1673− , coincidenceSubscribedNodeId = unsafeNodeId c
1718+ , coincidenceSubscribedNodeId = nid
1676 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "CoincidenceSubscribed"
1721 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "CoincidenceSubscribed"
@@ -1687,26 +1732,34 @@ cleanupCoincidenceSubscribed subscribed = do
1687subscribeCoincidenceSubscribed :: CoincidenceSubscribed x a -> Subscriber x a -> IO WeakBagTicket
1732subscribeCoincidenceSubscribed :: CoincidenceSubscribed x a -> Subscriber x a -> IO WeakBagTicket
1688subscribeCoincidenceSubscribed subscribed sub = WeakBag.insert sub (coincidenceSubscribedSubscribers subscribed) (coincidenceSubscribedWeakSelf subscribed) cleanupCoincidenceSubscribed
1733subscribeCoincidenceSubscribed subscribed sub = WeakBag.insert sub (coincidenceSubscribedSubscribers subscribed) (coincidenceSubscribedWeakSelf subscribed) cleanupCoincidenceSubscribed
1690−{-# INLINE merge #-}
1735+{-# INLINE mergeG #-}
1691−merge :: forall k x. (HasSpiderTimeline x, GCompare k) => Dynamic x (PatchDMap k (Event x)) -> Event x (DMap k Identity)
1736+mergeG :: forall k q x v. (HasSpiderTimeline x, GCompare k)
1737+ => (forall a. q a -> Event x (v a))
1738+ -> DynamicS x (PatchDMap k q) -> Event x (DMap k v)
1692−merge d = cacheEvent (mergeCheap d)
1739+mergeG nt d = cacheEvent (mergeCheap nt d)
1694{-# INLINE mergeWithMove #-}
1741{-# INLINE mergeWithMove #-}
1695−mergeWithMove :: forall k x. (HasSpiderTimeline x, GCompare k) => Dynamic x (PatchDMapWithMove k (Event x)) -> Event x (DMap k Identity)
1742+mergeWithMove :: forall k v q x. (HasSpiderTimeline x, GCompare k)
1743+ => (forall a. q a -> Event x (v a))
1744+ -> DynamicS x (PatchDMapWithMove k q) -> Event x (DMap k v)
1696−mergeWithMove d = cacheEvent (mergeCheapWithMove d)
1745+mergeWithMove nt d = cacheEvent (mergeCheapWithMove nt d)
1698{-# INLINE [1] mergeCheap #-}
1747{-# INLINE [1] mergeCheap #-}
1699−mergeCheap :: forall k x. (HasSpiderTimeline x, GCompare k) => Dynamic x (PatchDMap k (Event x)) -> Event x (DMap k Identity)
1749+ :: forall k x q v. (HasSpiderTimeline x, GCompare k)
1750+ => (forall a. q a -> Event x (v a))
1751+ -> DynamicS x (PatchDMap k q)
1752+ -> Event x (DMap k v)
1700−mergeCheap = mergeCheap' getInitialSubscribers updateMe destroy
1753+mergeCheap nt = mergeGCheap' getInitialSubscribers updateMe destroy
1702− updateMe :: MergeUpdateFunc k x (PatchDMap k (Event x)) (MergeSubscribedParent x)
1755+ updateMe :: MergeUpdateFunc k v x (PatchDMap k q) (MergeSubscribedParent x)
1703 updateMe subscriber heightBagRef oldParents (PatchDMap p) = do
1756 updateMe subscriber heightBagRef oldParents (PatchDMap p) = do
1704 let f (subscriptionsToKill, ps) (k :=> ComposeMaybe me) = do
1757 let f (subscriptionsToKill, ps) (k :=> ComposeMaybe me) = do
1705 (mOldSubd, newPs) <- case me of
1758 (mOldSubd, newPs) <- case me of
1706 Nothing -> return $ DMap.updateLookupWithKey (\_ _ -> Nothing) k ps
1759 Nothing -> return $ DMap.updateLookupWithKey (\_ _ -> Nothing) k ps
1707 Just e -> do
1760 Just e -> do
1708 let s = subscriber $ return k
1761 let s = subscriber $ return k
1709− subscription@(EventSubscription _ subd) <- subscribe e s
1762+ subscription@(EventSubscription _ subd) <- subscribe (nt e) s
1710 newParentHeight <- liftIO $ getEventSubscribedHeight subd
1763 newParentHeight <- liftIO $ getEventSubscribedHeight subd
1711 let newParent = MergeSubscribedParent subscription
1764 let newParent = MergeSubscribedParent subscription
1712 liftIO $ modifyIORef' heightBagRef $ heightBagAdd newParentHeight
1765 liftIO $ modifyIORef' heightBagRef $ heightBagAdd newParentHeight
@@ -1716,28 +1769,33 @@ mergeCheap = mergeCheap' getInitialSubscribers updateMe destroy
1716 liftIO $ modifyIORef heightBagRef $ heightBagRemove oldHeight
1769 liftIO $ modifyIORef heightBagRef $ heightBagRemove oldHeight
1717 return (maybeToList (unMergeSubscribedParent <$> mOldSubd) ++ subscriptionsToKill, newPs)
1770 return (maybeToList (unMergeSubscribedParent <$> mOldSubd) ++ subscriptionsToKill, newPs)
1718 foldM f ([], oldParents) $ DMap.toList p
1771 foldM f ([], oldParents) $ DMap.toList p
1719− getInitialSubscribers :: MergeInitFunc k x (MergeSubscribedParent x)
1773+ getInitialSubscribers :: MergeInitFunc k v q x (MergeSubscribedParent x)
1720 getInitialSubscribers initialParents subscriber = do
1774 getInitialSubscribers initialParents subscriber = do
1721 subscribers <- forM (DMap.toList initialParents) $ \(k :=> e) -> do
1775 subscribers <- forM (DMap.toList initialParents) $ \(k :=> e) -> do
1722 let s = subscriber $ return k
1776 let s = subscriber $ return k
1723− (subscription@(EventSubscription _ parentSubd), parentOcc) <- subscribeAndRead e s
1777+ (subscription@(EventSubscription _ parentSubd), parentOcc) <- subscribeAndRead (nt e) s
1724 height <- liftIO $ getEventSubscribedHeight parentSubd
1778 height <- liftIO $ getEventSubscribedHeight parentSubd
1725− return (fmap (\x -> k :=> Identity x) parentOcc, height, k :=> MergeSubscribedParent subscription)
1779+ return (fmap (k :=>) parentOcc, height, k :=> MergeSubscribedParent subscription)
1726 return ( DMap.fromDistinctAscList $ mapMaybe (\(x, _, _) -> x) subscribers
1780 return ( DMap.fromDistinctAscList $ mapMaybe (\(x, _, _) -> x) subscribers
1727 , fmap (\(_, h, _) -> h) subscribers --TODO: Assert that there's no invalidHeight in here
1781 , fmap (\(_, h, _) -> h) subscribers --TODO: Assert that there's no invalidHeight in here
1728 , DMap.fromDistinctAscList $ map (\(_, _, x) -> x) subscribers
1782 , DMap.fromDistinctAscList $ map (\(_, _, x) -> x) subscribers
1730 destroy :: MergeDestroyFunc k (MergeSubscribedParent x)
1785 destroy :: MergeDestroyFunc k (MergeSubscribedParent x)
1731 destroy s = forM_ (DMap.toList s) $ \(_ :=> MergeSubscribedParent sub) -> unsubscribe sub
1786 destroy s = forM_ (DMap.toList s) $ \(_ :=> MergeSubscribedParent sub) -> unsubscribe sub
1733{-# INLINE [1] mergeCheapWithMove #-}
1788{-# INLINE [1] mergeCheapWithMove #-}
1734−mergeCheapWithMove :: forall k x. (HasSpiderTimeline x, GCompare k) => Dynamic x (PatchDMapWithMove k (Event x)) -> Event x (DMap k Identity)
1789+mergeCheapWithMove :: forall k x v q. (HasSpiderTimeline x, GCompare k)
1790+ => (forall a. q a -> Event x (v a))
1791+ -> DynamicS x (PatchDMapWithMove k q)
1792+ -> Event x (DMap k v)
1735−mergeCheapWithMove = mergeCheap' getInitialSubscribers updateMe destroy
1793+mergeCheapWithMove nt = mergeGCheap' getInitialSubscribers updateMe destroy
1737− updateMe :: MergeUpdateFunc k x (PatchDMapWithMove k (Event x)) (MergeSubscribedParentWithMove x k)
1795+ updateMe :: MergeUpdateFunc k v x (PatchDMapWithMove k q) (MergeSubscribedParentWithMove x k)
1738 updateMe subscriber heightBagRef oldParents p = do
1796 updateMe subscriber heightBagRef oldParents p = do
1739 -- Prepare new parents for insertion
1797 -- Prepare new parents for insertion
1740− let subscribeParent :: forall a. k a -> Event x a -> EventM x (MergeSubscribedParentWithMove x k a)
1798+ let subscribeParent :: forall a. k a -> Event x (v a) -> EventM x (MergeSubscribedParentWithMove x k a)
1741 subscribeParent k e = do
1799 subscribeParent k e = do
1742 keyRef <- liftIO $ newIORef k
1800 keyRef <- liftIO $ newIORef k
1743 let s = subscriber $ liftIO $ readIORef keyRef
1801 let s = subscriber $ liftIO $ readIORef keyRef
@@ -1746,9 +1804,9 @@ mergeCheapWithMove = mergeCheap' getInitialSubscribers updateMe destroy
1746 newParentHeight <- getEventSubscribedHeight subd
1804 newParentHeight <- getEventSubscribedHeight subd
1747 modifyIORef' heightBagRef $ heightBagAdd newParentHeight
1805 modifyIORef' heightBagRef $ heightBagAdd newParentHeight
1748 return $ MergeSubscribedParentWithMove subscription keyRef
1806 return $ MergeSubscribedParentWithMove subscription keyRef
1749− p' <- PatchDMapWithMove.traversePatchDMapWithMoveWithKey subscribeParent p
1807+ p' <- PatchDMapWithMove.traversePatchDMapWithMoveWithKey (\k q -> subscribeParent k (nt q)) p
1750 -- Collect old parents for deletion and update the keys of moved parents
1808 -- Collect old parents for deletion and update the keys of moved parents
1751− let moveOrDelete :: forall a. k a -> PatchDMapWithMove.NodeInfo k (Event x) a -> MergeSubscribedParentWithMove x k a -> Constant (EventM x (Maybe (EventSubscription x))) a
1809+ let moveOrDelete :: forall a. k a -> PatchDMapWithMove.NodeInfo k q a -> MergeSubscribedParentWithMove x k a -> Constant (EventM x (Maybe (EventSubscription x))) a
1752 moveOrDelete _ ni parent = Constant $ case getComposeMaybe $ PatchDMapWithMove._nodeInfo_to ni of
1810 moveOrDelete _ ni parent = Constant $ case getComposeMaybe $ PatchDMapWithMove._nodeInfo_to ni of
1753 Nothing -> do
1811 Nothing -> do
1754 oldHeight <- liftIO $ getEventSubscribedHeight $ _eventSubscription_subscribed $ _mergeSubscribedParentWithMove_subscription parent
1812 oldHeight <- liftIO $ getEventSubscribedHeight $ _eventSubscription_subscribed $ _mergeSubscribedParentWithMove_subscription parent
@@ -1759,46 +1817,47 @@ mergeCheapWithMove = mergeCheap' getInitialSubscribers updateMe destroy
1759 return Nothing
1817 return Nothing
1760 toDelete <- fmap catMaybes $ mapM (\(_ :=> v) -> getConstant v) $ DMap.toList $ DMap.intersectionWithKey moveOrDelete (unPatchDMapWithMove p) oldParents
1818 toDelete <- fmap catMaybes $ mapM (\(_ :=> v) -> getConstant v) $ DMap.toList $ DMap.intersectionWithKey moveOrDelete (unPatchDMapWithMove p) oldParents
1761 return (toDelete, applyAlways p' oldParents)
1819 return (toDelete, applyAlways p' oldParents)
1762− getInitialSubscribers :: MergeInitFunc k x (MergeSubscribedParentWithMove x k)
1820+ getInitialSubscribers :: MergeInitFunc k v q x (MergeSubscribedParentWithMove x k)
1763 getInitialSubscribers initialParents subscriber = do
1821 getInitialSubscribers initialParents subscriber = do
1764 subscribers <- forM (DMap.toList initialParents) $ \(k :=> e) -> do
1822 subscribers <- forM (DMap.toList initialParents) $ \(k :=> e) -> do
1765 keyRef <- liftIO $ newIORef k
1823 keyRef <- liftIO $ newIORef k
1766 let s = subscriber $ liftIO $ readIORef keyRef
1824 let s = subscriber $ liftIO $ readIORef keyRef
1767− (subscription@(EventSubscription _ parentSubd), parentOcc) <- subscribeAndRead e s
1825+ (subscription@(EventSubscription _ parentSubd), parentOcc) <- subscribeAndRead (nt e) s
1768 height <- liftIO $ getEventSubscribedHeight parentSubd
1826 height <- liftIO $ getEventSubscribedHeight parentSubd
1769− return (fmap (\x -> k :=> Identity x) parentOcc, height, k :=> MergeSubscribedParentWithMove subscription keyRef)
1827+ return (fmap (k :=>) parentOcc, height, k :=> MergeSubscribedParentWithMove subscription keyRef)
1770 return ( DMap.fromDistinctAscList $ mapMaybe (\(x, _, _) -> x) subscribers
1828 return ( DMap.fromDistinctAscList $ mapMaybe (\(x, _, _) -> x) subscribers
1771 , fmap (\(_, h, _) -> h) subscribers --TODO: Assert that there's no invalidHeight in here
1829 , fmap (\(_, h, _) -> h) subscribers --TODO: Assert that there's no invalidHeight in here
1772 , DMap.fromDistinctAscList $ map (\(_, _, x) -> x) subscribers
1830 , DMap.fromDistinctAscList $ map (\(_, _, x) -> x) subscribers
1774 destroy :: MergeDestroyFunc k (MergeSubscribedParentWithMove x k)
1833 destroy :: MergeDestroyFunc k (MergeSubscribedParentWithMove x k)
1775 destroy s = forM_ (DMap.toList s) $ \(_ :=> MergeSubscribedParentWithMove sub _) -> unsubscribe sub
1834 destroy s = forM_ (DMap.toList s) $ \(_ :=> MergeSubscribedParentWithMove sub _) -> unsubscribe sub
1777−type MergeUpdateFunc k x p s
1836+type MergeUpdateFunc k v x p s
1778− = (forall a. EventM x (k a) -> Subscriber x a)
1837+ = (forall a. EventM x (k a) -> Subscriber x (v a))
1779 -> IORef HeightBag
1838 -> IORef HeightBag
1780 -> DMap k s
1839 -> DMap k s
1782 -> EventM x ([EventSubscription x], DMap k s)
1841 -> EventM x ([EventSubscription x], DMap k s)
1784−type MergeInitFunc k x s
1843+type MergeInitFunc k v q x s
1785− = DMap k (Event x)
1844+ = DMap k q
1786− -> (forall a. EventM x (k a) -> Subscriber x a)
1845+ -> (forall a. EventM x (k a) -> Subscriber x (v a))
1787− -> EventM x (DMap k Identity, [Height], DMap k s)
1846+ -> EventM x (DMap k v, [Height], DMap k s)
1789type MergeDestroyFunc k s
1848type MergeDestroyFunc k s
1790 = DMap k s
1849 = DMap k s
1791 -> IO ()
1850 -> IO ()
1793−data Merge x k s = Merge
1852+data Merge x k v s = Merge
1794 { _merge_parentsRef :: {-# UNPACK #-} !(IORef (DMap k s))
1853 { _merge_parentsRef :: {-# UNPACK #-} !(IORef (DMap k s))
1795 , _merge_heightBagRef :: {-# UNPACK #-} !(IORef HeightBag)
1854 , _merge_heightBagRef :: {-# UNPACK #-} !(IORef HeightBag)
1796 , _merge_heightRef :: {-# UNPACK #-} !(IORef Height)
1855 , _merge_heightRef :: {-# UNPACK #-} !(IORef Height)
1797− , _merge_sub :: {-# UNPACK #-} !(Subscriber x (DMap k Identity))
1856+ , _merge_sub :: {-# UNPACK #-} !(Subscriber x (DMap k v))
1798− , _merge_accumRef :: {-# UNPACK #-} !(IORef (DMap k Identity))
1857+ , _merge_accumRef :: {-# UNPACK #-} !(IORef (DMap k v))
1801−invalidateMergeHeight :: Merge x k s -> IO ()
1860+invalidateMergeHeight :: Merge x k v s -> IO ()
1802invalidateMergeHeight m = invalidateMergeHeight' (_merge_heightRef m) (_merge_sub m)
1861invalidateMergeHeight m = invalidateMergeHeight' (_merge_heightRef m) (_merge_sub m)
1804invalidateMergeHeight' :: IORef Height -> Subscriber x a -> IO ()
1863invalidateMergeHeight' :: IORef Height -> Subscriber x a -> IO ()
@@ -1808,8 +1867,7 @@ invalidateMergeHeight' heightRef sub = do
1808 writeIORef heightRef $! invalidHeight
1867 writeIORef heightRef $! invalidHeight
1809 subscriberInvalidateHeight sub oldHeight
1868 subscriberInvalidateHeight sub oldHeight
1812−revalidateMergeHeight :: Merge x k s -> IO ()
1870+revalidateMergeHeight :: Merge x k v s -> IO ()
1813revalidateMergeHeight m = do
1871revalidateMergeHeight m = do
1814 currentHeight <- readIORef $ _merge_heightRef m
1872 currentHeight <- readIORef $ _merge_heightRef m
1815 when (currentHeight == invalidHeight) $ do -- revalidateMergeHeight may be called multiple times; perhaps the's a way to finesse it to avoid this check
1873 when (currentHeight == invalidHeight) $ do -- revalidateMergeHeight may be called multiple times; perhaps the's a way to finesse it to avoid this check
@@ -1825,19 +1883,19 @@ revalidateMergeHeight m = do
1825 subscriberRecalculateHeight (_merge_sub m) height
1883 subscriberRecalculateHeight (_merge_sub m) height
1826 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
1884 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
1828−scheduleMergeSelf :: HasSpiderTimeline x => Merge x k s -> Height -> EventM x ()
1886+scheduleMergeSelf :: HasSpiderTimeline x => Merge x k v s -> Height -> EventM x ()
1829scheduleMergeSelf m height = scheduleMerge' height (_merge_heightRef m) $ do
1887scheduleMergeSelf m height = scheduleMerge' height (_merge_heightRef m) $ do
1830 vals <- liftIO $ readIORef $ _merge_accumRef m
1888 vals <- liftIO $ readIORef $ _merge_accumRef m
1831 liftIO $ writeIORef (_merge_accumRef m) $! DMap.empty -- Once we're done with this, we can clear it immediately, because if there's a cacheEvent in front of us, it'll handle subsequent subscribers, and if not, we won't get subsequent subscribers
1889 liftIO $ writeIORef (_merge_accumRef m) $! DMap.empty -- Once we're done with this, we can clear it immediately, because if there's a cacheEvent in front of us, it'll handle subsequent subscribers, and if not, we won't get subsequent subscribers
1832 --TODO: Assert that m is not empty
1890 --TODO: Assert that m is not empty
1833 subscriberPropagate (_merge_sub m) vals
1891 subscriberPropagate (_merge_sub m) vals
1835−mergeSubscriber :: forall x k s a. (HasSpiderTimeline x, GCompare k) => Merge x k s -> EventM x (k a) -> Subscriber x a
1893+mergeSubscriber :: forall x k v s a. (HasSpiderTimeline x, GCompare k) => Merge x k v s -> EventM x (k a) -> Subscriber x (v a)
1836mergeSubscriber m getKey = Subscriber
1894mergeSubscriber m getKey = Subscriber
1837 { subscriberPropagate = \a -> do
1895 { subscriberPropagate = \a -> do
1838 oldM <- liftIO $ readIORef $ _merge_accumRef m
1896 oldM <- liftIO $ readIORef $ _merge_accumRef m
1839 k <- getKey
1897 k <- getKey
1840− let newM = DMap.insertWith (error $ "Same key fired multiple times for Merge") k (Identity a) oldM
1898+ let newM = DMap.insertWith (error $ "Same key fired multiple times for Merge") k a oldM
1841 tracePropagate (Proxy :: Proxy x) $ " DMap.size oldM = " <> show (DMap.size oldM) <> "; DMap.size newM = " <> show (DMap.size newM)
1899 tracePropagate (Proxy :: Proxy x) $ " DMap.size oldM = " <> show (DMap.size oldM) <> "; DMap.size newM = " <> show (DMap.size newM)
1842 liftIO $ writeIORef (_merge_accumRef m) $! newM
1900 liftIO $ writeIORef (_merge_accumRef m) $! newM
1843 when (DMap.null oldM) $ do -- Only schedule the firing once
1901 when (DMap.null oldM) $ do -- Only schedule the firing once
@@ -1868,7 +1926,7 @@ mergeSubscriber m getKey = Subscriber
1870--TODO: Be able to run as much of this as possible promptly
1928--TODO: Be able to run as much of this as possible promptly
1871−updateMerge :: (HasSpiderTimeline x, GCompare k) => Merge x k s -> MergeUpdateFunc k x p s -> p -> SomeMergeUpdate x
1929+updateMerge :: (HasSpiderTimeline x, GCompare k) => Merge x k v s -> MergeUpdateFunc k v x p s -> p -> SomeMergeUpdate x
1872updateMerge m updateFunc p = SomeMergeUpdate updateMe (invalidateMergeHeight m) (revalidateMergeHeight m)
1930updateMerge m updateFunc p = SomeMergeUpdate updateMe (invalidateMergeHeight m) (revalidateMergeHeight m)
1873 where updateMe = do
1931 where updateMe = do
1874 oldParents <- liftIO $ readIORef $ _merge_parentsRef m
1932 oldParents <- liftIO $ readIORef $ _merge_parentsRef m
@@ -1876,9 +1934,10 @@ updateMerge m updateFunc p = SomeMergeUpdate updateMe (invalidateMergeHeight m)
1876 liftIO $ writeIORef (_merge_parentsRef m) $! newParents
1934 liftIO $ writeIORef (_merge_parentsRef m) $! newParents
1877 return subscriptionsToKill
1935 return subscriptionsToKill
1879−{-# INLINE mergeCheap' #-}
1937+{-# INLINE mergeGCheap' #-}
1938+mergeGCheap' :: forall k v x p s q. (HasSpiderTimeline x, GCompare k, PatchTarget p ~ DMap k q)
1880−mergeCheap' :: forall k x p s. (HasSpiderTimeline x, GCompare k, PatchTarget p ~ DMap k (Event x)) => MergeInitFunc k x s -> MergeUpdateFunc k x p s -> MergeDestroyFunc k s -> Dynamic x p -> Event x (DMap k Identity)
1939+ => MergeInitFunc k v q x s -> MergeUpdateFunc k v x p s -> MergeDestroyFunc k s -> DynamicS x p -> Event x (DMap k v)
1881−mergeCheap' getInitialSubscribers updateFunc destroy d = Event $ \sub -> do
1940+mergeGCheap' getInitialSubscribers updateFunc destroy d = Event $ \sub -> do
1882 initialParents <- readBehaviorUntracked $ dynamicCurrent d
1941 initialParents <- readBehaviorUntracked $ dynamicCurrent d
1883 accumRef <- liftIO $ newIORef $ error "merge: accumRef not yet initialized"
1942 accumRef <- liftIO $ newIORef $ error "merge: accumRef not yet initialized"
1884 heightRef <- liftIO $ newIORef $ error "merge: heightRef not yet initialized"
1943 heightRef <- liftIO $ newIORef $ error "merge: heightRef not yet initialized"
@@ -1926,11 +1985,11 @@ mergeCheap' getInitialSubscribers updateFunc destroy d = Event $ \sub -> do
1929−mergeInt :: forall x a. (HasSpiderTimeline x) => Dynamic x (PatchIntMap (Event x a)) -> Event x (IntMap a)
1988+mergeInt :: forall x a. (HasSpiderTimeline x) => DynamicS x (PatchIntMap (Event x a)) -> Event x (IntMap a)
1930mergeInt = cacheEvent . mergeIntCheap
1989mergeInt = cacheEvent . mergeIntCheap
1932{-# INLINABLE mergeIntCheap #-}
1991{-# INLINABLE mergeIntCheap #-}
1933−mergeIntCheap :: forall x a. (HasSpiderTimeline x) => Dynamic x (PatchIntMap (Event x a)) -> Event x (IntMap a)
1992+mergeIntCheap :: forall x a. (HasSpiderTimeline x) => DynamicS x (PatchIntMap (Event x a)) -> Event x (IntMap a)
1934mergeIntCheap d = Event $ \sub -> do
1993mergeIntCheap d = Event $ \sub -> do
1935 initialParents <- readBehaviorUntracked $ dynamicCurrent d
1994 initialParents <- readBehaviorUntracked $ dynamicCurrent d
1936 accum <- liftIO $ FastMutableIntMap.newEmpty
1995 accum <- liftIO $ FastMutableIntMap.newEmpty
@@ -2027,14 +2086,16 @@ mergeIntCheap d = Event $ \sub -> do
2029newtype EventSelector x k = EventSelector { select :: forall a. k a -> Event x a }
2088newtype EventSelector x k = EventSelector { select :: forall a. k a -> Event x a }
2089+newtype EventSelectorG x k v = EventSelectorG { selectG :: forall a. k a -> Event x (v a) }
2031−fan :: (HasSpiderTimeline x, GCompare k) => Event x (DMap k Identity) -> EventSelector x k
2091+fanG :: (HasSpiderTimeline x, GCompare k) => Event x (DMap k v) -> EventSelectorG x k v
2032−fan e =
2092+fanG e = unsafePerformIO $ do
2093+ ref <- newIORef Nothing
2033 let f = Fan
2094 let f = Fan
2034 { fanParent = e
2095 { fanParent = e
2035− , fanSubscribed = unsafeNewIORef e Nothing
2096+ , fanSubscribed = ref
2037− in EventSelector $ \k -> eventFan k f
2098+ pure $ EventSelectorG $ \k -> eventFan k f
2039runHoldInits :: HasSpiderTimeline x => IORef [SomeHoldInit x] -> IORef [SomeDynInit x] -> IORef [SomeMergeInit x] -> EventM x ()
2100runHoldInits :: HasSpiderTimeline x => IORef [SomeHoldInit x] -> IORef [SomeDynInit x] -> IORef [SomeMergeInit x] -> EventM x ()
2040runHoldInits holdInitRef dynInitRef mergeInitRef = do
2101runHoldInits holdInitRef dynInitRef mergeInitRef = do
@@ -2088,7 +2149,7 @@ clearEventEnv (EventEnv toAssignRef holdInitRef dynInitRef mergeUpdateRef mergeI
2088-- | Run an event action outside of a frame
2149-- | Run an event action outside of a frame
2089runFrame :: forall x a. HasSpiderTimeline x => EventM x a -> SpiderHost x a --TODO: This function also needs to hold the mutex
2150runFrame :: forall x a. HasSpiderTimeline x => EventM x a -> SpiderHost x a --TODO: This function also needs to hold the mutex
2090runFrame a = SpiderHost $ do
2151runFrame a = SpiderHost $ do
2091− let env = _spiderTimeline_eventEnv (spiderTimeline :: SpiderTimelineEnv x)
2152+ let env = _spiderTimeline_eventEnv $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
2092 let go = do
2153 let go = do
2093 result <- a
2154 result <- a
2094 runHoldInits (eventEnvHoldInits env) (eventEnvDynInits env) (eventEnvMergeInits env) -- This must happen before doing the assignments, in case subscribing a Hold causes existing Holds to be read by the newly-propagated events
2155 runHoldInits (eventEnvHoldInits env) (eventEnvDynInits env) (eventEnvMergeInits env) -- This must happen before doing the assignments, in case subscribing a Hold causes existing Holds to be read by the newly-propagated events
@@ -2146,7 +2207,7 @@ runFrame a = SpiderHost $ do
2146 myHeight <- readIORef $ switchSubscribedHeight subscribed
2207 myHeight <- readIORef $ switchSubscribedHeight subscribed
2147 when (parentHeight /= myHeight) $ do
2208 when (parentHeight /= myHeight) $ do
2148 writeIORef (switchSubscribedHeight subscribed) $! invalidHeight
2209 writeIORef (switchSubscribedHeight subscribed) $! invalidHeight
2149− WeakBag.traverse (switchSubscribedSubscribers subscribed) $ invalidateSubscriberHeight myHeight
2210+ WeakBag.traverse_ (switchSubscribedSubscribers subscribed) $ invalidateSubscriberHeight myHeight
2150 mapM_ _someMergeUpdate_invalidateHeight mergeUpdates --TODO: In addition to when the patch is completely empty, we should also not run this if it has some Nothing values, but none of them have actually had any effect; potentially, we could even check for Just values with no effect (e.g. by comparing their IORefs and ignoring them if they are unchanged); actually, we could just check if the new height is different
2211 mapM_ _someMergeUpdate_invalidateHeight mergeUpdates --TODO: In addition to when the patch is completely empty, we should also not run this if it has some Nothing values, but none of them have actually had any effect; potentially, we could even check for Just values with no effect (e.g. by comparing their IORefs and ignoring them if they are unchanged); actually, we could just check if the new height is different
2151 forM_ coincidenceInfos $ \(SomeResetCoincidence subscription mcs) -> do
2212 forM_ coincidenceInfos $ \(SomeResetCoincidence subscription mcs) -> do
2152 unsubscribe subscription
2213 unsubscribe subscription
@@ -2187,7 +2248,7 @@ invalidateCoincidenceHeight subscribed = do
2187 oldHeight <- readIORef $ coincidenceSubscribedHeight subscribed
2248 oldHeight <- readIORef $ coincidenceSubscribedHeight subscribed
2188 when (oldHeight /= invalidHeight) $ do
2249 when (oldHeight /= invalidHeight) $ do
2189 writeIORef (coincidenceSubscribedHeight subscribed) $! invalidHeight
2250 writeIORef (coincidenceSubscribedHeight subscribed) $! invalidHeight
2190− WeakBag.traverse (coincidenceSubscribedSubscribers subscribed) $ invalidateSubscriberHeight oldHeight
2251+ WeakBag.traverse_ (coincidenceSubscribedSubscribers subscribed) $ invalidateSubscriberHeight oldHeight
2192updateSwitchHeight :: Height -> SwitchSubscribed x a -> IO ()
2253updateSwitchHeight :: Height -> SwitchSubscribed x a -> IO ()
2193updateSwitchHeight new subscribed = do
2254updateSwitchHeight new subscribed = do
@@ -2195,7 +2256,7 @@ updateSwitchHeight new subscribed = do
2195 when (oldHeight == invalidHeight) $ do --TODO: This 'when' should probably be an assertion
2256 when (oldHeight == invalidHeight) $ do --TODO: This 'when' should probably be an assertion
2196 when (new /= invalidHeight) $ do --TODO: This 'when' should probably be an assertion
2257 when (new /= invalidHeight) $ do --TODO: This 'when' should probably be an assertion
2197 writeIORef (switchSubscribedHeight subscribed) $! new
2258 writeIORef (switchSubscribedHeight subscribed) $! new
2198− WeakBag.traverse (switchSubscribedSubscribers subscribed) $ recalculateSubscriberHeight new
2259+ WeakBag.traverse_ (switchSubscribedSubscribers subscribed) $ recalculateSubscriberHeight new
2200recalculateCoincidenceHeight :: CoincidenceSubscribed x a -> IO ()
2261recalculateCoincidenceHeight :: CoincidenceSubscribed x a -> IO ()
2201recalculateCoincidenceHeight subscribed = do
2262recalculateCoincidenceHeight subscribed = do
@@ -2204,7 +2265,7 @@ recalculateCoincidenceHeight subscribed = do
2204 height <- calculateCoincidenceHeight subscribed
2265 height <- calculateCoincidenceHeight subscribed
2205 when (height /= invalidHeight) $ do
2266 when (height /= invalidHeight) $ do
2206 writeIORef (coincidenceSubscribedHeight subscribed) $! height
2267 writeIORef (coincidenceSubscribedHeight subscribed) $! height
2207− WeakBag.traverse (coincidenceSubscribedSubscribers subscribed) $ recalculateSubscriberHeight height
2268+ WeakBag.traverse_ (coincidenceSubscribedSubscribers subscribed) $ recalculateSubscriberHeight height
2209calculateSwitchHeight :: SwitchSubscribed x a -> IO Height
2270calculateSwitchHeight :: SwitchSubscribed x a -> IO Height
2210calculateSwitchHeight subscribed = getEventSubscribedHeight . _eventSubscription_subscribed =<< readIORef (switchSubscribedCurrentParent subscribed)
2271calculateSwitchHeight subscribed = getEventSubscribedHeight . _eventSubscription_subscribed =<< readIORef (switchSubscribedCurrentParent subscribed)
@@ -2299,7 +2360,7 @@ instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x))
2299 fail _ = error "Dynamic does not support 'fail'"
2360 fail _ = error "Dynamic does not support 'fail'"
2301{-# INLINABLE newJoinDyn #-}
2362{-# INLINABLE newJoinDyn #-}
2302−newJoinDyn :: HasSpiderTimeline x => Reflex.Spider.Internal.Dynamic x (Identity (Reflex.Spider.Internal.Dynamic x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
2363+newJoinDyn :: HasSpiderTimeline x => DynamicS x (Identity (DynamicS x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
2303newJoinDyn d =
2364newJoinDyn d =
2304 let readV0 = readBehaviorTracked . dynamicCurrent =<< readBehaviorTracked (dynamicCurrent d)
2365 let readV0 = readBehaviorTracked . dynamicCurrent =<< readBehaviorTracked (dynamicCurrent d)
2305 eOuter = Reflex.Spider.Internal.push (fmap (Just . Identity) . readBehaviorUntracked . dynamicCurrent . runIdentity) $ dynamicUpdated d
2366 eOuter = Reflex.Spider.Internal.push (fmap (Just . Identity) . readBehaviorUntracked . dynamicCurrent . runIdentity) $ dynamicUpdated d
@@ -2449,7 +2510,7 @@ unsafeNewSpiderTimelineEnv = do
2449#ifdef DEBUG
2510#ifdef DEBUG
2450 depthRef <- newIORef 0
2511 depthRef <- newIORef 0
2452− return $ SpiderTimelineEnv
2513+ return $ STE $ SpiderTimelineEnv
2453 { _spiderTimeline_lock = lock
2514 { _spiderTimeline_lock = lock
2454 , _spiderTimeline_eventEnv = env
2515 , _spiderTimeline_eventEnv = env
2455#ifdef DEBUG
2516#ifdef DEBUG
@@ -2461,17 +2522,17 @@ unsafeNewSpiderTimelineEnv = do
2461newSpiderTimeline :: IO (Some SpiderTimelineEnv)
2522newSpiderTimeline :: IO (Some SpiderTimelineEnv)
2462newSpiderTimeline = withSpiderTimeline (pure . Some)
2523newSpiderTimeline = withSpiderTimeline (pure . Some)
2464−data LocalSpiderTimeline x s
2525+data LocalSpiderTimeline (x :: Type) s
2466instance Reifies s (SpiderTimelineEnv x) =>
2527instance Reifies s (SpiderTimelineEnv x) =>
2467 HasSpiderTimeline (LocalSpiderTimeline x s) where
2528 HasSpiderTimeline (LocalSpiderTimeline x s) where
2468− spiderTimeline = localSpiderTimeline (Proxy :: Proxy s) $ reflect (Proxy :: Proxy s)
2529+ spiderTimeline = localSpiderTimeline Proxy $ reflect (Proxy :: Proxy s)
2470localSpiderTimeline
2531localSpiderTimeline
2471− :: Proxy s
2532+ :: proxy s
2472 -> SpiderTimelineEnv x
2533 -> SpiderTimelineEnv x
2473 -> SpiderTimelineEnv (LocalSpiderTimeline x s)
2534 -> SpiderTimelineEnv (LocalSpiderTimeline x s)
2474−localSpiderTimeline _ = unsafeCoerce
2535+localSpiderTimeline _ = coerce
2476-- | Pass a new timeline to the given function.
2537-- | Pass a new timeline to the given function.
2477withSpiderTimeline :: (forall x. HasSpiderTimeline x => SpiderTimelineEnv x -> IO r) -> IO r
2538withSpiderTimeline :: (forall x. HasSpiderTimeline x => SpiderTimelineEnv x -> IO r) -> IO r
@@ -2479,18 +2540,18 @@ withSpiderTimeline k = do
2479 env <- unsafeNewSpiderTimelineEnv
2540 env <- unsafeNewSpiderTimelineEnv
2480 reify env $ \s -> k $ localSpiderTimeline s env
2541 reify env $ \s -> k $ localSpiderTimeline s env
2482−newtype SpiderPullM x a = SpiderPullM (BehaviorM x a) deriving (Functor, Applicative, Monad, MonadIO, MonadFix)
2543+newtype SpiderPullM (x :: Type) a = SpiderPullM (BehaviorM x a) deriving (Functor, Applicative, Monad, MonadIO, MonadFix)
2484type ComputeM = EventM
2545type ComputeM = EventM
2486−newtype SpiderPushM x a = SpiderPushM (ComputeM x a) deriving (Functor, Applicative, Monad, MonadIO, MonadFix)
2547+newtype SpiderPushM (x :: Type) a = SpiderPushM (ComputeM x a) deriving (Functor, Applicative, Monad, MonadIO, MonadFix)
2488instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2549instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2489 {-# SPECIALIZE instance R.Reflex (SpiderTimeline Global) #-}
2550 {-# SPECIALIZE instance R.Reflex (SpiderTimeline Global) #-}
2490 newtype Behavior (SpiderTimeline x) a = SpiderBehavior { unSpiderBehavior :: Behavior x a }
2551 newtype Behavior (SpiderTimeline x) a = SpiderBehavior { unSpiderBehavior :: Behavior x a }
2491 newtype Event (SpiderTimeline x) a = SpiderEvent { unSpiderEvent :: Event x a }
2552 newtype Event (SpiderTimeline x) a = SpiderEvent { unSpiderEvent :: Event x a }
2492− newtype Dynamic (SpiderTimeline x) a = SpiderDynamic { unSpiderDynamic :: Dynamic x (Identity a) } -- deriving (Functor, Applicative, Monad)
2553+ newtype Dynamic (SpiderTimeline x) a = SpiderDynamic { unSpiderDynamic :: DynamicS x (Identity a) } -- deriving (Functor, Applicative, Monad)
2493− newtype Incremental (SpiderTimeline x) p = SpiderIncremental { unSpiderIncremental :: Dynamic x p }
2554+ newtype Incremental (SpiderTimeline x) p = SpiderIncremental { unSpiderIncremental :: DynamicS x p }
2494 type PullM (SpiderTimeline x) = SpiderPullM x
2555 type PullM (SpiderTimeline x) = SpiderPullM x
2495 type PushM (SpiderTimeline x) = SpiderPushM x
2556 type PushM (SpiderTimeline x) = SpiderPushM x
2496 {-# INLINABLE never #-}
2557 {-# INLINABLE never #-}
@@ -2503,10 +2564,15 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2503 pushCheap f = SpiderEvent . pushCheap (coerce f) . unSpiderEvent
2564 pushCheap f = SpiderEvent . pushCheap (coerce f) . unSpiderEvent
2504 {-# INLINABLE pull #-}
2565 {-# INLINABLE pull #-}
2505 pull = SpiderBehavior . pull . coerce
2566 pull = SpiderBehavior . pull . coerce
2506− {-# INLINABLE merge #-}
2567+ {-# INLINABLE fanG #-}
2507− merge = SpiderEvent . merge . dynamicConst . (coerce :: DMap k (R.Event (SpiderTimeline x)) -> DMap k (Event x))
2508− {-# INLINABLE fan #-}
2509− fan e = R.EventSelector $ SpiderEvent . select (fan (unSpiderEvent e))
2568+ fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
2569+ {-# INLINABLE mergeG #-}
2571+ :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2572+ => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
2574+ -> R.Event (SpiderTimeline x) (DMap k v)
2575+ mergeG nt = SpiderEvent . mergeG (unSpiderEvent #. nt) . dynamicConst
2510 {-# INLINABLE switch #-}
2576 {-# INLINABLE switch #-}
2511 switch = SpiderEvent . switch . (coerce :: Behavior x (R.Event (SpiderTimeline x) a) -> Behavior x (Event x a)) . unSpiderBehavior
2577 switch = SpiderEvent . switch . (coerce :: Behavior x (R.Event (SpiderTimeline x) a) -> Behavior x (Event x a)) . unSpiderBehavior
2512 {-# INLINABLE coincidence #-}
2578 {-# INLINABLE coincidence #-}
@@ -2514,15 +2580,15 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2514 {-# INLINABLE current #-}
2580 {-# INLINABLE current #-}
2515 current = SpiderBehavior . dynamicCurrent . unSpiderDynamic
2581 current = SpiderBehavior . dynamicCurrent . unSpiderDynamic
2516 {-# INLINABLE updated #-}
2582 {-# INLINABLE updated #-}
2517− updated = coerce $ SpiderEvent . dynamicUpdated . unSpiderDynamic
2583+ updated = SpiderEvent #. dynamicUpdated .# fmap coerce . unSpiderDynamic
2518 {-# INLINABLE unsafeBuildDynamic #-}
2584 {-# INLINABLE unsafeBuildDynamic #-}
2519 unsafeBuildDynamic readV0 v' = SpiderDynamic $ dynamicDynIdentity $ unsafeBuildDynamic (coerce readV0) $ coerce $ unSpiderEvent v'
2585 unsafeBuildDynamic readV0 v' = SpiderDynamic $ dynamicDynIdentity $ unsafeBuildDynamic (coerce readV0) $ coerce $ unSpiderEvent v'
2520 {-# INLINABLE unsafeBuildIncremental #-}
2586 {-# INLINABLE unsafeBuildIncremental #-}
2521 unsafeBuildIncremental readV0 dv = SpiderIncremental $ dynamicDyn $ unsafeBuildDynamic (coerce readV0) $ unSpiderEvent dv
2587 unsafeBuildIncremental readV0 dv = SpiderIncremental $ dynamicDyn $ unsafeBuildDynamic (coerce readV0) $ unSpiderEvent dv
2522− {-# INLINABLE mergeIncremental #-}
2588+ {-# INLINABLE mergeIncrementalG #-}
2523− mergeIncremental = SpiderEvent . merge . (unsafeCoerce :: Dynamic x (PatchDMap k (R.Event (SpiderTimeline x))) -> Dynamic x (PatchDMap k (Event x))) . unSpiderIncremental
2589+ mergeIncrementalG nt = SpiderEvent #. mergeG (coerce #. nt) .# unSpiderIncremental
2524− {-# INLINABLE mergeIncrementalWithMove #-}
2590+ {-# INLINABLE mergeIncrementalWithMoveG #-}
2525− mergeIncrementalWithMove = SpiderEvent . mergeWithMove . (unsafeCoerce :: Dynamic x (PatchDMapWithMove k (R.Event (SpiderTimeline x))) -> Dynamic x (PatchDMapWithMove k (Event x))) . unSpiderIncremental
2591+ mergeIncrementalWithMoveG nt = SpiderEvent #. mergeWithMove (coerce #. nt) .# unSpiderIncremental
2526 {-# INLINABLE currentIncremental #-}
2592 {-# INLINABLE currentIncremental #-}
2527 currentIncremental = SpiderBehavior . dynamicCurrent . unSpiderIncremental
2593 currentIncremental = SpiderBehavior . dynamicCurrent . unSpiderIncremental
2528 {-# INLINABLE updatedIncremental #-}
2594 {-# INLINABLE updatedIncremental #-}
@@ -2533,9 +2599,10 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2533 return $ Identity <$> apply p c --TODO: Avoid the redundant 'apply'
2599 return $ Identity <$> apply p c --TODO: Avoid the redundant 'apply'
2534 eventCoercion Coercion = Coercion
2600 eventCoercion Coercion = Coercion
2535 behaviorCoercion Coercion = Coercion
2601 behaviorCoercion Coercion = Coercion
2536− dynamicCoercion = unsafeCoerce --TODO: How can we avoid this unsafeCoerce? This is safe only because we know how Identity works as a Patch instance
2602+ dynamicCoercion Coercion = Coercion
2603+ incrementalCoercion Coercion Coercion = Coercion
2537 {-# INLINABLE mergeIntIncremental #-}
2604 {-# INLINABLE mergeIntIncremental #-}
2538− mergeIntIncremental = SpiderEvent . mergeInt . (unsafeCoerce :: Dynamic x (PatchIntMap (R.Event (SpiderTimeline x) a)) -> Dynamic x (PatchIntMap (Event x a))) . unSpiderIncremental
2605+ mergeIntIncremental = SpiderEvent . mergeInt . coerce
2539 {-# INLINABLE fanInt #-}
2606 {-# INLINABLE fanInt #-}
2540 fanInt e = R.EventSelectorInt $ SpiderEvent . selectInt (fanInt (unSpiderEvent e))
2607 fanInt e = R.EventSelectorInt $ SpiderEvent . selectInt (fanInt (unSpiderEvent e))
@@ -2560,7 +2627,7 @@ instance MonadAtomicRef (EventM x) where
2560 atomicModifyRef r f = liftIO $ atomicModifyRef r f
2627 atomicModifyRef r f = liftIO $ atomicModifyRef r f
2562-- | The monad for actions that manipulate a Spider timeline identified by @x@
2629-- | The monad for actions that manipulate a Spider timeline identified by @x@
2563−newtype SpiderHost x a = SpiderHost { unSpiderHost :: IO a } deriving (Functor, Applicative, MonadFix, MonadIO, MonadException, MonadAsyncException)
2630+newtype SpiderHost (x :: Type) a = SpiderHost { unSpiderHost :: IO a } deriving (Functor, Applicative, MonadFix, MonadIO, MonadException, MonadAsyncException)
2565instance Monad (SpiderHost x) where
2632instance Monad (SpiderHost x) where
2566 {-# INLINABLE (>>=) #-}
2633 {-# INLINABLE (>>=) #-}
@@ -2582,7 +2649,7 @@ runSpiderHost (SpiderHost a) = a
2582runSpiderHostForTimeline :: SpiderHost x a -> SpiderTimelineEnv x -> IO a
2649runSpiderHostForTimeline :: SpiderHost x a -> SpiderTimelineEnv x -> IO a
2583runSpiderHostForTimeline (SpiderHost a) _ = a
2650runSpiderHostForTimeline (SpiderHost a) _ = a
2585−newtype SpiderHostFrame x a = SpiderHostFrame { runSpiderHostFrame :: EventM x a }
2652+newtype SpiderHostFrame (x :: Type) a = SpiderHostFrame { runSpiderHostFrame :: EventM x a }
2586 deriving (Functor, Applicative, MonadFix, MonadIO, MonadException, MonadAsyncException)
2653 deriving (Functor, Applicative, MonadFix, MonadIO, MonadException, MonadAsyncException)
2588instance Monad (SpiderHostFrame x) where
2655instance Monad (SpiderHostFrame x) where