on develop help sign in
use fan at top level instead of filtering with fmapMaybe at subscription sites
commit766353a7cf771e0a24d09aa8936dfabbd3168b54 authorJoe Betz <joebetz91@gmail.com> dateSat 6 Jun 2020, 11:06 (6 years ago) parent7f051bf
1 file changed+7−7
src/Reflex/PubSub.hs+7−7
src/Reflex/PubSub.hs+7−7
@@ -9,7 +9,7 @@
99
1010module Reflex.PubSub where
1111
12−import Prelude hiding (lookup)
12+import Prelude hiding (lookup)
1313
1414import Control.Monad.Reader
1515import Data.Constraint.Extras
@@ -19,7 +19,7 @@ import Data.Functor.Identity
1919
2020import Reflex
2121
22−type PubSubT t tag m = EventWriterT t [DSum tag Identity] (ReaderT (Event t (DMap tag Identity)) m)
22+type PubSubT t tag m = EventWriterT t [DSum tag Identity] (ReaderT (EventSelector t tag) m)
2323
2424-- | 'PubSub' enables you to broadcast events from anywhere in your reflex network to
2525-- any location that is subscribed to the topic they are published to. Topics are best
@@ -31,14 +31,14 @@ class (Reflex t, Monad m, Semigroup a) => PubSub t tag m a where
3131
3232runPubSubT :: (MonadFix m, Has Semigroup tag, GCompare tag, Reflex t) => PubSubT t tag m a -> m a
3333runPubSubT pubSub = mdo
34− (result, publishE) <- runReaderT (runEventWriterT pubSub) $
35− fromListWithKey (\k v1 v2 -> has @Semigroup k (v1 <> v2)) <$> publishE
34+ (result, publishE) <- runReaderT (runEventWriterT pubSub) $ do
35+ fan $ fromListWithKey (\k v1 v2 -> has @Semigroup k (v1 <> v2)) <$> publishE
3636 pure result
3737
38−instance (Reflex t, Monad m, GCompare tag, Semigroup a) => PubSub t tag (PubSubT t tag m) a where
38+instance (Reflex t, Monad m, Semigroup a) => PubSub t tag (PubSubT t tag m) a where
3939 publish topic payloadE =
4040 tellEvent $ (\pl -> [topic ==> pl]) <$> payloadE
4141
4242 subscribe topic = do
43− topicPublishE <- asks $ fmap (lookup topic)
44− pure $ fmapMaybe (fmap runIdentity) topicPublishE
43+ eventSelector <- asks select
44+ pure $ eventSelector topic