@@ -16,15 +16,9 @@
1616{-# LANGUAGE TypeFamilies #-}
1717{-# LANGUAGE TypeOperators #-}
1818{-# LANGUAGE UndecidableInstances #-}
19−{-# LANGUAGE PolyKinds #-}
20−{-# LANGUAGE DeriveFunctor #-}
21−{-# LANGUAGE StandaloneDeriving #-}
22−{-# LANGUAGE InstanceSigs #-}
23−
2419#ifdef USE_REFLEX_OPTIMIZER
2520{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
2621#endif
27−{-# OPTIONS_GHC -Wunused-binds #-}
2822-- | This module is the implementation of the 'Spider' 'Reflex' engine. It uses
2923-- a graph traversal algorithm to propagate 'Event's and 'Behavior's.
3024module Reflex.Spider.Internal (module Reflex.Spider.Internal) where
@@ -38,10 +32,7 @@ import Control.Monad hiding (forM, forM_, mapM, mapM_)
3832import Control.Monad.Exception
3933import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
4034import Control.Monad.Primitive
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
35+import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_)
4536import Control.Monad.Ref
4637import Data.Align
4738import Data.Coerce
@@ -57,7 +48,6 @@ import Data.GADT.Compare
5748import Data.IntMap.Strict (IntMap)
5849import qualified Data.IntMap.Strict as IntMap
5950import Data.IORef
60−import Data.Kind (Type)
6151import Data.Maybe hiding (mapMaybe)
6252import Data.Monoid ((<>))
6353import Data.Proxy
@@ -94,7 +84,6 @@ import qualified Data.FastWeakBag as FastWeakBag
9484import Data.Reflection
9585import Data.Some (Some(Some))
9686import Data.Type.Coercion
97−import Data.Profunctor.Unsafe ((#.), (.#))
9887import Data.WeakBag (WeakBag, WeakBagTicket, _weakBag_children)
9988import qualified Data.WeakBag as WeakBag
10089import qualified Reflex.Class
@@ -211,6 +200,12 @@ nextNodeIdRef = unsafePerformIO $ newIORef 1
211200
212201newNodeId :: IO Int
213202newNodeId = atomicModifyIORef' nextNodeIdRef $ \n -> (succ n, n)
203+
204+{-# NOINLINE unsafeNodeId #-}
205+unsafeNodeId :: a -> Int
206+unsafeNodeId a = unsafePerformIO $ do
207+ touch a
208+ newNodeId
214209#endif
215210
216211--------------------------------------------------------------------------------
@@ -314,10 +309,9 @@ cacheEvent e =
314309#else
315310 Event $
316311#endif
317− unsafePerformIO $ do
318− mSubscribedRef :: IORef (FastWeak (CacheSubscribed x a))
319− <- newIORef emptyFastWeak
320− pure $ \sub -> {-# SCC "cacheEvent" #-} do
312+ let mSubscribedRef :: IORef (FastWeak (CacheSubscribed x a))
313+ !mSubscribedRef = unsafeNewIORef e emptyFastWeak
314+ in \sub -> {-# SCC "cacheEvent" #-} do
321315#ifdef DEBUG_TRACE_EVENTS
322316 unless (BS8.null callSite) $ liftIO $ BS8.hPutStrLn stderr callSite
323317#endif
@@ -383,7 +377,7 @@ eventRoot !k !r = Event $ wrap eventSubscribedRoot $ liftIO . getRootSubscribed
383377eventNever :: Event x a
384378eventNever = Event $ \_ -> return (EventSubscription (return ()) eventSubscribedNever, Nothing)
385379
386−eventFan :: (GCompare k, HasSpiderTimeline x) => k a -> Fan x k v -> Event x (v a)
380+eventFan :: (GCompare k, HasSpiderTimeline x) => k a -> Fan x k -> Event x a
387381eventFan !k !f = Event $ wrap eventSubscribedFan $ getFanSubscribed k f
388382
389383eventSwitch :: HasSpiderTimeline x => Switch x a -> Event x a
@@ -435,14 +429,14 @@ newSubscriberHold h = return $ Subscriber
435429 , subscriberRecalculateHeight = \_ -> return ()
436430 }
437431
438−newSubscriberFan :: forall x k v. (HasSpiderTimeline x, GCompare k) => FanSubscribed x k v -> IO (Subscriber x (DMap k v))
432+newSubscriberFan :: forall x k. (HasSpiderTimeline x, GCompare k) => FanSubscribed x k -> IO (Subscriber x (DMap k Identity))
439433newSubscriberFan subscribed = return $ Subscriber
440434 { subscriberPropagate = \a -> {-# SCC "traverseFan" #-} do
441435 subs <- liftIO $ readIORef $ fanSubscribedSubscribers subscribed
442436 tracePropagate (Proxy :: Proxy x) $ "SubscriberFan" <> showNodeId subscribed <> ": " ++ show (DMap.size subs) ++ " keys subscribed, " ++ show (DMap.size a) ++ " keys firing"
443437 liftIO $ writeIORef (fanSubscribedOccurrence subscribed) $ Just a
444438 scheduleClear $ fanSubscribedOccurrence subscribed
445− let f _ (Pair v subsubs) = do
439+ let f _ (Pair (Identity v) subsubs) = do
446440 propagate v $ _fanSubscribedChildren_list subsubs
447441 return $ Constant ()
448442 _ <- DMap.traverseWithKey f $ DMap.intersectionWithKey (\_ -> Pair) a subs --TODO: Would be nice to have DMap.traverse_
@@ -558,11 +552,6 @@ propagateFast a subscribers = withIncreasedDepth $ do
558552toAny :: a -> Any
559553toAny = unsafeCoerce
560554
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
565−-- perfectly safe.
566555data EventSubscribed x = EventSubscribed
567556 { eventSubscribedHeightRef :: {-# UNPACK #-} !(IORef Height)
568557 , eventSubscribedRetained :: {-# NOUNPACK #-} !Any
@@ -595,7 +584,7 @@ eventSubscribedNever = EventSubscribed
595584#endif
596585 }
597586
598−eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
587+eventSubscribedFan :: FanSubscribed x k -> EventSubscribed x
599588eventSubscribedFan !subscribed = EventSubscribed
600589 { eventSubscribedHeightRef = eventSubscribedHeightRef $ _eventSubscription_subscribed $ fanSubscribedParent subscribed
601590 , eventSubscribedRetained = toAny subscribed
@@ -686,7 +675,7 @@ behaviorPull !p = Behavior $ do
686675 wi <- liftIO $ mkWeakPtrWithDebug i "InvalidatorPull"
687676 parentsRef <- liftIO $ newIORef []
688677 holdInits <- askBehaviorHoldInits
689− a <- liftIO $ runReaderIO (unBehaviorM $ pullCompute p) (Just (wi, parentsRef), holdInits)
678+ a <- liftIO $ runReaderT (unBehaviorM $ pullCompute p) (Just (wi, parentsRef), holdInits)
690679 invsRef <- liftIO . newIORef . maybeToList =<< askInvalidator
691680 parents <- liftIO $ readIORef parentsRef
692681 let subscribed = PullSubscribed
@@ -721,46 +710,42 @@ readBehaviorUntracked b = do
721710-- Dynamic
722711--------------------------------------------------------------------------------
723712
724−type DynamicS x p = Dynamic x (PatchTarget p) p
725−
726−data Dynamic x target p = Dynamic
727− { dynamicCurrent :: !(Behavior x target)
713+data Dynamic x p = Dynamic
714+ { dynamicCurrent :: !(Behavior x (PatchTarget p))
728715 , dynamicUpdated :: Event x p -- This must be lazy; see the comment on holdEvent --TODO: Would this let us eliminate `Dyn`?
729716 }
730717
731−deriving instance (HasSpiderTimeline x) => Functor (Dynamic x target)
732−
733−
734−
735−
736−dynamicHold :: Hold x p -> DynamicS x p
718+dynamicHold :: Hold x p -> Dynamic x p
737719dynamicHold !h = Dynamic
738720 { dynamicCurrent = behaviorHold h
739721 , dynamicUpdated = eventHold h
740722 }
741723
742−dynamicHoldIdentity :: Hold x (Identity a) -> DynamicS x (Identity a)
724+dynamicHoldIdentity :: Hold x (Identity a) -> Dynamic x (Identity a)
743725dynamicHoldIdentity = dynamicHold
744726
745−dynamicConst :: PatchTarget p -> DynamicS x p
727+dynamicConst :: PatchTarget p -> Dynamic x p
746728dynamicConst !a = Dynamic
747729 { dynamicCurrent = behaviorConst a
748730 , dynamicUpdated = eventNever
749731 }
750732
751−dynamicDyn :: (HasSpiderTimeline x, Patch p) => Dyn x p -> DynamicS x p
733+dynamicDyn :: (HasSpiderTimeline x, Patch p) => Dyn x p -> Dynamic x p
752734dynamicDyn !d = Dynamic
753735 { dynamicCurrent = behaviorDyn d
754736 , dynamicUpdated = eventDyn d
755737 }
756738
757−dynamicDynIdentity :: HasSpiderTimeline x => Dyn x (Identity a) -> DynamicS x (Identity a)
739+dynamicDynIdentity :: HasSpiderTimeline x => Dyn x (Identity a) -> Dynamic x (Identity a)
758740dynamicDynIdentity = dynamicDyn
759741
760742--------------------------------------------------------------------------------
761743-- Combinators
762744--------------------------------------------------------------------------------
763745
746+--TODO: Figure out why certain things are not 'representational', then make them
747+--representational so we can use coerce
748+
764749--type role Hold representational
765750data Hold x p
766751 = Hold { holdValue :: !(IORef (PatchTarget p))
@@ -781,25 +766,20 @@ globalSpiderTimelineEnv = unsafePerformIO unsafeNewSpiderTimelineEnv
781766
782767-- | Stores all global data relevant to a particular Spider timeline; only one
783768-- 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
788−
789−data SpiderTimelineEnv' x = SpiderTimelineEnv
769+data SpiderTimelineEnv x = SpiderTimelineEnv
790770 { _spiderTimeline_lock :: {-# UNPACK #-} !(MVar ())
791771 , _spiderTimeline_eventEnv :: {-# UNPACK #-} !(EventEnv x)
792772#ifdef DEBUG
793773 , _spiderTimeline_depth :: {-# UNPACK #-} !(IORef Int)
794774#endif
795775 }
796−type role SpiderTimelineEnv' phantom
776+type role SpiderTimelineEnv nominal
797777
798778instance Eq (SpiderTimelineEnv x) where
799779 _ == _ = True -- Since only one exists of each type
800780
801781instance GEq SpiderTimelineEnv where
802− a `geq` b = if _spiderTimeline_lock (unSTE a) == _spiderTimeline_lock (unSTE b)
782+ a `geq` b = if _spiderTimeline_lock a == _spiderTimeline_lock b
803783 then Just $ unsafeCoerce Refl -- This unsafeCoerce is safe because the same SpiderTimelineEnv can't have two different 'x' arguments
804784 else Nothing
805785
@@ -822,7 +802,7 @@ runEventM :: EventM x a -> IO a
822802runEventM = unEventM
823803
824804asksEventEnv :: forall x a. HasSpiderTimeline x => (EventEnv x -> a) -> EventM x a
825−asksEventEnv f = return $ f $ _spiderTimeline_eventEnv (unSTE (spiderTimeline :: SpiderTimelineEnv x))
805+asksEventEnv f = return $ f $ _spiderTimeline_eventEnv (spiderTimeline :: SpiderTimelineEnv x)
826806
827807class MonadIO m => Defer a m where
828808 getDeferralQueue :: m (IORef [a])
@@ -960,9 +940,9 @@ getHoldEventSubscription h = do
960940
961941type BehaviorEnv x = (Maybe (Weak (Invalidator x), IORef [SomeBehaviorSubscribed x]), IORef [SomeHoldInit x])
962942
943+--type role BehaviorM representational
963944-- BehaviorM can sample behaviors
964−newtype BehaviorM x a = BehaviorM { unBehaviorM :: ReaderIO (BehaviorEnv x) a }
965− deriving (Functor, Applicative, MonadIO, MonadFix, MonadReader (BehaviorEnv x))
945+newtype BehaviorM x a = BehaviorM { unBehaviorM :: ReaderT (BehaviorEnv x) IO a } deriving (Functor, Applicative, MonadIO, MonadFix)
966946
967947instance Monad (BehaviorM x) where
968948 {-# INLINE (>>=) #-}
@@ -1013,7 +993,7 @@ data RootSubscribed x a = forall k. GCompare k => RootSubscribed
1013993#endif
1014994 }
1015995
1016−data Root x k
996+data Root x (k :: * -> *)
1017997 = Root { rootOccurrence :: !(IORef (DMap k Identity)) -- The currently-firing occurrence of this event
1018998 , rootSubscribed :: !(IORef (DMap k (RootSubscribed x)))
1019999 , rootInit :: !(forall a. k a -> RootTrigger x a -> IO (IO ()))
@@ -1083,25 +1063,25 @@ heightBagVerify b@(HeightBag s c) = if
10831063heightBagVerify = id
10841064#endif
10851065
1086−data FanSubscribedChildren x k v a = FanSubscribedChildren
1087− { _fanSubscribedChildren_list :: !(WeakBag (Subscriber x (v a)))
1088− , _fanSubscribedChildren_self :: {-# NOUNPACK #-} !(k a, FanSubscribed x k v)
1089− , _fanSubscribedChildren_weakSelf :: !(IORef (Weak (k a, FanSubscribed x k v)))
1066+data FanSubscribedChildren (x :: *) k a = FanSubscribedChildren
1067+ { _fanSubscribedChildren_list :: !(WeakBag (Subscriber x a))
1068+ , _fanSubscribedChildren_self :: {-# NOUNPACK #-} !(k a, FanSubscribed x k)
1069+ , _fanSubscribedChildren_weakSelf :: !(IORef (Weak (k a, FanSubscribed x k)))
10901070 }
10911071
1092−data FanSubscribed x k v
1093− = FanSubscribed { fanSubscribedCachedSubscribed :: !(IORef (Maybe (FanSubscribed x k v)))
1094− , fanSubscribedOccurrence :: !(IORef (Maybe (DMap k v)))
1095− , fanSubscribedSubscribers :: !(IORef (DMap k (FanSubscribedChildren x k v))) -- This DMap should never be empty
1072+data FanSubscribed (x :: *) k
1073+ = FanSubscribed { fanSubscribedCachedSubscribed :: !(IORef (Maybe (FanSubscribed x k)))
1074+ , fanSubscribedOccurrence :: !(IORef (Maybe (DMap k Identity)))
1075+ , fanSubscribedSubscribers :: !(IORef (DMap k (FanSubscribedChildren x k))) -- This DMap should never be empty
10961076 , fanSubscribedParent :: !(EventSubscription x)
10971077#ifdef DEBUG_NODEIDS
10981078 , fanSubscribedNodeId :: Int
10991079#endif
11001080 }
11011081
1102−data Fan x k v
1103− = Fan { fanParent :: !(Event x (DMap k v))
1104− , fanSubscribed :: !(IORef (Maybe (FanSubscribed x k v)))
1082+data Fan x k
1083+ = Fan { fanParent :: !(Event x (DMap k Identity))
1084+ , fanSubscribed :: !(IORef (Maybe (FanSubscribed x k)))
11051085 }
11061086
11071087data SwitchSubscribed x a
@@ -1163,8 +1143,7 @@ instance HasSpiderTimeline x => Align (Event x) where
11631143#if MIN_VERSION_these(0, 8, 0)
11641144instance HasSpiderTimeline x => Semialign (Event x) where
11651145#endif
1166− align ea eb = mapMaybe dmapToThese $ mergeG coerce $ dynamicConst $
1167− DMap.fromDistinctAscList [LeftTag :=> ea, RightTag :=> eb]
1146+ align ea eb = mapMaybe dmapToThese $ merge $ dynamicConst $ DMap.fromDistinctAscList [LeftTag :=> ea, RightTag :=> eb]
11681147
11691148#ifdef MIN_VERSION_semialign
11701149#if MIN_VERSION_semialign(1,1,0)
@@ -1177,13 +1156,13 @@ data DynType x p = UnsafeDyn !(BehaviorM x (PatchTarget p), Event x p)
11771156 | BuildDyn !(EventM x (PatchTarget p), Event x p)
11781157 | HoldDyn !(Hold x p)
11791158
1180−newtype Dyn (x :: Type) p = Dyn { unDyn :: IORef (DynType x p) }
1159+newtype Dyn x p = Dyn { unDyn :: IORef (DynType x p) }
11811160
1182−newMapDyn :: HasSpiderTimeline x => (a -> b) -> DynamicS x (Identity a) -> DynamicS x (Identity b)
1161+newMapDyn :: HasSpiderTimeline x => (a -> b) -> Dynamic x (Identity a) -> Dynamic x (Identity b)
11831162newMapDyn f d = dynamicDynIdentity $ unsafeBuildDynamic (fmap f $ readBehaviorTracked $ dynamicCurrent d) (Identity . f . runIdentity <$> dynamicUpdated d)
11841163
11851164--TODO: Avoid the duplication between this and R.zipDynWith
1186−zipDynWith :: HasSpiderTimeline x => (a -> b -> c) -> DynamicS x (Identity a) -> DynamicS x (Identity b) -> DynamicS x (Identity c)
1165+zipDynWith :: HasSpiderTimeline x => (a -> b -> c) -> Dynamic x (Identity a) -> Dynamic x (Identity b) -> Dynamic x (Identity c)
11871166zipDynWith f da db =
11881167 let eab = align (dynamicUpdated da) (dynamicUpdated db)
11891168 ec = flip push eab $ \o -> do
@@ -1206,12 +1185,18 @@ buildDynamic readV0 v' = do
12061185 return d
12071186
12081187unsafeBuildDynamic :: BehaviorM x (PatchTarget p) -> Event x p -> Dyn x p
1209−unsafeBuildDynamic readV0 v' =
1210− Dyn $ unsafePerformIO $ newIORef $ UnsafeDyn (readV0, v')
1188+unsafeBuildDynamic readV0 v' = Dyn $ unsafeNewIORef x $ UnsafeDyn x
1189+ where x = (readV0, v')
12111190
12121191-- ResultM can read behaviors and events
12131192type ResultM = EventM
12141193
1194+{-# NOINLINE unsafeNewIORef #-}
1195+unsafeNewIORef :: a -> b -> IORef b
1196+unsafeNewIORef a b = unsafePerformIO $ do
1197+ touch a
1198+ newIORef b
1199+
12151200instance HasSpiderTimeline x => Functor (Event x) where
12161201 fmap f = push $ return . Just . f
12171202
@@ -1224,42 +1209,33 @@ push f e = cacheEvent (pushCheap f e)
12241209
12251210{-# INLINABLE pull #-}
12261211pull :: BehaviorM x a -> Behavior x a
1227−pull a = unsafePerformIO $ do
1228− ref <- newIORef Nothing
1229−#ifdef DEBUG_NODEIDS
1230− nid <- newNodeId
1231−#endif
1232− pure $ behaviorPull $ Pull
1233− { pullCompute = a
1234− , pullValue = ref
1212+pull a = behaviorPull $ Pull
1213+ { pullCompute = a
1214+ , pullValue = unsafeNewIORef a Nothing
12351215#ifdef DEBUG_NODEIDS
1236− , pullNodeId = nid
1216+ , pullNodeId = unsafeNodeId a
12371217#endif
1238− }
1218+ }
12391219
12401220{-# INLINABLE switch #-}
12411221switch :: HasSpiderTimeline x => Behavior x (Event x a) -> Event x a
1242−switch a = unsafePerformIO $ do
1243− ref <- newIORef Nothing
1244− pure $ eventSwitch $ Switch
1245− { switchParent = a
1246− , switchSubscribed = ref
1247− }
1222+switch a = eventSwitch $ Switch
1223+ { switchParent = a
1224+ , switchSubscribed = unsafeNewIORef a Nothing
1225+ }
12481226
12491227coincidence :: HasSpiderTimeline x => Event x (Event x a) -> Event x a
1250−coincidence a = unsafePerformIO $ do
1251− ref <- newIORef Nothing
1252− pure $ eventCoincidence $ Coincidence
1253− { coincidenceParent = a
1254− , coincidenceSubscribed = ref
1255− }
1228+coincidence a = eventCoincidence $ Coincidence
1229+ { coincidenceParent = a
1230+ , coincidenceSubscribed = unsafeNewIORef a Nothing
1231+ }
12561232
12571233-- Propagate the given event occurrence; before cleaning up, run the given action, which may read the state of events and behaviors
12581234run :: forall x b. HasSpiderTimeline x => [DSum (RootTrigger x) Identity] -> ResultM x b -> SpiderHost x b
12591235run roots after = do
12601236 tracePropagate (Proxy :: Proxy x) $ "Running an event frame with " <> show (length roots) <> " events"
12611237 let t = spiderTimeline :: SpiderTimelineEnv x
1262− result <- SpiderHost $ withMVar (_spiderTimeline_lock (unSTE t)) $ \_ -> unSpiderHost $ runFrame $ do
1238+ result <- SpiderHost $ withMVar (_spiderTimeline_lock t) $ \_ -> unSpiderHost $ runFrame $ do
12631239 rootsToPropagate <- forM roots $ \r@(RootTrigger (_, occRef, k) :=> a) -> do
12641240 occBefore <- liftIO $ do
12651241 occBefore <- readIORef occRef
@@ -1323,9 +1299,9 @@ type WeakList a = [Weak a]
13231299withIncreasedDepth :: CanTrace x m => m a -> m a
13241300withIncreasedDepth a = do
13251301 spiderTimeline <- askSpiderTimelineEnv
1326− liftIO $ modifyIORef' (_spiderTimeline_depth (unSTE spiderTimeline)) succ
1302+ liftIO $ modifyIORef' (_spiderTimeline_depth spiderTimeline) succ
13271303 result <- a
1328− liftIO $ modifyIORef' (_spiderTimeline_depth (unSTE spiderTimeline)) pred
1304+ liftIO $ modifyIORef' (_spiderTimeline_depth spiderTimeline) pred
13291305 return result
13301306#else
13311307withIncreasedDepth :: m a -> m a
@@ -1352,7 +1328,7 @@ traceMWhen _ b getMessage = when b $ do
13521328 message <- getMessage
13531329#ifdef DEBUG
13541330 spiderTimeline <- askSpiderTimelineEnv
1355− d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE spiderTimeline
1331+ d <- liftIO $ readIORef $ _spiderTimeline_depth spiderTimeline
13561332#else
13571333 let d = 0
13581334#endif
@@ -1393,25 +1369,25 @@ propagateSubscriberHold h a = do
13931369data SomeResetCoincidence x = forall a. SomeResetCoincidence !(EventSubscription x) !(Maybe (CoincidenceSubscribed x a)) -- The CoincidenceSubscriber will be present only if heights need to be reset
13941370
13951371runBehaviorM :: BehaviorM x a -> Maybe (Weak (Invalidator x), IORef [SomeBehaviorSubscribed x]) -> IORef [SomeHoldInit x] -> IO a
1396−runBehaviorM a mwi holdInits = runReaderIO (unBehaviorM a) (mwi, holdInits)
1372+runBehaviorM a mwi holdInits = runReaderT (unBehaviorM a) (mwi, holdInits)
13971373
13981374askInvalidator :: BehaviorM x (Maybe (Weak (Invalidator x)))
13991375askInvalidator = do
1400− (!m, _) <- ask
1376+ (!m, _) <- BehaviorM ask
14011377 case m of
14021378 Nothing -> return Nothing
14031379 Just (!wi, _) -> return $ Just wi
14041380
14051381askParentsRef :: BehaviorM x (Maybe (IORef [SomeBehaviorSubscribed x]))
14061382askParentsRef = do
1407− (!m, _) <- ask
1383+ (!m, _) <- BehaviorM ask
14081384 case m of
14091385 Nothing -> return Nothing
14101386 Just (_, !p) -> return $ Just p
14111387
14121388askBehaviorHoldInits :: BehaviorM x (IORef [SomeHoldInit x])
14131389askBehaviorHoldInits = do
1414− (_, !his) <- ask
1390+ (_, !his) <- BehaviorM ask
14151391 return his
14161392
14171393{-# INLINE getDynHold #-}
@@ -1456,9 +1432,6 @@ getRootSubscribed k r sub = do
14561432 when debugPropagate $ putStrLn $ "getRootSubscribed: calling rootInit"
14571433 uninit <- rootInit r k $ RootTrigger (subs, rootOccurrence r, k)
14581434 writeIORef uninitRef $! uninit
1459−#ifdef DEBUG_NODEIDS
1460− nid <- newNodeId
1461−#endif
14621435 let !subscribed = RootSubscribed
14631436 { rootSubscribedKey = k
14641437 , rootSubscribedCachedSubscribed = cached
@@ -1467,7 +1440,7 @@ getRootSubscribed k r sub = do
14671440 , rootSubscribedUninit = uninit
14681441 , rootSubscribedWeakSelf = weakSelf
14691442#ifdef DEBUG_NODEIDS
1470− , rootSubscribedNodeId = nid
1443+ , rootSubscribedNodeId = unsafeNodeId (k, r, subs)
14711444#endif
14721445 }
14731446 -- If we die at the same moment that all our children die, they will
@@ -1516,10 +1489,16 @@ newFanInt = do
15161489 , _fanInt_occRef = occRef
15171490 }
15181491
1492+{-# NOINLINE unsafeNewFanInt #-}
1493+unsafeNewFanInt :: b -> FanInt x a
1494+unsafeNewFanInt b = unsafePerformIO $ do
1495+ touch b
1496+ newFanInt
1497+
15191498fanInt :: HasSpiderTimeline x => Event x (IntMap a) -> EventSelectorInt x a
1520−fanInt p = unsafePerformIO $ do
1521− self <- newFanInt
1522− pure $ EventSelectorInt $ \k -> Event $ \sub -> do
1499+fanInt p =
1500+ let self = unsafeNewFanInt p
1501+ in EventSelectorInt $ \k -> Event $ \sub -> do
15231502 isEmpty <- liftIO $ FastMutableIntMap.isEmpty (_fanInt_subscribers self)
15241503 when isEmpty $ do -- This is the first subscriber, so we need to subscribe to our input
15251504 (subscription, parentOcc) <- subscribeAndRead p $ Subscriber
@@ -1555,7 +1534,7 @@ fanInt p = unsafePerformIO $ do
15551534 return (EventSubscription (FastWeakBag.remove t) $! EventSubscribed heightRef $! toAny (_fanInt_subscriptionRef self, t), IntMap.lookup k currentOcc)
15561535
15571536{-# INLINABLE getFanSubscribed #-}
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))
1537+getFanSubscribed :: (HasSpiderTimeline x, GCompare k) => k a -> Fan x k -> Subscriber x a -> EventM x (WeakBagTicket, FanSubscribed x k, Maybe a)
15591538getFanSubscribed k f sub = do
15601539 mSubscribed <- liftIO $ readIORef $ fanSubscribed f
15611540 case mSubscribed of
@@ -1573,16 +1552,13 @@ getFanSubscribed k f sub = do
15731552 subscribersRef <- liftIO $ newIORef $ error "getFanSubscribed: subscribersRef not yet initialized"
15741553 occRef <- liftIO $ newIORef parentOcc
15751554 when (isJust parentOcc) $ scheduleClear occRef
1576−#ifdef DEBUG_NODEIDS
1577− nid <- liftIO newNodeId
1578−#endif
15791555 let subscribed = FanSubscribed
15801556 { fanSubscribedCachedSubscribed = fanSubscribed f
15811557 , fanSubscribedOccurrence = occRef
15821558 , fanSubscribedParent = subscription
15831559 , fanSubscribedSubscribers = subscribersRef
15841560#ifdef DEBUG_NODEIDS
1585− , fanSubscribedNodeId = nid
1561+ , fanSubscribedNodeId = unsafeNodeId f
15861562#endif
15871563 }
15881564 let !self = (k, subscribed)
@@ -1592,7 +1568,7 @@ getFanSubscribed k f sub = do
15921568 liftIO $ writeIORef (fanSubscribed f) $ Just subscribed
15931569 return (slnForSub, subscribed, coerce $ DMap.lookup k =<< parentOcc)
15941570
1595−cleanupFanSubscribed :: GCompare k => (k a, FanSubscribed x k v) -> IO ()
1571+cleanupFanSubscribed :: GCompare k => (k a, FanSubscribed x k) -> IO ()
15961572cleanupFanSubscribed (k, subscribed) = do
15971573 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
15981574 let reducedSubscribers = DMap.delete k subscribers
@@ -1604,7 +1580,7 @@ cleanupFanSubscribed (k, subscribed) = do
16041580 else writeIORef (fanSubscribedSubscribers subscribed) $! reducedSubscribers
16051581
16061582{-# INLINE subscribeFanSubscribed #-}
1607−subscribeFanSubscribed :: GCompare k => k a -> FanSubscribed x k v -> Subscriber x (v a) -> IO WeakBagTicket
1583+subscribeFanSubscribed :: GCompare k => k a -> FanSubscribed x k -> Subscriber x a -> IO WeakBagTicket
16081584subscribeFanSubscribed k subscribed sub = do
16091585 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
16101586 case DMap.lookup k subscribers of
@@ -1642,9 +1618,6 @@ getSwitchSubscribed s sub = do
16421618 when (isJust parentOcc) $ scheduleClear occRef
16431619 weakSelf <- liftIO $ newIORef $ error "getSwitchSubscribed: weakSelf not yet initialized"
16441620 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupSwitchSubscribed
1645−#ifdef DEBUG_NODEIDS
1646− nid <- liftIO newNodeId
1647−#endif
16481621 let !subscribed = SwitchSubscribed
16491622 { switchSubscribedCachedSubscribed = switchSubscribed s
16501623 , switchSubscribedOccurrence = occRef
@@ -1657,7 +1630,7 @@ getSwitchSubscribed s sub = do
16571630 , switchSubscribedCurrentParent = subscriptionRef
16581631 , switchSubscribedWeakSelf = weakSelf
16591632#ifdef DEBUG_NODEIDS
1660− , switchSubscribedNodeId = nid
1633+ , switchSubscribedNodeId = unsafeNodeId s
16611634#endif
16621635 }
16631636 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "switchSubscribedWeakSelf"
@@ -1702,9 +1675,6 @@ getCoincidenceSubscribed c sub = do
17021675 scheduleClear innerSubdRef
17031676 weakSelf <- liftIO $ newIORef $ error "getCoincidenceSubscribed: weakSelf not yet implemented"
17041677 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupCoincidenceSubscribed
1705−#ifdef DEBUG_NODEIDS
1706− nid <- liftIO newNodeId
1707−#endif
17081678 let subscribed = CoincidenceSubscribed
17091679 { coincidenceSubscribedCachedSubscribed = coincidenceSubscribed c
17101680 , coincidenceSubscribedOccurrence = occRef
@@ -1715,7 +1685,7 @@ getCoincidenceSubscribed c sub = do
17151685 , coincidenceSubscribedInnerParent = innerSubdRef
17161686 , coincidenceSubscribedWeakSelf = weakSelf
17171687#ifdef DEBUG_NODEIDS
1718− , coincidenceSubscribedNodeId = nid
1688+ , coincidenceSubscribedNodeId = unsafeNodeId c
17191689#endif
17201690 }
17211691 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "CoincidenceSubscribed"
@@ -1732,34 +1702,26 @@ cleanupCoincidenceSubscribed subscribed = do
17321702subscribeCoincidenceSubscribed :: CoincidenceSubscribed x a -> Subscriber x a -> IO WeakBagTicket
17331703subscribeCoincidenceSubscribed subscribed sub = WeakBag.insert sub (coincidenceSubscribedSubscribers subscribed) (coincidenceSubscribedWeakSelf subscribed) cleanupCoincidenceSubscribed
17341704
1735−{-# INLINE mergeG #-}
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)
1739−mergeG nt d = cacheEvent (mergeCheap nt d)
1705+{-# INLINE merge #-}
1706+merge :: forall k x. (HasSpiderTimeline x, GCompare k) => Dynamic x (PatchDMap k (Event x)) -> Event x (DMap k Identity)
1707+merge d = cacheEvent (mergeCheap d)
17401708
17411709{-# INLINE mergeWithMove #-}
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)
1745−mergeWithMove nt d = cacheEvent (mergeCheapWithMove nt d)
1710+mergeWithMove :: forall k x. (HasSpiderTimeline x, GCompare k) => Dynamic x (PatchDMapWithMove k (Event x)) -> Event x (DMap k Identity)
1711+mergeWithMove d = cacheEvent (mergeCheapWithMove d)
17461712
17471713{-# INLINE [1] mergeCheap #-}
1748−mergeCheap
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)
1753−mergeCheap nt = mergeGCheap' getInitialSubscribers updateMe destroy
1714+mergeCheap :: forall k x. (HasSpiderTimeline x, GCompare k) => Dynamic x (PatchDMap k (Event x)) -> Event x (DMap k Identity)
1715+mergeCheap = mergeCheap' getInitialSubscribers updateMe destroy
17541716 where
1755− updateMe :: MergeUpdateFunc k v x (PatchDMap k q) (MergeSubscribedParent x)
1717+ updateMe :: MergeUpdateFunc k x (PatchDMap k (Event x)) (MergeSubscribedParent x)
17561718 updateMe subscriber heightBagRef oldParents (PatchDMap p) = do
17571719 let f (subscriptionsToKill, ps) (k :=> ComposeMaybe me) = do
17581720 (mOldSubd, newPs) <- case me of
17591721 Nothing -> return $ DMap.updateLookupWithKey (\_ _ -> Nothing) k ps
17601722 Just e -> do
17611723 let s = subscriber $ return k
1762− subscription@(EventSubscription _ subd) <- subscribe (nt e) s
1724+ subscription@(EventSubscription _ subd) <- subscribe e s
17631725 newParentHeight <- liftIO $ getEventSubscribedHeight subd
17641726 let newParent = MergeSubscribedParent subscription
17651727 liftIO $ modifyIORef' heightBagRef $ heightBagAdd newParentHeight
@@ -1769,33 +1731,28 @@ mergeCheap nt = mergeGCheap' getInitialSubscribers updateMe destroy
17691731 liftIO $ modifyIORef heightBagRef $ heightBagRemove oldHeight
17701732 return (maybeToList (unMergeSubscribedParent <$> mOldSubd) ++ subscriptionsToKill, newPs)
17711733 foldM f ([], oldParents) $ DMap.toList p
1772−
1773− getInitialSubscribers :: MergeInitFunc k v q x (MergeSubscribedParent x)
1734+ getInitialSubscribers :: MergeInitFunc k x (MergeSubscribedParent x)
17741735 getInitialSubscribers initialParents subscriber = do
17751736 subscribers <- forM (DMap.toList initialParents) $ \(k :=> e) -> do
17761737 let s = subscriber $ return k
1777− (subscription@(EventSubscription _ parentSubd), parentOcc) <- subscribeAndRead (nt e) s
1738+ (subscription@(EventSubscription _ parentSubd), parentOcc) <- subscribeAndRead e s
17781739 height <- liftIO $ getEventSubscribedHeight parentSubd
1779− return (fmap (k :=>) parentOcc, height, k :=> MergeSubscribedParent subscription)
1740+ return (fmap (\x -> k :=> Identity x) parentOcc, height, k :=> MergeSubscribedParent subscription)
17801741 return ( DMap.fromDistinctAscList $ mapMaybe (\(x, _, _) -> x) subscribers
17811742 , fmap (\(_, h, _) -> h) subscribers --TODO: Assert that there's no invalidHeight in here
17821743 , DMap.fromDistinctAscList $ map (\(_, _, x) -> x) subscribers
17831744 )
1784−
17851745 destroy :: MergeDestroyFunc k (MergeSubscribedParent x)
17861746 destroy s = forM_ (DMap.toList s) $ \(_ :=> MergeSubscribedParent sub) -> unsubscribe sub
17871747
17881748{-# INLINE [1] mergeCheapWithMove #-}
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)
1793−mergeCheapWithMove nt = mergeGCheap' getInitialSubscribers updateMe destroy
1749+mergeCheapWithMove :: forall k x. (HasSpiderTimeline x, GCompare k) => Dynamic x (PatchDMapWithMove k (Event x)) -> Event x (DMap k Identity)
1750+mergeCheapWithMove = mergeCheap' getInitialSubscribers updateMe destroy
17941751 where
1795− updateMe :: MergeUpdateFunc k v x (PatchDMapWithMove k q) (MergeSubscribedParentWithMove x k)
1752+ updateMe :: MergeUpdateFunc k x (PatchDMapWithMove k (Event x)) (MergeSubscribedParentWithMove x k)
17961753 updateMe subscriber heightBagRef oldParents p = do
17971754 -- Prepare new parents for insertion
1798− let subscribeParent :: forall a. k a -> Event x (v a) -> EventM x (MergeSubscribedParentWithMove x k a)
1755+ let subscribeParent :: forall a. k a -> Event x a -> EventM x (MergeSubscribedParentWithMove x k a)
17991756 subscribeParent k e = do
18001757 keyRef <- liftIO $ newIORef k
18011758 let s = subscriber $ liftIO $ readIORef keyRef
@@ -1804,9 +1761,9 @@ mergeCheapWithMove nt = mergeGCheap' getInitialSubscribers updateMe destroy
18041761 newParentHeight <- getEventSubscribedHeight subd
18051762 modifyIORef' heightBagRef $ heightBagAdd newParentHeight
18061763 return $ MergeSubscribedParentWithMove subscription keyRef
1807− p' <- PatchDMapWithMove.traversePatchDMapWithMoveWithKey (\k q -> subscribeParent k (nt q)) p
1764+ p' <- PatchDMapWithMove.traversePatchDMapWithMoveWithKey subscribeParent p
18081765 -- Collect old parents for deletion and update the keys of moved parents
1809− let moveOrDelete :: forall a. k a -> PatchDMapWithMove.NodeInfo k q a -> MergeSubscribedParentWithMove x k a -> Constant (EventM x (Maybe (EventSubscription x))) a
1766+ let moveOrDelete :: forall a. k a -> PatchDMapWithMove.NodeInfo k (Event x) a -> MergeSubscribedParentWithMove x k a -> Constant (EventM x (Maybe (EventSubscription x))) a
18101767 moveOrDelete _ ni parent = Constant $ case getComposeMaybe $ PatchDMapWithMove._nodeInfo_to ni of
18111768 Nothing -> do
18121769 oldHeight <- liftIO $ getEventSubscribedHeight $ _eventSubscription_subscribed $ _mergeSubscribedParentWithMove_subscription parent
@@ -1817,47 +1774,46 @@ mergeCheapWithMove nt = mergeGCheap' getInitialSubscribers updateMe destroy
18171774 return Nothing
18181775 toDelete <- fmap catMaybes $ mapM (\(_ :=> v) -> getConstant v) $ DMap.toList $ DMap.intersectionWithKey moveOrDelete (unPatchDMapWithMove p) oldParents
18191776 return (toDelete, applyAlways p' oldParents)
1820− getInitialSubscribers :: MergeInitFunc k v q x (MergeSubscribedParentWithMove x k)
1777+ getInitialSubscribers :: MergeInitFunc k x (MergeSubscribedParentWithMove x k)
18211778 getInitialSubscribers initialParents subscriber = do
18221779 subscribers <- forM (DMap.toList initialParents) $ \(k :=> e) -> do
18231780 keyRef <- liftIO $ newIORef k
18241781 let s = subscriber $ liftIO $ readIORef keyRef
1825− (subscription@(EventSubscription _ parentSubd), parentOcc) <- subscribeAndRead (nt e) s
1782+ (subscription@(EventSubscription _ parentSubd), parentOcc) <- subscribeAndRead e s
18261783 height <- liftIO $ getEventSubscribedHeight parentSubd
1827− return (fmap (k :=>) parentOcc, height, k :=> MergeSubscribedParentWithMove subscription keyRef)
1784+ return (fmap (\x -> k :=> Identity x) parentOcc, height, k :=> MergeSubscribedParentWithMove subscription keyRef)
18281785 return ( DMap.fromDistinctAscList $ mapMaybe (\(x, _, _) -> x) subscribers
18291786 , fmap (\(_, h, _) -> h) subscribers --TODO: Assert that there's no invalidHeight in here
18301787 , DMap.fromDistinctAscList $ map (\(_, _, x) -> x) subscribers
18311788 )
1832−
18331789 destroy :: MergeDestroyFunc k (MergeSubscribedParentWithMove x k)
18341790 destroy s = forM_ (DMap.toList s) $ \(_ :=> MergeSubscribedParentWithMove sub _) -> unsubscribe sub
18351791
1836−type MergeUpdateFunc k v x p s
1837− = (forall a. EventM x (k a) -> Subscriber x (v a))
1792+type MergeUpdateFunc k x p s
1793+ = (forall a. EventM x (k a) -> Subscriber x a)
18381794 -> IORef HeightBag
18391795 -> DMap k s
18401796 -> p
18411797 -> EventM x ([EventSubscription x], DMap k s)
18421798
1843−type MergeInitFunc k v q x s
1844− = DMap k q
1845− -> (forall a. EventM x (k a) -> Subscriber x (v a))
1846− -> EventM x (DMap k v, [Height], DMap k s)
1799+type MergeInitFunc k x s
1800+ = DMap k (Event x)
1801+ -> (forall a. EventM x (k a) -> Subscriber x a)
1802+ -> EventM x (DMap k Identity, [Height], DMap k s)
18471803
18481804type MergeDestroyFunc k s
18491805 = DMap k s
18501806 -> IO ()
18511807
1852−data Merge x k v s = Merge
1808+data Merge x k s = Merge
18531809 { _merge_parentsRef :: {-# UNPACK #-} !(IORef (DMap k s))
18541810 , _merge_heightBagRef :: {-# UNPACK #-} !(IORef HeightBag)
18551811 , _merge_heightRef :: {-# UNPACK #-} !(IORef Height)
1856− , _merge_sub :: {-# UNPACK #-} !(Subscriber x (DMap k v))
1857− , _merge_accumRef :: {-# UNPACK #-} !(IORef (DMap k v))
1812+ , _merge_sub :: {-# UNPACK #-} !(Subscriber x (DMap k Identity))
1813+ , _merge_accumRef :: {-# UNPACK #-} !(IORef (DMap k Identity))
18581814 }
18591815
1860−invalidateMergeHeight :: Merge x k v s -> IO ()
1816+invalidateMergeHeight :: Merge x k s -> IO ()
18611817invalidateMergeHeight m = invalidateMergeHeight' (_merge_heightRef m) (_merge_sub m)
18621818
18631819invalidateMergeHeight' :: IORef Height -> Subscriber x a -> IO ()
@@ -1867,7 +1823,8 @@ invalidateMergeHeight' heightRef sub = do
18671823 writeIORef heightRef $! invalidHeight
18681824 subscriberInvalidateHeight sub oldHeight
18691825
1870−revalidateMergeHeight :: Merge x k v s -> IO ()
1826+
1827+revalidateMergeHeight :: Merge x k s -> IO ()
18711828revalidateMergeHeight m = do
18721829 currentHeight <- readIORef $ _merge_heightRef m
18731830 when (currentHeight == invalidHeight) $ do -- revalidateMergeHeight may be called multiple times; perhaps the's a way to finesse it to avoid this check
@@ -1883,19 +1840,19 @@ revalidateMergeHeight m = do
18831840 subscriberRecalculateHeight (_merge_sub m) height
18841841 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
18851842
1886−scheduleMergeSelf :: HasSpiderTimeline x => Merge x k v s -> Height -> EventM x ()
1843+scheduleMergeSelf :: HasSpiderTimeline x => Merge x k s -> Height -> EventM x ()
18871844scheduleMergeSelf m height = scheduleMerge' height (_merge_heightRef m) $ do
18881845 vals <- liftIO $ readIORef $ _merge_accumRef m
18891846 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
18901847 --TODO: Assert that m is not empty
18911848 subscriberPropagate (_merge_sub m) vals
18921849
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)
1850+mergeSubscriber :: forall x k s a. (HasSpiderTimeline x, GCompare k) => Merge x k s -> EventM x (k a) -> Subscriber x a
18941851mergeSubscriber m getKey = Subscriber
18951852 { subscriberPropagate = \a -> do
18961853 oldM <- liftIO $ readIORef $ _merge_accumRef m
18971854 k <- getKey
1898− let newM = DMap.insertWith (error $ "Same key fired multiple times for Merge") k a oldM
1855+ let newM = DMap.insertWith (error $ "Same key fired multiple times for Merge") k (Identity a) oldM
18991856 tracePropagate (Proxy :: Proxy x) $ " DMap.size oldM = " <> show (DMap.size oldM) <> "; DMap.size newM = " <> show (DMap.size newM)
19001857 liftIO $ writeIORef (_merge_accumRef m) $! newM
19011858 when (DMap.null oldM) $ do -- Only schedule the firing once
@@ -1926,7 +1883,7 @@ mergeSubscriber m getKey = Subscriber
19261883 }
19271884
19281885--TODO: Be able to run as much of this as possible promptly
1929−updateMerge :: (HasSpiderTimeline x, GCompare k) => Merge x k v s -> MergeUpdateFunc k v x p s -> p -> SomeMergeUpdate x
1886+updateMerge :: (HasSpiderTimeline x, GCompare k) => Merge x k s -> MergeUpdateFunc k x p s -> p -> SomeMergeUpdate x
19301887updateMerge m updateFunc p = SomeMergeUpdate updateMe (invalidateMergeHeight m) (revalidateMergeHeight m)
19311888 where updateMe = do
19321889 oldParents <- liftIO $ readIORef $ _merge_parentsRef m
@@ -1934,10 +1891,9 @@ updateMerge m updateFunc p = SomeMergeUpdate updateMe (invalidateMergeHeight m)
19341891 liftIO $ writeIORef (_merge_parentsRef m) $! newParents
19351892 return subscriptionsToKill
19361893
1937−{-# INLINE mergeGCheap' #-}
1938−mergeGCheap' :: forall k v x p s q. (HasSpiderTimeline x, GCompare k, PatchTarget p ~ DMap k q)
1939− => MergeInitFunc k v q x s -> MergeUpdateFunc k v x p s -> MergeDestroyFunc k s -> DynamicS x p -> Event x (DMap k v)
1940−mergeGCheap' getInitialSubscribers updateFunc destroy d = Event $ \sub -> do
1894+{-# INLINE mergeCheap' #-}
1895+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)
1896+mergeCheap' getInitialSubscribers updateFunc destroy d = Event $ \sub -> do
19411897 initialParents <- readBehaviorUntracked $ dynamicCurrent d
19421898 accumRef <- liftIO $ newIORef $ error "merge: accumRef not yet initialized"
19431899 heightRef <- liftIO $ newIORef $ error "merge: heightRef not yet initialized"
@@ -1985,11 +1941,11 @@ mergeGCheap' getInitialSubscribers updateFunc destroy d = Event $ \sub -> do
19851941 , occ
19861942 )
19871943
1988−mergeInt :: forall x a. (HasSpiderTimeline x) => DynamicS x (PatchIntMap (Event x a)) -> Event x (IntMap a)
1944+mergeInt :: forall x a. (HasSpiderTimeline x) => Dynamic x (PatchIntMap (Event x a)) -> Event x (IntMap a)
19891945mergeInt = cacheEvent . mergeIntCheap
19901946
19911947{-# INLINABLE mergeIntCheap #-}
1992−mergeIntCheap :: forall x a. (HasSpiderTimeline x) => DynamicS x (PatchIntMap (Event x a)) -> Event x (IntMap a)
1948+mergeIntCheap :: forall x a. (HasSpiderTimeline x) => Dynamic x (PatchIntMap (Event x a)) -> Event x (IntMap a)
19931949mergeIntCheap d = Event $ \sub -> do
19941950 initialParents <- readBehaviorUntracked $ dynamicCurrent d
19951951 accum <- liftIO $ FastMutableIntMap.newEmpty
@@ -2086,16 +2042,14 @@ mergeIntCheap d = Event $ \sub -> do
20862042 )
20872043
20882044newtype 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) }
20902045
2091−fanG :: (HasSpiderTimeline x, GCompare k) => Event x (DMap k v) -> EventSelectorG x k v
2092−fanG e = unsafePerformIO $ do
2093− ref <- newIORef Nothing
2046+fan :: (HasSpiderTimeline x, GCompare k) => Event x (DMap k Identity) -> EventSelector x k
2047+fan e =
20942048 let f = Fan
20952049 { fanParent = e
2096− , fanSubscribed = ref
2050+ , fanSubscribed = unsafeNewIORef e Nothing
20972051 }
2098− pure $ EventSelectorG $ \k -> eventFan k f
2052+ in EventSelector $ \k -> eventFan k f
20992053
21002054runHoldInits :: HasSpiderTimeline x => IORef [SomeHoldInit x] -> IORef [SomeDynInit x] -> IORef [SomeMergeInit x] -> EventM x ()
21012055runHoldInits holdInitRef dynInitRef mergeInitRef = do
@@ -2149,7 +2103,7 @@ clearEventEnv (EventEnv toAssignRef holdInitRef dynInitRef mergeUpdateRef mergeI
21492103-- | Run an event action outside of a frame
21502104runFrame :: forall x a. HasSpiderTimeline x => EventM x a -> SpiderHost x a --TODO: This function also needs to hold the mutex
21512105runFrame a = SpiderHost $ do
2152− let env = _spiderTimeline_eventEnv $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
2106+ let env = _spiderTimeline_eventEnv (spiderTimeline :: SpiderTimelineEnv x)
21532107 let go = do
21542108 result <- a
21552109 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
@@ -2360,7 +2314,7 @@ instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x))
23602314 fail _ = error "Dynamic does not support 'fail'"
23612315
23622316{-# INLINABLE newJoinDyn #-}
2363−newJoinDyn :: HasSpiderTimeline x => DynamicS x (Identity (DynamicS x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
2317+newJoinDyn :: HasSpiderTimeline x => Reflex.Spider.Internal.Dynamic x (Identity (Reflex.Spider.Internal.Dynamic x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
23642318newJoinDyn d =
23652319 let readV0 = readBehaviorTracked . dynamicCurrent =<< readBehaviorTracked (dynamicCurrent d)
23662320 eOuter = Reflex.Spider.Internal.push (fmap (Just . Identity) . readBehaviorUntracked . dynamicCurrent . runIdentity) $ dynamicUpdated d
@@ -2510,7 +2464,7 @@ unsafeNewSpiderTimelineEnv = do
25102464#ifdef DEBUG
25112465 depthRef <- newIORef 0
25122466#endif
2513− return $ STE $ SpiderTimelineEnv
2467+ return $ SpiderTimelineEnv
25142468 { _spiderTimeline_lock = lock
25152469 , _spiderTimeline_eventEnv = env
25162470#ifdef DEBUG
@@ -2522,17 +2476,17 @@ unsafeNewSpiderTimelineEnv = do
25222476newSpiderTimeline :: IO (Some SpiderTimelineEnv)
25232477newSpiderTimeline = withSpiderTimeline (pure . Some)
25242478
2525−data LocalSpiderTimeline (x :: Type) s
2479+data LocalSpiderTimeline x s
25262480
25272481instance Reifies s (SpiderTimelineEnv x) =>
25282482 HasSpiderTimeline (LocalSpiderTimeline x s) where
2529− spiderTimeline = localSpiderTimeline Proxy $ reflect (Proxy :: Proxy s)
2483+ spiderTimeline = localSpiderTimeline (Proxy :: Proxy s) $ reflect (Proxy :: Proxy s)
25302484
25312485localSpiderTimeline
2532− :: proxy s
2486+ :: Proxy s
25332487 -> SpiderTimelineEnv x
25342488 -> SpiderTimelineEnv (LocalSpiderTimeline x s)
2535−localSpiderTimeline _ = coerce
2489+localSpiderTimeline _ = unsafeCoerce
25362490
25372491-- | Pass a new timeline to the given function.
25382492withSpiderTimeline :: (forall x. HasSpiderTimeline x => SpiderTimelineEnv x -> IO r) -> IO r
@@ -2540,18 +2494,18 @@ withSpiderTimeline k = do
25402494 env <- unsafeNewSpiderTimelineEnv
25412495 reify env $ \s -> k $ localSpiderTimeline s env
25422496
2543−newtype SpiderPullM (x :: Type) a = SpiderPullM (BehaviorM x a) deriving (Functor, Applicative, Monad, MonadIO, MonadFix)
2497+newtype SpiderPullM x a = SpiderPullM (BehaviorM x a) deriving (Functor, Applicative, Monad, MonadIO, MonadFix)
25442498
25452499type ComputeM = EventM
25462500
2547−newtype SpiderPushM (x :: Type) a = SpiderPushM (ComputeM x a) deriving (Functor, Applicative, Monad, MonadIO, MonadFix)
2501+newtype SpiderPushM x a = SpiderPushM (ComputeM x a) deriving (Functor, Applicative, Monad, MonadIO, MonadFix)
25482502
25492503instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
25502504 {-# SPECIALIZE instance R.Reflex (SpiderTimeline Global) #-}
25512505 newtype Behavior (SpiderTimeline x) a = SpiderBehavior { unSpiderBehavior :: Behavior x a }
25522506 newtype Event (SpiderTimeline x) a = SpiderEvent { unSpiderEvent :: Event x a }
2553− newtype Dynamic (SpiderTimeline x) a = SpiderDynamic { unSpiderDynamic :: DynamicS x (Identity a) } -- deriving (Functor, Applicative, Monad)
2554− newtype Incremental (SpiderTimeline x) p = SpiderIncremental { unSpiderIncremental :: DynamicS x p }
2507+ newtype Dynamic (SpiderTimeline x) a = SpiderDynamic { unSpiderDynamic :: Dynamic x (Identity a) } -- deriving (Functor, Applicative, Monad)
2508+ newtype Incremental (SpiderTimeline x) p = SpiderIncremental { unSpiderIncremental :: Dynamic x p }
25552509 type PullM (SpiderTimeline x) = SpiderPullM x
25562510 type PushM (SpiderTimeline x) = SpiderPushM x
25572511 {-# INLINABLE never #-}
@@ -2564,15 +2518,10 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
25642518 pushCheap f = SpiderEvent . pushCheap (coerce f) . unSpiderEvent
25652519 {-# INLINABLE pull #-}
25662520 pull = SpiderBehavior . pull . coerce
2567− {-# INLINABLE fanG #-}
2568− fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
2569− {-# INLINABLE mergeG #-}
2570− mergeG
2571− :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2572− => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
2573− -> DMap k q
2574− -> R.Event (SpiderTimeline x) (DMap k v)
2575− mergeG nt = SpiderEvent . mergeG (unSpiderEvent #. nt) . dynamicConst
2521+ {-# INLINABLE merge #-}
2522+ merge = SpiderEvent . merge . dynamicConst . (coerce :: DMap k (R.Event (SpiderTimeline x)) -> DMap k (Event x))
2523+ {-# INLINABLE fan #-}
2524+ fan e = R.EventSelector $ SpiderEvent . select (fan (unSpiderEvent e))
25762525 {-# INLINABLE switch #-}
25772526 switch = SpiderEvent . switch . (coerce :: Behavior x (R.Event (SpiderTimeline x) a) -> Behavior x (Event x a)) . unSpiderBehavior
25782527 {-# INLINABLE coincidence #-}
@@ -2580,15 +2529,15 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
25802529 {-# INLINABLE current #-}
25812530 current = SpiderBehavior . dynamicCurrent . unSpiderDynamic
25822531 {-# INLINABLE updated #-}
2583− updated = SpiderEvent #. dynamicUpdated .# fmap coerce . unSpiderDynamic
2532+ updated = coerce $ SpiderEvent . dynamicUpdated . unSpiderDynamic
25842533 {-# INLINABLE unsafeBuildDynamic #-}
25852534 unsafeBuildDynamic readV0 v' = SpiderDynamic $ dynamicDynIdentity $ unsafeBuildDynamic (coerce readV0) $ coerce $ unSpiderEvent v'
25862535 {-# INLINABLE unsafeBuildIncremental #-}
25872536 unsafeBuildIncremental readV0 dv = SpiderIncremental $ dynamicDyn $ unsafeBuildDynamic (coerce readV0) $ unSpiderEvent dv
2588− {-# INLINABLE mergeIncrementalG #-}
2589− mergeIncrementalG nt = SpiderEvent #. mergeG (coerce #. nt) .# unSpiderIncremental
2590− {-# INLINABLE mergeIncrementalWithMoveG #-}
2591− mergeIncrementalWithMoveG nt = SpiderEvent #. mergeWithMove (coerce #. nt) .# unSpiderIncremental
2537+ {-# INLINABLE mergeIncremental #-}
2538+ mergeIncremental = SpiderEvent . merge . (unsafeCoerce :: Dynamic x (PatchDMap k (R.Event (SpiderTimeline x))) -> Dynamic x (PatchDMap k (Event x))) . unSpiderIncremental
2539+ {-# INLINABLE mergeIncrementalWithMove #-}
2540+ mergeIncrementalWithMove = SpiderEvent . mergeWithMove . (unsafeCoerce :: Dynamic x (PatchDMapWithMove k (R.Event (SpiderTimeline x))) -> Dynamic x (PatchDMapWithMove k (Event x))) . unSpiderIncremental
25922541 {-# INLINABLE currentIncremental #-}
25932542 currentIncremental = SpiderBehavior . dynamicCurrent . unSpiderIncremental
25942543 {-# INLINABLE updatedIncremental #-}
@@ -2599,10 +2548,9 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
25992548 return $ Identity <$> apply p c --TODO: Avoid the redundant 'apply'
26002549 eventCoercion Coercion = Coercion
26012550 behaviorCoercion Coercion = Coercion
2602− dynamicCoercion Coercion = Coercion
2603− incrementalCoercion Coercion Coercion = Coercion
2551+ dynamicCoercion = unsafeCoerce --TODO: How can we avoid this unsafeCoerce? This is safe only because we know how Identity works as a Patch instance
26042552 {-# INLINABLE mergeIntIncremental #-}
2605− mergeIntIncremental = SpiderEvent . mergeInt . coerce
2553+ mergeIntIncremental = SpiderEvent . mergeInt . (unsafeCoerce :: Dynamic x (PatchIntMap (R.Event (SpiderTimeline x) a)) -> Dynamic x (PatchIntMap (Event x a))) . unSpiderIncremental
26062554 {-# INLINABLE fanInt #-}
26072555 fanInt e = R.EventSelectorInt $ SpiderEvent . selectInt (fanInt (unSpiderEvent e))
26082556
@@ -2627,7 +2575,7 @@ instance MonadAtomicRef (EventM x) where
26272575 atomicModifyRef r f = liftIO $ atomicModifyRef r f
26282576
26292577-- | The monad for actions that manipulate a Spider timeline identified by @x@
2630−newtype SpiderHost (x :: Type) a = SpiderHost { unSpiderHost :: IO a } deriving (Functor, Applicative, MonadFix, MonadIO, MonadException, MonadAsyncException)
2578+newtype SpiderHost x a = SpiderHost { unSpiderHost :: IO a } deriving (Functor, Applicative, MonadFix, MonadIO, MonadException, MonadAsyncException)
26312579
26322580instance Monad (SpiderHost x) where
26332581 {-# INLINABLE (>>=) #-}
@@ -2649,7 +2597,7 @@ runSpiderHost (SpiderHost a) = a
26492597runSpiderHostForTimeline :: SpiderHost x a -> SpiderTimelineEnv x -> IO a
26502598runSpiderHostForTimeline (SpiderHost a) _ = a
26512599
2652−newtype SpiderHostFrame (x :: Type) a = SpiderHostFrame { runSpiderHostFrame :: EventM x a }
2600+newtype SpiderHostFrame x a = SpiderHostFrame { runSpiderHostFrame :: EventM x a }
26532601 deriving (Functor, Applicative, MonadFix, MonadIO, MonadException, MonadAsyncException)
26542602
26552603instance Monad (SpiderHostFrame x) where