@@ -17,10 +17,7 @@
17{-# LANGUAGE TypeOperators #-}
17{-# LANGUAGE TypeOperators #-}
18{-# LANGUAGE UndecidableInstances #-}
18{-# LANGUAGE UndecidableInstances #-}
19{-# LANGUAGE PolyKinds #-}
19{-# LANGUAGE PolyKinds #-}
20−{-# LANGUAGE DeriveFunctor #-}
21−{-# LANGUAGE StandaloneDeriving #-}
22{-# LANGUAGE InstanceSigs #-}
20{-# LANGUAGE InstanceSigs #-}
24#ifdef USE_REFLEX_OPTIMIZER
21#ifdef USE_REFLEX_OPTIMIZER
25{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
22{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
@@ -38,10 +35,7 @@ import Control.Monad hiding (forM, forM_, mapM, mapM_)
38import Control.Monad.Exception
35import Control.Monad.Exception
39import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
36import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
40import Control.Monad.Primitive
37import Control.Monad.Primitive
41−--import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_)
38+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
45import Control.Monad.Ref
39import Control.Monad.Ref
46import Data.Align
40import Data.Align
47import Data.Coerce
41import Data.Coerce
@@ -211,6 +205,12 @@ nextNodeIdRef = unsafePerformIO $ newIORef 1
212newNodeId :: IO Int
206newNodeId :: IO Int
213newNodeId = atomicModifyIORef' nextNodeIdRef $ \n -> (succ n, n)
207newNodeId = atomicModifyIORef' nextNodeIdRef $ \n -> (succ n, n)
209+{-# NOINLINE unsafeNodeId #-}
210+unsafeNodeId :: a -> Int
211+unsafeNodeId a = unsafePerformIO $ do
216--------------------------------------------------------------------------------
216--------------------------------------------------------------------------------
@@ -314,10 +314,9 @@ cacheEvent e =
317− unsafePerformIO $ do
318− mSubscribedRef :: IORef (FastWeak (CacheSubscribed x a))
317+ let mSubscribedRef :: IORef (FastWeak (CacheSubscribed x a))
319− <- newIORef emptyFastWeak
318+ !mSubscribedRef = unsafeNewIORef e emptyFastWeak
320− pure $ \sub -> {-# SCC "cacheEvent" #-} do
319+ in \sub -> {-# SCC "cacheEvent" #-} do
321#ifdef DEBUG_TRACE_EVENTS
320#ifdef DEBUG_TRACE_EVENTS
322 unless (BS8.null callSite) $ liftIO $ BS8.hPutStrLn stderr callSite
321 unless (BS8.null callSite) $ liftIO $ BS8.hPutStrLn stderr callSite
@@ -383,7 +382,7 @@ eventRoot !k !r = Event $ wrap eventSubscribedRoot $ liftIO . getRootSubscribed
383eventNever :: Event x a
382eventNever :: Event x a
384eventNever = Event $ \_ -> return (EventSubscription (return ()) eventSubscribedNever, Nothing)
383eventNever = Event $ \_ -> return (EventSubscription (return ()) eventSubscribedNever, Nothing)
386−eventFan :: (GCompare k, HasSpiderTimeline x) => k a -> Fan x k v -> Event x (v a)
385+eventFan :: (GCompare k, HasSpiderTimeline x) => k a -> Fan x k -> Event x a
387eventFan !k !f = Event $ wrap eventSubscribedFan $ getFanSubscribed k f
386eventFan !k !f = Event $ wrap eventSubscribedFan $ getFanSubscribed k f
389eventSwitch :: HasSpiderTimeline x => Switch x a -> Event x a
388eventSwitch :: HasSpiderTimeline x => Switch x a -> Event x a
@@ -435,14 +434,14 @@ newSubscriberHold h = return $ Subscriber
435 , subscriberRecalculateHeight = \_ -> return ()
434 , subscriberRecalculateHeight = \_ -> return ()
438−newSubscriberFan :: forall x k v. (HasSpiderTimeline x, GCompare k) => FanSubscribed x k v -> IO (Subscriber x (DMap k v))
437+newSubscriberFan :: forall x k. (HasSpiderTimeline x, GCompare k) => FanSubscribed x k -> IO (Subscriber x (DMap k Identity))
439newSubscriberFan subscribed = return $ Subscriber
438newSubscriberFan subscribed = return $ Subscriber
440 { subscriberPropagate = \a -> {-# SCC "traverseFan" #-} do
439 { subscriberPropagate = \a -> {-# SCC "traverseFan" #-} do
441 subs <- liftIO $ readIORef $ fanSubscribedSubscribers subscribed
440 subs <- liftIO $ readIORef $ fanSubscribedSubscribers subscribed
442 tracePropagate (Proxy :: Proxy x) $ "SubscriberFan" <> showNodeId subscribed <> ": " ++ show (DMap.size subs) ++ " keys subscribed, " ++ show (DMap.size a) ++ " keys firing"
441 tracePropagate (Proxy :: Proxy x) $ "SubscriberFan" <> showNodeId subscribed <> ": " ++ show (DMap.size subs) ++ " keys subscribed, " ++ show (DMap.size a) ++ " keys firing"
443 liftIO $ writeIORef (fanSubscribedOccurrence subscribed) $ Just a
442 liftIO $ writeIORef (fanSubscribedOccurrence subscribed) $ Just a
444 scheduleClear $ fanSubscribedOccurrence subscribed
443 scheduleClear $ fanSubscribedOccurrence subscribed
445− let f _ (Pair v subsubs) = do
444+ let f _ (Pair (Identity v) subsubs) = do
446 propagate v $ _fanSubscribedChildren_list subsubs
445 propagate v $ _fanSubscribedChildren_list subsubs
447 return $ Constant ()
446 return $ Constant ()
448 _ <- DMap.traverseWithKey f $ DMap.intersectionWithKey (\_ -> Pair) a subs --TODO: Would be nice to have DMap.traverse_
447 _ <- DMap.traverseWithKey f $ DMap.intersectionWithKey (\_ -> Pair) a subs --TODO: Would be nice to have DMap.traverse_
@@ -558,11 +557,6 @@ propagateFast a subscribers = withIncreasedDepth $ do
558toAny :: a -> Any
557toAny :: a -> Any
559toAny = unsafeCoerce
558toAny = 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
566data EventSubscribed x = EventSubscribed
560data EventSubscribed x = EventSubscribed
567 { eventSubscribedHeightRef :: {-# UNPACK #-} !(IORef Height)
561 { eventSubscribedHeightRef :: {-# UNPACK #-} !(IORef Height)
568 , eventSubscribedRetained :: {-# NOUNPACK #-} !Any
562 , eventSubscribedRetained :: {-# NOUNPACK #-} !Any
@@ -595,7 +589,7 @@ eventSubscribedNever = EventSubscribed
598−eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
592+eventSubscribedFan :: FanSubscribed x k -> EventSubscribed x
599eventSubscribedFan !subscribed = EventSubscribed
593eventSubscribedFan !subscribed = EventSubscribed
600 { eventSubscribedHeightRef = eventSubscribedHeightRef $ _eventSubscription_subscribed $ fanSubscribedParent subscribed
594 { eventSubscribedHeightRef = eventSubscribedHeightRef $ _eventSubscription_subscribed $ fanSubscribedParent subscribed
601 , eventSubscribedRetained = toAny subscribed
595 , eventSubscribedRetained = toAny subscribed
@@ -686,7 +680,7 @@ behaviorPull !p = Behavior $ do
686 wi <- liftIO $ mkWeakPtrWithDebug i "InvalidatorPull"
680 wi <- liftIO $ mkWeakPtrWithDebug i "InvalidatorPull"
687 parentsRef <- liftIO $ newIORef []
681 parentsRef <- liftIO $ newIORef []
688 holdInits <- askBehaviorHoldInits
682 holdInits <- askBehaviorHoldInits
689− a <- liftIO $ runReaderIO (unBehaviorM $ pullCompute p) (Just (wi, parentsRef), holdInits)
683+ a <- liftIO $ runReaderT (unBehaviorM $ pullCompute p) (Just (wi, parentsRef), holdInits)
690 invsRef <- liftIO . newIORef . maybeToList =<< askInvalidator
684 invsRef <- liftIO . newIORef . maybeToList =<< askInvalidator
691 parents <- liftIO $ readIORef parentsRef
685 parents <- liftIO $ readIORef parentsRef
692 let subscribed = PullSubscribed
686 let subscribed = PullSubscribed
@@ -721,46 +715,42 @@ readBehaviorUntracked b = do
721-- Dynamic
715-- Dynamic
722--------------------------------------------------------------------------------
716--------------------------------------------------------------------------------
724−type DynamicS x p = Dynamic x (PatchTarget p) p
726−data Dynamic x target p = Dynamic
718+data Dynamic x p = Dynamic
727− { dynamicCurrent :: !(Behavior x target)
719+ { dynamicCurrent :: !(Behavior x (PatchTarget p))
728 , dynamicUpdated :: Event x p -- This must be lazy; see the comment on holdEvent --TODO: Would this let us eliminate `Dyn`?
720 , 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)
736−dynamicHold :: Hold x p -> DynamicS x p
723+dynamicHold :: Hold x p -> Dynamic x p
737dynamicHold !h = Dynamic
724dynamicHold !h = Dynamic
738 { dynamicCurrent = behaviorHold h
725 { dynamicCurrent = behaviorHold h
739 , dynamicUpdated = eventHold h
726 , dynamicUpdated = eventHold h
742−dynamicHoldIdentity :: Hold x (Identity a) -> DynamicS x (Identity a)
729+dynamicHoldIdentity :: Hold x (Identity a) -> Dynamic x (Identity a)
743dynamicHoldIdentity = dynamicHold
730dynamicHoldIdentity = dynamicHold
745−dynamicConst :: PatchTarget p -> DynamicS x p
732+dynamicConst :: PatchTarget p -> Dynamic x p
746dynamicConst !a = Dynamic
733dynamicConst !a = Dynamic
747 { dynamicCurrent = behaviorConst a
734 { dynamicCurrent = behaviorConst a
748 , dynamicUpdated = eventNever
735 , dynamicUpdated = eventNever
751−dynamicDyn :: (HasSpiderTimeline x, Patch p) => Dyn x p -> DynamicS x p
738+dynamicDyn :: (HasSpiderTimeline x, Patch p) => Dyn x p -> Dynamic x p
752dynamicDyn !d = Dynamic
739dynamicDyn !d = Dynamic
753 { dynamicCurrent = behaviorDyn d
740 { dynamicCurrent = behaviorDyn d
754 , dynamicUpdated = eventDyn d
741 , dynamicUpdated = eventDyn d
757−dynamicDynIdentity :: HasSpiderTimeline x => Dyn x (Identity a) -> DynamicS x (Identity a)
744+dynamicDynIdentity :: HasSpiderTimeline x => Dyn x (Identity a) -> Dynamic x (Identity a)
758dynamicDynIdentity = dynamicDyn
745dynamicDynIdentity = dynamicDyn
760--------------------------------------------------------------------------------
747--------------------------------------------------------------------------------
761-- Combinators
748-- Combinators
762--------------------------------------------------------------------------------
749--------------------------------------------------------------------------------
751+--TODO: Figure out why certain things are not 'representational', then make them
752+--representational so we can use coerce
764--type role Hold representational
754--type role Hold representational
765data Hold x p
755data Hold x p
766 = Hold { holdValue :: !(IORef (PatchTarget p))
756 = Hold { holdValue :: !(IORef (PatchTarget p))
@@ -781,25 +771,20 @@ globalSpiderTimelineEnv = unsafePerformIO unsafeNewSpiderTimelineEnv
782-- | Stores all global data relevant to a particular Spider timeline; only one
772-- | Stores all global data relevant to a particular Spider timeline; only one
783-- value should exist for each type @x@
773-- 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
789−data SpiderTimelineEnv' x = SpiderTimelineEnv
774+data SpiderTimelineEnv x = SpiderTimelineEnv
790 { _spiderTimeline_lock :: {-# UNPACK #-} !(MVar ())
775 { _spiderTimeline_lock :: {-# UNPACK #-} !(MVar ())
791 , _spiderTimeline_eventEnv :: {-# UNPACK #-} !(EventEnv x)
776 , _spiderTimeline_eventEnv :: {-# UNPACK #-} !(EventEnv x)
792#ifdef DEBUG
777#ifdef DEBUG
793 , _spiderTimeline_depth :: {-# UNPACK #-} !(IORef Int)
778 , _spiderTimeline_depth :: {-# UNPACK #-} !(IORef Int)
796−type role SpiderTimelineEnv' phantom
781+type role SpiderTimelineEnv nominal
798instance Eq (SpiderTimelineEnv x) where
783instance Eq (SpiderTimelineEnv x) where
799 _ == _ = True -- Since only one exists of each type
784 _ == _ = True -- Since only one exists of each type
801instance GEq SpiderTimelineEnv where
786instance GEq SpiderTimelineEnv where
802− a `geq` b = if _spiderTimeline_lock (unSTE a) == _spiderTimeline_lock (unSTE b)
787+ a `geq` b = if _spiderTimeline_lock a == _spiderTimeline_lock b
803 then Just $ unsafeCoerce Refl -- This unsafeCoerce is safe because the same SpiderTimelineEnv can't have two different 'x' arguments
788 then Just $ unsafeCoerce Refl -- This unsafeCoerce is safe because the same SpiderTimelineEnv can't have two different 'x' arguments
804 else Nothing
789 else Nothing
@@ -822,7 +807,7 @@ runEventM :: EventM x a -> IO a
822runEventM = unEventM
807runEventM = unEventM
824asksEventEnv :: forall x a. HasSpiderTimeline x => (EventEnv x -> a) -> EventM x a
809asksEventEnv :: forall x a. HasSpiderTimeline x => (EventEnv x -> a) -> EventM x a
825−asksEventEnv f = return $ f $ _spiderTimeline_eventEnv (unSTE (spiderTimeline :: SpiderTimelineEnv x))
810+asksEventEnv f = return $ f $ _spiderTimeline_eventEnv (spiderTimeline :: SpiderTimelineEnv x)
827class MonadIO m => Defer a m where
812class MonadIO m => Defer a m where
828 getDeferralQueue :: m (IORef [a])
813 getDeferralQueue :: m (IORef [a])
@@ -960,9 +945,9 @@ getHoldEventSubscription h = do
961type BehaviorEnv x = (Maybe (Weak (Invalidator x), IORef [SomeBehaviorSubscribed x]), IORef [SomeHoldInit x])
946type BehaviorEnv x = (Maybe (Weak (Invalidator x), IORef [SomeBehaviorSubscribed x]), IORef [SomeHoldInit x])
948+--type role BehaviorM representational
963-- BehaviorM can sample behaviors
949-- BehaviorM can sample behaviors
964−newtype BehaviorM x a = BehaviorM { unBehaviorM :: ReaderIO (BehaviorEnv x) a }
950+newtype BehaviorM x a = BehaviorM { unBehaviorM :: ReaderT (BehaviorEnv x) IO a } deriving (Functor, Applicative, MonadIO, MonadFix)
965− deriving (Functor, Applicative, MonadIO, MonadFix, MonadReader (BehaviorEnv x))
967instance Monad (BehaviorM x) where
952instance Monad (BehaviorM x) where
968 {-# INLINE (>>=) #-}
953 {-# INLINE (>>=) #-}
@@ -1013,7 +998,7 @@ data RootSubscribed x a = forall k. GCompare k => RootSubscribed
1016−data Root x k
1001+data Root x (k :: * -> *)
1017 = Root { rootOccurrence :: !(IORef (DMap k Identity)) -- The currently-firing occurrence of this event
1002 = Root { rootOccurrence :: !(IORef (DMap k Identity)) -- The currently-firing occurrence of this event
1018 , rootSubscribed :: !(IORef (DMap k (RootSubscribed x)))
1003 , rootSubscribed :: !(IORef (DMap k (RootSubscribed x)))
1019 , rootInit :: !(forall a. k a -> RootTrigger x a -> IO (IO ()))
1004 , rootInit :: !(forall a. k a -> RootTrigger x a -> IO (IO ()))
@@ -1083,25 +1068,25 @@ heightBagVerify b@(HeightBag s c) = if
1083heightBagVerify = id
1068heightBagVerify = id
1086−data FanSubscribedChildren x k v a = FanSubscribedChildren
1071+data FanSubscribedChildren (x :: *) k a = FanSubscribedChildren
1087− { _fanSubscribedChildren_list :: !(WeakBag (Subscriber x (v a)))
1072+ { _fanSubscribedChildren_list :: !(WeakBag (Subscriber x a))
1088− , _fanSubscribedChildren_self :: {-# NOUNPACK #-} !(k a, FanSubscribed x k v)
1073+ , _fanSubscribedChildren_self :: {-# NOUNPACK #-} !(k a, FanSubscribed x k)
1089− , _fanSubscribedChildren_weakSelf :: !(IORef (Weak (k a, FanSubscribed x k v)))
1074+ , _fanSubscribedChildren_weakSelf :: !(IORef (Weak (k a, FanSubscribed x k)))
1092−data FanSubscribed x k v
1077+data FanSubscribed (x :: *) k
1093− = FanSubscribed { fanSubscribedCachedSubscribed :: !(IORef (Maybe (FanSubscribed x k v)))
1078+ = FanSubscribed { fanSubscribedCachedSubscribed :: !(IORef (Maybe (FanSubscribed x k)))
1094− , fanSubscribedOccurrence :: !(IORef (Maybe (DMap k v)))
1079+ , fanSubscribedOccurrence :: !(IORef (Maybe (DMap k Identity)))
1095− , fanSubscribedSubscribers :: !(IORef (DMap k (FanSubscribedChildren x k v))) -- This DMap should never be empty
1080+ , fanSubscribedSubscribers :: !(IORef (DMap k (FanSubscribedChildren x k))) -- This DMap should never be empty
1096 , fanSubscribedParent :: !(EventSubscription x)
1081 , fanSubscribedParent :: !(EventSubscription x)
1097#ifdef DEBUG_NODEIDS
1082#ifdef DEBUG_NODEIDS
1098 , fanSubscribedNodeId :: Int
1083 , fanSubscribedNodeId :: Int
1102−data Fan x k v
1087+data Fan x k
1103− = Fan { fanParent :: !(Event x (DMap k v))
1088+ = Fan { fanParent :: !(Event x (DMap k Identity))
1104− , fanSubscribed :: !(IORef (Maybe (FanSubscribed x k v)))
1089+ , fanSubscribed :: !(IORef (Maybe (FanSubscribed x k)))
1107data SwitchSubscribed x a
1092data SwitchSubscribed x a
@@ -1179,11 +1164,11 @@ data DynType x p = UnsafeDyn !(BehaviorM x (PatchTarget p), Event x p)
1180newtype Dyn (x :: Type) p = Dyn { unDyn :: IORef (DynType x p) }
1165newtype Dyn (x :: Type) p = Dyn { unDyn :: IORef (DynType x p) }
1182−newMapDyn :: HasSpiderTimeline x => (a -> b) -> DynamicS x (Identity a) -> DynamicS x (Identity b)
1167+newMapDyn :: HasSpiderTimeline x => (a -> b) -> Dynamic x (Identity a) -> Dynamic x (Identity b)
1183newMapDyn f d = dynamicDynIdentity $ unsafeBuildDynamic (fmap f $ readBehaviorTracked $ dynamicCurrent d) (Identity . f . runIdentity <$> dynamicUpdated d)
1168newMapDyn f d = dynamicDynIdentity $ unsafeBuildDynamic (fmap f $ readBehaviorTracked $ dynamicCurrent d) (Identity . f . runIdentity <$> dynamicUpdated d)
1185--TODO: Avoid the duplication between this and R.zipDynWith
1170--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)
1171+zipDynWith :: HasSpiderTimeline x => (a -> b -> c) -> Dynamic x (Identity a) -> Dynamic x (Identity b) -> Dynamic x (Identity c)
1187zipDynWith f da db =
1172zipDynWith f da db =
1188 let eab = align (dynamicUpdated da) (dynamicUpdated db)
1173 let eab = align (dynamicUpdated da) (dynamicUpdated db)
1189 ec = flip push eab $ \o -> do
1174 ec = flip push eab $ \o -> do
@@ -1206,12 +1191,18 @@ buildDynamic readV0 v' = do
1206 return d
1191 return d
1208unsafeBuildDynamic :: BehaviorM x (PatchTarget p) -> Event x p -> Dyn x p
1193unsafeBuildDynamic :: BehaviorM x (PatchTarget p) -> Event x p -> Dyn x p
1209−unsafeBuildDynamic readV0 v' =
1194+unsafeBuildDynamic readV0 v' = Dyn $ unsafeNewIORef x $ UnsafeDyn x
1210− Dyn $ unsafePerformIO $ newIORef $ UnsafeDyn (readV0, v')
1195+ where x = (readV0, v')
1212-- ResultM can read behaviors and events
1197-- ResultM can read behaviors and events
1213type ResultM = EventM
1198type ResultM = EventM
1200+{-# NOINLINE unsafeNewIORef #-}
1201+unsafeNewIORef :: a -> b -> IORef b
1202+unsafeNewIORef a b = unsafePerformIO $ do
1215instance HasSpiderTimeline x => Functor (Event x) where
1206instance HasSpiderTimeline x => Functor (Event x) where
1216 fmap f = push $ return . Just . f
1207 fmap f = push $ return . Just . f
@@ -1224,42 +1215,33 @@ push f e = cacheEvent (pushCheap f e)
1225{-# INLINABLE pull #-}
1216{-# INLINABLE pull #-}
1226pull :: BehaviorM x a -> Behavior x a
1217pull :: BehaviorM x a -> Behavior x a
1227−pull a = unsafePerformIO $ do
1218+pull a = behaviorPull $ Pull
1228− ref <- newIORef Nothing
1219+ { pullCompute = a
1220+ , pullValue = unsafeNewIORef a Nothing
1229#ifdef DEBUG_NODEIDS
1221#ifdef DEBUG_NODEIDS
1230− nid <- newNodeId
1222+ , pullNodeId = unsafeNodeId a
1232− pure $ behaviorPull $ Pull
1235−#ifdef DEBUG_NODEIDS
1240{-# INLINABLE switch #-}
1226{-# INLINABLE switch #-}
1241switch :: HasSpiderTimeline x => Behavior x (Event x a) -> Event x a
1227switch :: HasSpiderTimeline x => Behavior x (Event x a) -> Event x a
1242−switch a = unsafePerformIO $ do
1243− ref <- newIORef Nothing
1244− pure $ eventSwitch $ Switch
1228+switch a = eventSwitch $ Switch
1245− { switchParent = a
1229+ { switchParent = a
1246− , switchSubscribed = ref
1230+ , switchSubscribed = unsafeNewIORef a Nothing
1249coincidence :: HasSpiderTimeline x => Event x (Event x a) -> Event x a
1233coincidence :: HasSpiderTimeline x => Event x (Event x a) -> Event x a
1250−coincidence a = unsafePerformIO $ do
1251− ref <- newIORef Nothing
1252− pure $ eventCoincidence $ Coincidence
1234+coincidence a = eventCoincidence $ Coincidence
1253− { coincidenceParent = a
1235+ { coincidenceParent = a
1254− , coincidenceSubscribed = ref
1236+ , coincidenceSubscribed = unsafeNewIORef a Nothing
1257-- Propagate the given event occurrence; before cleaning up, run the given action, which may read the state of events and behaviors
1239-- Propagate the given event occurrence; before cleaning up, run the given action, which may read the state of events and behaviors
1258run :: forall x b. HasSpiderTimeline x => [DSum (RootTrigger x) Identity] -> ResultM x b -> SpiderHost x b
1240run :: forall x b. HasSpiderTimeline x => [DSum (RootTrigger x) Identity] -> ResultM x b -> SpiderHost x b
1259run roots after = do
1241run roots after = do
1260 tracePropagate (Proxy :: Proxy x) $ "Running an event frame with " <> show (length roots) <> " events"
1242 tracePropagate (Proxy :: Proxy x) $ "Running an event frame with " <> show (length roots) <> " events"
1261 let t = spiderTimeline :: SpiderTimelineEnv x
1243 let t = spiderTimeline :: SpiderTimelineEnv x
1262− result <- SpiderHost $ withMVar (_spiderTimeline_lock (unSTE t)) $ \_ -> unSpiderHost $ runFrame $ do
1244+ result <- SpiderHost $ withMVar (_spiderTimeline_lock t) $ \_ -> unSpiderHost $ runFrame $ do
1263 rootsToPropagate <- forM roots $ \r@(RootTrigger (_, occRef, k) :=> a) -> do
1245 rootsToPropagate <- forM roots $ \r@(RootTrigger (_, occRef, k) :=> a) -> do
1264 occBefore <- liftIO $ do
1246 occBefore <- liftIO $ do
1265 occBefore <- readIORef occRef
1247 occBefore <- readIORef occRef
@@ -1323,9 +1305,9 @@ type WeakList a = [Weak a]
1323withIncreasedDepth :: CanTrace x m => m a -> m a
1305withIncreasedDepth :: CanTrace x m => m a -> m a
1324withIncreasedDepth a = do
1306withIncreasedDepth a = do
1325 spiderTimeline <- askSpiderTimelineEnv
1307 spiderTimeline <- askSpiderTimelineEnv
1326− liftIO $ modifyIORef' (_spiderTimeline_depth (unSTE spiderTimeline)) succ
1308+ liftIO $ modifyIORef' (_spiderTimeline_depth spiderTimeline) succ
1327 result <- a
1309 result <- a
1328− liftIO $ modifyIORef' (_spiderTimeline_depth (unSTE spiderTimeline)) pred
1310+ liftIO $ modifyIORef' (_spiderTimeline_depth spiderTimeline) pred
1329 return result
1311 return result
1331withIncreasedDepth :: m a -> m a
1313withIncreasedDepth :: m a -> m a
@@ -1352,7 +1334,7 @@ traceMWhen _ b getMessage = when b $ do
1352 message <- getMessage
1334 message <- getMessage
1353#ifdef DEBUG
1335#ifdef DEBUG
1354 spiderTimeline <- askSpiderTimelineEnv
1336 spiderTimeline <- askSpiderTimelineEnv
1355− d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE spiderTimeline
1337+ d <- liftIO $ readIORef $ _spiderTimeline_depth spiderTimeline
1357 let d = 0
1339 let d = 0
@@ -1393,25 +1375,25 @@ propagateSubscriberHold h a = do
1393data SomeResetCoincidence x = forall a. SomeResetCoincidence !(EventSubscription x) !(Maybe (CoincidenceSubscribed x a)) -- The CoincidenceSubscriber will be present only if heights need to be reset
1375data SomeResetCoincidence x = forall a. SomeResetCoincidence !(EventSubscription x) !(Maybe (CoincidenceSubscribed x a)) -- The CoincidenceSubscriber will be present only if heights need to be reset
1395runBehaviorM :: BehaviorM x a -> Maybe (Weak (Invalidator x), IORef [SomeBehaviorSubscribed x]) -> IORef [SomeHoldInit x] -> IO a
1377runBehaviorM :: 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)
1378+runBehaviorM a mwi holdInits = runReaderT (unBehaviorM a) (mwi, holdInits)
1398askInvalidator :: BehaviorM x (Maybe (Weak (Invalidator x)))
1380askInvalidator :: BehaviorM x (Maybe (Weak (Invalidator x)))
1399askInvalidator = do
1381askInvalidator = do
1400− (!m, _) <- ask
1382+ (!m, _) <- BehaviorM ask
1401 case m of
1383 case m of
1402 Nothing -> return Nothing
1384 Nothing -> return Nothing
1403 Just (!wi, _) -> return $ Just wi
1385 Just (!wi, _) -> return $ Just wi
1405askParentsRef :: BehaviorM x (Maybe (IORef [SomeBehaviorSubscribed x]))
1387askParentsRef :: BehaviorM x (Maybe (IORef [SomeBehaviorSubscribed x]))
1406askParentsRef = do
1388askParentsRef = do
1407− (!m, _) <- ask
1389+ (!m, _) <- BehaviorM ask
1408 case m of
1390 case m of
1409 Nothing -> return Nothing
1391 Nothing -> return Nothing
1410 Just (_, !p) -> return $ Just p
1392 Just (_, !p) -> return $ Just p
1412askBehaviorHoldInits :: BehaviorM x (IORef [SomeHoldInit x])
1394askBehaviorHoldInits :: BehaviorM x (IORef [SomeHoldInit x])
1413askBehaviorHoldInits = do
1395askBehaviorHoldInits = do
1414− (_, !his) <- ask
1396+ (_, !his) <- BehaviorM ask
1415 return his
1397 return his
1417{-# INLINE getDynHold #-}
1399{-# INLINE getDynHold #-}
@@ -1456,9 +1438,6 @@ getRootSubscribed k r sub = do
1456 when debugPropagate $ putStrLn $ "getRootSubscribed: calling rootInit"
1438 when debugPropagate $ putStrLn $ "getRootSubscribed: calling rootInit"
1457 uninit <- rootInit r k $ RootTrigger (subs, rootOccurrence r, k)
1439 uninit <- rootInit r k $ RootTrigger (subs, rootOccurrence r, k)
1458 writeIORef uninitRef $! uninit
1440 writeIORef uninitRef $! uninit
1459−#ifdef DEBUG_NODEIDS
1462 let !subscribed = RootSubscribed
1441 let !subscribed = RootSubscribed
1463 { rootSubscribedKey = k
1442 { rootSubscribedKey = k
1464 , rootSubscribedCachedSubscribed = cached
1443 , rootSubscribedCachedSubscribed = cached
@@ -1467,7 +1446,7 @@ getRootSubscribed k r sub = do
1467 , rootSubscribedUninit = uninit
1446 , rootSubscribedUninit = uninit
1468 , rootSubscribedWeakSelf = weakSelf
1447 , rootSubscribedWeakSelf = weakSelf
1469#ifdef DEBUG_NODEIDS
1448#ifdef DEBUG_NODEIDS
1470− , rootSubscribedNodeId = nid
1449+ , rootSubscribedNodeId = unsafeNodeId (k, r, subs)
1473 -- If we die at the same moment that all our children die, they will
1452 -- If we die at the same moment that all our children die, they will
@@ -1516,10 +1495,16 @@ newFanInt = do
1516 , _fanInt_occRef = occRef
1495 , _fanInt_occRef = occRef
1498+{-# NOINLINE unsafeNewFanInt #-}
1499+unsafeNewFanInt :: b -> FanInt x a
1500+unsafeNewFanInt b = unsafePerformIO $ do
1519fanInt :: HasSpiderTimeline x => Event x (IntMap a) -> EventSelectorInt x a
1504fanInt :: HasSpiderTimeline x => Event x (IntMap a) -> EventSelectorInt x a
1520−fanInt p = unsafePerformIO $ do
1505+fanInt p =
1521− self <- newFanInt
1506+ let self = unsafeNewFanInt p
1522− pure $ EventSelectorInt $ \k -> Event $ \sub -> do
1507+ in EventSelectorInt $ \k -> Event $ \sub -> do
1523 isEmpty <- liftIO $ FastMutableIntMap.isEmpty (_fanInt_subscribers self)
1508 isEmpty <- liftIO $ FastMutableIntMap.isEmpty (_fanInt_subscribers self)
1524 when isEmpty $ do -- This is the first subscriber, so we need to subscribe to our input
1509 when isEmpty $ do -- This is the first subscriber, so we need to subscribe to our input
1525 (subscription, parentOcc) <- subscribeAndRead p $ Subscriber
1510 (subscription, parentOcc) <- subscribeAndRead p $ Subscriber
@@ -1555,7 +1540,7 @@ fanInt p = unsafePerformIO $ do
1555 return (EventSubscription (FastWeakBag.remove t) $! EventSubscribed heightRef $! toAny (_fanInt_subscriptionRef self, t), IntMap.lookup k currentOcc)
1540 return (EventSubscription (FastWeakBag.remove t) $! EventSubscribed heightRef $! toAny (_fanInt_subscriptionRef self, t), IntMap.lookup k currentOcc)
1557{-# INLINABLE getFanSubscribed #-}
1542{-# 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))
1543+getFanSubscribed :: (HasSpiderTimeline x, GCompare k) => k a -> Fan x k -> Subscriber x a -> EventM x (WeakBagTicket, FanSubscribed x k, Maybe a)
1559getFanSubscribed k f sub = do
1544getFanSubscribed k f sub = do
1560 mSubscribed <- liftIO $ readIORef $ fanSubscribed f
1545 mSubscribed <- liftIO $ readIORef $ fanSubscribed f
1561 case mSubscribed of
1546 case mSubscribed of
@@ -1573,16 +1558,13 @@ getFanSubscribed k f sub = do
1573 subscribersRef <- liftIO $ newIORef $ error "getFanSubscribed: subscribersRef not yet initialized"
1558 subscribersRef <- liftIO $ newIORef $ error "getFanSubscribed: subscribersRef not yet initialized"
1574 occRef <- liftIO $ newIORef parentOcc
1559 occRef <- liftIO $ newIORef parentOcc
1575 when (isJust parentOcc) $ scheduleClear occRef
1560 when (isJust parentOcc) $ scheduleClear occRef
1576−#ifdef DEBUG_NODEIDS
1577− nid <- liftIO newNodeId
1579 let subscribed = FanSubscribed
1561 let subscribed = FanSubscribed
1580 { fanSubscribedCachedSubscribed = fanSubscribed f
1562 { fanSubscribedCachedSubscribed = fanSubscribed f
1581 , fanSubscribedOccurrence = occRef
1563 , fanSubscribedOccurrence = occRef
1582 , fanSubscribedParent = subscription
1564 , fanSubscribedParent = subscription
1583 , fanSubscribedSubscribers = subscribersRef
1565 , fanSubscribedSubscribers = subscribersRef
1584#ifdef DEBUG_NODEIDS
1566#ifdef DEBUG_NODEIDS
1585− , fanSubscribedNodeId = nid
1567+ , fanSubscribedNodeId = unsafeNodeId f
1588 let !self = (k, subscribed)
1570 let !self = (k, subscribed)
@@ -1592,7 +1574,7 @@ getFanSubscribed k f sub = do
1592 liftIO $ writeIORef (fanSubscribed f) $ Just subscribed
1574 liftIO $ writeIORef (fanSubscribed f) $ Just subscribed
1593 return (slnForSub, subscribed, coerce $ DMap.lookup k =<< parentOcc)
1575 return (slnForSub, subscribed, coerce $ DMap.lookup k =<< parentOcc)
1595−cleanupFanSubscribed :: GCompare k => (k a, FanSubscribed x k v) -> IO ()
1577+cleanupFanSubscribed :: GCompare k => (k a, FanSubscribed x k) -> IO ()
1596cleanupFanSubscribed (k, subscribed) = do
1578cleanupFanSubscribed (k, subscribed) = do
1597 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1579 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1598 let reducedSubscribers = DMap.delete k subscribers
1580 let reducedSubscribers = DMap.delete k subscribers
@@ -1604,7 +1586,7 @@ cleanupFanSubscribed (k, subscribed) = do
1604 else writeIORef (fanSubscribedSubscribers subscribed) $! reducedSubscribers
1586 else writeIORef (fanSubscribedSubscribers subscribed) $! reducedSubscribers
1606{-# INLINE subscribeFanSubscribed #-}
1588{-# INLINE subscribeFanSubscribed #-}
1607−subscribeFanSubscribed :: GCompare k => k a -> FanSubscribed x k v -> Subscriber x (v a) -> IO WeakBagTicket
1589+subscribeFanSubscribed :: GCompare k => k a -> FanSubscribed x k -> Subscriber x a -> IO WeakBagTicket
1608subscribeFanSubscribed k subscribed sub = do
1590subscribeFanSubscribed k subscribed sub = do
1609 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1591 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1610 case DMap.lookup k subscribers of
1592 case DMap.lookup k subscribers of
@@ -1642,9 +1624,6 @@ getSwitchSubscribed s sub = do
1642 when (isJust parentOcc) $ scheduleClear occRef
1624 when (isJust parentOcc) $ scheduleClear occRef
1643 weakSelf <- liftIO $ newIORef $ error "getSwitchSubscribed: weakSelf not yet initialized"
1625 weakSelf <- liftIO $ newIORef $ error "getSwitchSubscribed: weakSelf not yet initialized"
1644 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupSwitchSubscribed
1626 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupSwitchSubscribed
1645−#ifdef DEBUG_NODEIDS
1646− nid <- liftIO newNodeId
1648 let !subscribed = SwitchSubscribed
1627 let !subscribed = SwitchSubscribed
1649 { switchSubscribedCachedSubscribed = switchSubscribed s
1628 { switchSubscribedCachedSubscribed = switchSubscribed s
1650 , switchSubscribedOccurrence = occRef
1629 , switchSubscribedOccurrence = occRef
@@ -1657,7 +1636,7 @@ getSwitchSubscribed s sub = do
1657 , switchSubscribedCurrentParent = subscriptionRef
1636 , switchSubscribedCurrentParent = subscriptionRef
1658 , switchSubscribedWeakSelf = weakSelf
1637 , switchSubscribedWeakSelf = weakSelf
1659#ifdef DEBUG_NODEIDS
1638#ifdef DEBUG_NODEIDS
1660− , switchSubscribedNodeId = nid
1639+ , switchSubscribedNodeId = unsafeNodeId s
1663 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "switchSubscribedWeakSelf"
1642 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "switchSubscribedWeakSelf"
@@ -1702,9 +1681,6 @@ getCoincidenceSubscribed c sub = do
1702 scheduleClear innerSubdRef
1681 scheduleClear innerSubdRef
1703 weakSelf <- liftIO $ newIORef $ error "getCoincidenceSubscribed: weakSelf not yet implemented"
1682 weakSelf <- liftIO $ newIORef $ error "getCoincidenceSubscribed: weakSelf not yet implemented"
1704 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupCoincidenceSubscribed
1683 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupCoincidenceSubscribed
1705−#ifdef DEBUG_NODEIDS
1706− nid <- liftIO newNodeId
1708 let subscribed = CoincidenceSubscribed
1684 let subscribed = CoincidenceSubscribed
1709 { coincidenceSubscribedCachedSubscribed = coincidenceSubscribed c
1685 { coincidenceSubscribedCachedSubscribed = coincidenceSubscribed c
1710 , coincidenceSubscribedOccurrence = occRef
1686 , coincidenceSubscribedOccurrence = occRef
@@ -1715,7 +1691,7 @@ getCoincidenceSubscribed c sub = do
1715 , coincidenceSubscribedInnerParent = innerSubdRef
1691 , coincidenceSubscribedInnerParent = innerSubdRef
1716 , coincidenceSubscribedWeakSelf = weakSelf
1692 , coincidenceSubscribedWeakSelf = weakSelf
1717#ifdef DEBUG_NODEIDS
1693#ifdef DEBUG_NODEIDS
1718− , coincidenceSubscribedNodeId = nid
1694+ , coincidenceSubscribedNodeId = unsafeNodeId c
1721 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "CoincidenceSubscribed"
1697 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "CoincidenceSubscribed"
@@ -1735,20 +1711,20 @@ subscribeCoincidenceSubscribed subscribed sub = WeakBag.insert sub (coincidenceS
1735{-# INLINE mergeG #-}
1711{-# INLINE mergeG #-}
1736mergeG :: forall k q x v. (HasSpiderTimeline x, GCompare k)
1712mergeG :: forall k q x v. (HasSpiderTimeline x, GCompare k)
1737 => (forall a. q a -> Event x (v a))
1713 => (forall a. q a -> Event x (v a))
1738− -> DynamicS x (PatchDMap k q) -> Event x (DMap k v)
1714+ -> Dynamic x (PatchDMap k q) -> Event x (DMap k v)
1739mergeG nt d = cacheEvent (mergeCheap nt d)
1715mergeG nt d = cacheEvent (mergeCheap nt d)
1741{-# INLINE mergeWithMove #-}
1717{-# INLINE mergeWithMove #-}
1742mergeWithMove :: forall k v q x. (HasSpiderTimeline x, GCompare k)
1718mergeWithMove :: forall k v q x. (HasSpiderTimeline x, GCompare k)
1743 => (forall a. q a -> Event x (v a))
1719 => (forall a. q a -> Event x (v a))
1744− -> DynamicS x (PatchDMapWithMove k q) -> Event x (DMap k v)
1720+ -> Dynamic x (PatchDMapWithMove k q) -> Event x (DMap k v)
1745mergeWithMove nt d = cacheEvent (mergeCheapWithMove nt d)
1721mergeWithMove nt d = cacheEvent (mergeCheapWithMove nt d)
1747{-# INLINE [1] mergeCheap #-}
1723{-# INLINE [1] mergeCheap #-}
1748mergeCheap
1724mergeCheap
1749 :: forall k x q v. (HasSpiderTimeline x, GCompare k)
1725 :: forall k x q v. (HasSpiderTimeline x, GCompare k)
1750 => (forall a. q a -> Event x (v a))
1726 => (forall a. q a -> Event x (v a))
1751− -> DynamicS x (PatchDMap k q)
1727+ -> Dynamic x (PatchDMap k q)
1752 -> Event x (DMap k v)
1728 -> Event x (DMap k v)
1753mergeCheap nt = mergeGCheap' getInitialSubscribers updateMe destroy
1729mergeCheap nt = mergeGCheap' getInitialSubscribers updateMe destroy
@@ -1788,7 +1764,7 @@ mergeCheap nt = mergeGCheap' getInitialSubscribers updateMe destroy
1788{-# INLINE [1] mergeCheapWithMove #-}
1764{-# INLINE [1] mergeCheapWithMove #-}
1789mergeCheapWithMove :: forall k x v q. (HasSpiderTimeline x, GCompare k)
1765mergeCheapWithMove :: forall k x v q. (HasSpiderTimeline x, GCompare k)
1790 => (forall a. q a -> Event x (v a))
1766 => (forall a. q a -> Event x (v a))
1791− -> DynamicS x (PatchDMapWithMove k q)
1767+ -> Dynamic x (PatchDMapWithMove k q)
1792 -> Event x (DMap k v)
1768 -> Event x (DMap k v)
1793mergeCheapWithMove nt = mergeGCheap' getInitialSubscribers updateMe destroy
1769mergeCheapWithMove nt = mergeGCheap' getInitialSubscribers updateMe destroy
@@ -1936,7 +1912,7 @@ updateMerge m updateFunc p = SomeMergeUpdate updateMe (invalidateMergeHeight m)
1937{-# INLINE mergeGCheap' #-}
1913{-# INLINE mergeGCheap' #-}
1938mergeGCheap' :: forall k v x p s q. (HasSpiderTimeline x, GCompare k, PatchTarget p ~ DMap k q)
1914mergeGCheap' :: 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)
1915+ => MergeInitFunc k v q x s -> MergeUpdateFunc k v x p s -> MergeDestroyFunc k s -> Dynamic x p -> Event x (DMap k v)
1940mergeGCheap' getInitialSubscribers updateFunc destroy d = Event $ \sub -> do
1916mergeGCheap' getInitialSubscribers updateFunc destroy d = Event $ \sub -> do
1941 initialParents <- readBehaviorUntracked $ dynamicCurrent d
1917 initialParents <- readBehaviorUntracked $ dynamicCurrent d
1942 accumRef <- liftIO $ newIORef $ error "merge: accumRef not yet initialized"
1918 accumRef <- liftIO $ newIORef $ error "merge: accumRef not yet initialized"
@@ -1985,11 +1961,11 @@ mergeGCheap' getInitialSubscribers updateFunc destroy d = Event $ \sub -> do
1988−mergeInt :: forall x a. (HasSpiderTimeline x) => DynamicS x (PatchIntMap (Event x a)) -> Event x (IntMap a)
1964+mergeInt :: forall x a. (HasSpiderTimeline x) => Dynamic x (PatchIntMap (Event x a)) -> Event x (IntMap a)
1989mergeInt = cacheEvent . mergeIntCheap
1965mergeInt = cacheEvent . mergeIntCheap
1991{-# INLINABLE mergeIntCheap #-}
1967{-# INLINABLE mergeIntCheap #-}
1992−mergeIntCheap :: forall x a. (HasSpiderTimeline x) => DynamicS x (PatchIntMap (Event x a)) -> Event x (IntMap a)
1968+mergeIntCheap :: forall x a. (HasSpiderTimeline x) => Dynamic x (PatchIntMap (Event x a)) -> Event x (IntMap a)
1993mergeIntCheap d = Event $ \sub -> do
1969mergeIntCheap d = Event $ \sub -> do
1994 initialParents <- readBehaviorUntracked $ dynamicCurrent d
1970 initialParents <- readBehaviorUntracked $ dynamicCurrent d
1995 accum <- liftIO $ FastMutableIntMap.newEmpty
1971 accum <- liftIO $ FastMutableIntMap.newEmpty
@@ -2086,16 +2062,14 @@ mergeIntCheap d = Event $ \sub -> do
2088newtype EventSelector x k = EventSelector { select :: forall a. k a -> Event x a }
2064newtype 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) }
2091−fanG :: (HasSpiderTimeline x, GCompare k) => Event x (DMap k v) -> EventSelectorG x k v
2066+fan :: (HasSpiderTimeline x, GCompare k) => Event x (DMap k Identity) -> EventSelector x k
2092−fanG e = unsafePerformIO $ do
2067+fan e =
2093− ref <- newIORef Nothing
2094 let f = Fan
2068 let f = Fan
2095 { fanParent = e
2069 { fanParent = e
2096− , fanSubscribed = ref
2070+ , fanSubscribed = unsafeNewIORef e Nothing
2098− pure $ EventSelectorG $ \k -> eventFan k f
2072+ in EventSelector $ \k -> eventFan k f
2100runHoldInits :: HasSpiderTimeline x => IORef [SomeHoldInit x] -> IORef [SomeDynInit x] -> IORef [SomeMergeInit x] -> EventM x ()
2074runHoldInits :: HasSpiderTimeline x => IORef [SomeHoldInit x] -> IORef [SomeDynInit x] -> IORef [SomeMergeInit x] -> EventM x ()
2101runHoldInits holdInitRef dynInitRef mergeInitRef = do
2075runHoldInits holdInitRef dynInitRef mergeInitRef = do
@@ -2149,7 +2123,7 @@ clearEventEnv (EventEnv toAssignRef holdInitRef dynInitRef mergeUpdateRef mergeI
2149-- | Run an event action outside of a frame
2123-- | Run an event action outside of a frame
2150runFrame :: forall x a. HasSpiderTimeline x => EventM x a -> SpiderHost x a --TODO: This function also needs to hold the mutex
2124runFrame :: forall x a. HasSpiderTimeline x => EventM x a -> SpiderHost x a --TODO: This function also needs to hold the mutex
2151runFrame a = SpiderHost $ do
2125runFrame a = SpiderHost $ do
2152− let env = _spiderTimeline_eventEnv $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
2126+ let env = _spiderTimeline_eventEnv (spiderTimeline :: SpiderTimelineEnv x)
2153 let go = do
2127 let go = do
2154 result <- a
2128 result <- a
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
2129 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 +2334,7 @@ instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x))
2360 fail _ = error "Dynamic does not support 'fail'"
2334 fail _ = error "Dynamic does not support 'fail'"
2362{-# INLINABLE newJoinDyn #-}
2336{-# INLINABLE newJoinDyn #-}
2363−newJoinDyn :: HasSpiderTimeline x => DynamicS x (Identity (DynamicS x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
2337+newJoinDyn :: HasSpiderTimeline x => Reflex.Spider.Internal.Dynamic x (Identity (Reflex.Spider.Internal.Dynamic x (Identity a))) -> Reflex.Spider.Internal.Dyn x (Identity a)
2364newJoinDyn d =
2338newJoinDyn d =
2365 let readV0 = readBehaviorTracked . dynamicCurrent =<< readBehaviorTracked (dynamicCurrent d)
2339 let readV0 = readBehaviorTracked . dynamicCurrent =<< readBehaviorTracked (dynamicCurrent d)
2366 eOuter = Reflex.Spider.Internal.push (fmap (Just . Identity) . readBehaviorUntracked . dynamicCurrent . runIdentity) $ dynamicUpdated d
2340 eOuter = Reflex.Spider.Internal.push (fmap (Just . Identity) . readBehaviorUntracked . dynamicCurrent . runIdentity) $ dynamicUpdated d
@@ -2510,7 +2484,7 @@ unsafeNewSpiderTimelineEnv = do
2510#ifdef DEBUG
2484#ifdef DEBUG
2511 depthRef <- newIORef 0
2485 depthRef <- newIORef 0
2513− return $ STE $ SpiderTimelineEnv
2487+ return $ SpiderTimelineEnv
2514 { _spiderTimeline_lock = lock
2488 { _spiderTimeline_lock = lock
2515 , _spiderTimeline_eventEnv = env
2489 , _spiderTimeline_eventEnv = env
2516#ifdef DEBUG
2490#ifdef DEBUG
@@ -2526,13 +2500,13 @@ data LocalSpiderTimeline (x :: Type) s
2527instance Reifies s (SpiderTimelineEnv x) =>
2501instance Reifies s (SpiderTimelineEnv x) =>
2528 HasSpiderTimeline (LocalSpiderTimeline x s) where
2502 HasSpiderTimeline (LocalSpiderTimeline x s) where
2529− spiderTimeline = localSpiderTimeline Proxy $ reflect (Proxy :: Proxy s)
2503+ spiderTimeline = localSpiderTimeline (Proxy :: Proxy s) $ reflect (Proxy :: Proxy s)
2531localSpiderTimeline
2505localSpiderTimeline
2532− :: proxy s
2506+ :: Proxy s
2533 -> SpiderTimelineEnv x
2507 -> SpiderTimelineEnv x
2534 -> SpiderTimelineEnv (LocalSpiderTimeline x s)
2508 -> SpiderTimelineEnv (LocalSpiderTimeline x s)
2535−localSpiderTimeline _ = coerce
2509+localSpiderTimeline _ = unsafeCoerce
2537-- | Pass a new timeline to the given function.
2511-- | Pass a new timeline to the given function.
2538withSpiderTimeline :: (forall x. HasSpiderTimeline x => SpiderTimelineEnv x -> IO r) -> IO r
2512withSpiderTimeline :: (forall x. HasSpiderTimeline x => SpiderTimelineEnv x -> IO r) -> IO r
@@ -2550,8 +2524,8 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2550 {-# SPECIALIZE instance R.Reflex (SpiderTimeline Global) #-}
2524 {-# SPECIALIZE instance R.Reflex (SpiderTimeline Global) #-}
2551 newtype Behavior (SpiderTimeline x) a = SpiderBehavior { unSpiderBehavior :: Behavior x a }
2525 newtype Behavior (SpiderTimeline x) a = SpiderBehavior { unSpiderBehavior :: Behavior x a }
2552 newtype Event (SpiderTimeline x) a = SpiderEvent { unSpiderEvent :: Event x a }
2526 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)
2527+ newtype Dynamic (SpiderTimeline x) a = SpiderDynamic { unSpiderDynamic :: Dynamic x (Identity a) } -- deriving (Functor, Applicative, Monad)
2554− newtype Incremental (SpiderTimeline x) p = SpiderIncremental { unSpiderIncremental :: DynamicS x p }
2528+ newtype Incremental (SpiderTimeline x) p = SpiderIncremental { unSpiderIncremental :: Dynamic x p }
2555 type PullM (SpiderTimeline x) = SpiderPullM x
2529 type PullM (SpiderTimeline x) = SpiderPullM x
2556 type PushM (SpiderTimeline x) = SpiderPushM x
2530 type PushM (SpiderTimeline x) = SpiderPushM x
2557 {-# INLINABLE never #-}
2531 {-# INLINABLE never #-}
@@ -2564,8 +2538,6 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2564 pushCheap f = SpiderEvent . pushCheap (coerce f) . unSpiderEvent
2538 pushCheap f = SpiderEvent . pushCheap (coerce f) . unSpiderEvent
2565 {-# INLINABLE pull #-}
2539 {-# INLINABLE pull #-}
2566 pull = SpiderBehavior . pull . coerce
2540 pull = SpiderBehavior . pull . coerce
2567− {-# INLINABLE fanG #-}
2568− fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
2569 {-# INLINABLE mergeG #-}
2541 {-# INLINABLE mergeG #-}
2571 :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2543 :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
@@ -2573,6 +2545,8 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2573 -> DMap k q
2545 -> DMap k q
2574 -> R.Event (SpiderTimeline x) (DMap k v)
2546 -> R.Event (SpiderTimeline x) (DMap k v)
2575 mergeG nt = SpiderEvent . mergeG (unSpiderEvent #. nt) . dynamicConst
2547 mergeG nt = SpiderEvent . mergeG (unSpiderEvent #. nt) . dynamicConst
2548+ {-# INLINABLE fan #-}
2549+ fan e = R.EventSelector $ SpiderEvent . select (fan (unSpiderEvent e))
2576 {-# INLINABLE switch #-}
2550 {-# INLINABLE switch #-}
2577 switch = SpiderEvent . switch . (coerce :: Behavior x (R.Event (SpiderTimeline x) a) -> Behavior x (Event x a)) . unSpiderBehavior
2551 switch = SpiderEvent . switch . (coerce :: Behavior x (R.Event (SpiderTimeline x) a) -> Behavior x (Event x a)) . unSpiderBehavior
2578 {-# INLINABLE coincidence #-}
2552 {-# INLINABLE coincidence #-}
@@ -2580,7 +2554,7 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2580 {-# INLINABLE current #-}
2554 {-# INLINABLE current #-}
2581 current = SpiderBehavior . dynamicCurrent . unSpiderDynamic
2555 current = SpiderBehavior . dynamicCurrent . unSpiderDynamic
2582 {-# INLINABLE updated #-}
2556 {-# INLINABLE updated #-}
2583− updated = SpiderEvent #. dynamicUpdated .# fmap coerce . unSpiderDynamic
2557+ updated = coerce $ SpiderEvent . dynamicUpdated . unSpiderDynamic
2584 {-# INLINABLE unsafeBuildDynamic #-}
2558 {-# INLINABLE unsafeBuildDynamic #-}
2585 unsafeBuildDynamic readV0 v' = SpiderDynamic $ dynamicDynIdentity $ unsafeBuildDynamic (coerce readV0) $ coerce $ unSpiderEvent v'
2559 unsafeBuildDynamic readV0 v' = SpiderDynamic $ dynamicDynIdentity $ unsafeBuildDynamic (coerce readV0) $ coerce $ unSpiderEvent v'
2586 {-# INLINABLE unsafeBuildIncremental #-}
2560 {-# INLINABLE unsafeBuildIncremental #-}
@@ -2599,10 +2573,9 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
2599 return $ Identity <$> apply p c --TODO: Avoid the redundant 'apply'
2573 return $ Identity <$> apply p c --TODO: Avoid the redundant 'apply'
2600 eventCoercion Coercion = Coercion
2574 eventCoercion Coercion = Coercion
2601 behaviorCoercion Coercion = Coercion
2575 behaviorCoercion Coercion = Coercion
2602− dynamicCoercion Coercion = Coercion
2576+ dynamicCoercion = unsafeCoerce --TODO: How can we avoid this unsafeCoerce? This is safe only because we know how Identity works as a Patch instance
2603− incrementalCoercion Coercion Coercion = Coercion
2604 {-# INLINABLE mergeIntIncremental #-}
2577 {-# INLINABLE mergeIntIncremental #-}
2605− mergeIntIncremental = SpiderEvent . mergeInt . coerce
2578+ mergeIntIncremental = SpiderEvent . mergeInt . (unsafeCoerce :: Dynamic x (PatchIntMap (R.Event (SpiderTimeline x) a)) -> Dynamic x (PatchIntMap (Event x a))) . unSpiderIncremental
2606 {-# INLINABLE fanInt #-}
2579 {-# INLINABLE fanInt #-}
2607 fanInt e = R.EventSelectorInt $ SpiderEvent . selectInt (fanInt (unSpiderEvent e))
2580 fanInt e = R.EventSelectorInt $ SpiderEvent . selectInt (fanInt (unSpiderEvent e))