@@ -3,7 +3,6 @@ module Wire.Event where
33import Prelude
44import Control.Alt (class Alt , alt )
55import Control.Alternative (class Alternative , class Plus , empty )
6- import Control.Apply (lift2 )
76import Control.Monad.Rec.Class (class MonadRec , Step (..), tailRecM )
87import Data.Array as Array
98import Data.Either (either , hush )
@@ -26,18 +25,18 @@ create = do
2625 subscribers <- Ref .new []
2726 let
2827 event =
29- Event \notify -> do
28+ Event \emit -> do
3029 unsubscribing <- Ref .new false
3130 let
3231 isUnsubscribing = Ref .read unsubscribing
3332
34- subscriber = \a -> unlessM isUnsubscribing $ notify a
35- Ref .modify_ (flip Array .snoc subscriber) subscribers
33+ subscriber = \a -> unlessM isUnsubscribing do emit a
34+ Ref .modify_ (_ <> [ subscriber ] ) subscribers
3635 pure do
3736 Ref .write true unsubscribing
3837 Ref .modify_ (Array .deleteBy unsafeRefEq subscriber) subscribers
3938
40- push a = Ref .read subscribers >>= traverse_ \notify -> notify a
39+ push a = Ref .read subscribers >>= traverse_ \emit -> emit a
4140 pure { event, push }
4241
4342makeEvent :: forall a . ((a -> Effect Unit ) -> Effect (Effect Unit )) -> Event a
@@ -47,15 +46,15 @@ subscribe :: forall a b. Event a -> (a -> Effect b) -> Effect (Effect Unit)
4746subscribe (Event event) = event <<< (void <<< _)
4847
4948filter :: forall a . (a -> Boolean ) -> Event a -> Event a
50- filter pred (Event event) = Event \notify -> event \a -> if pred a then notify a else pure unit
49+ filter pred (Event event) = Event \emit -> event \a -> if pred a then emit a else pure unit
5150
5251fold :: forall a b . (b -> a -> b ) -> b -> Event a -> Event b
5352fold f b (Event event) =
54- Event \notify -> do
53+ Event \emit -> do
5554 accum <- Ref .new b
5655 event \a -> do
5756 value <- Ref .modify (flip f a) accum
58- notify value
57+ emit value
5958
6059share :: forall a . Event a -> Effect (Event a )
6160share source = do
@@ -76,21 +75,21 @@ share source = do
7675 Ref .write Nothing cancelSource
7776
7877 event =
79- Event \notify -> do
78+ Event \emit -> do
8079 incrementCount
81- cancel <- subscribe shared.event notify
80+ cancel <- subscribe shared.event emit
8281 pure $ cancel *> decrementCount
8382 pure event
8483
8584distinct :: forall a . Eq a => Event a -> Event a
8685distinct (Event event) =
87- Event \notify -> do
86+ Event \emit -> do
8887 latest <- Ref .new Nothing
8988 event \a -> do
9089 b <- Ref .read latest
9190 when (pure a /= b) do
9291 Ref .write (pure a) latest
93- notify a
92+ emit a
9493
9594bufferUntil :: forall b a . Event b -> Event a -> Event (Array a )
9695bufferUntil flush source =
@@ -105,28 +104,28 @@ bufferUntil flush source =
105104
106105fromFoldable :: forall a f . Foldable f => f a -> Event a
107106fromFoldable xs =
108- Event \notify -> do
107+ Event \emit -> do
109108 fiber <-
110109 Aff .launchAff
111110 $ for_ xs \x -> do
112- liftEffect $ notify x
111+ liftEffect $ emit x
113112 Aff .delay (Milliseconds 0.0 )
114113 pure
115114 $ Aff .launchAff_
116115 $ Aff .killFiber (Aff .error " canceled" ) fiber
117116
118117range :: Int -> Int -> Event Int
119118range start end =
120- Event \notify -> do
119+ Event \emit -> do
121120 let
122121 go pos
123122 | pos /= end = do
124- liftEffect $ notify pos
123+ liftEffect $ emit pos
125124 Aff .delay (Milliseconds 0.0 )
126125 pure (Loop (pos + step))
127126
128127 go _ = do
129- liftEffect $ notify end
128+ liftEffect $ emit end
130129 pure (Done unit)
131130 fiber <- Aff .launchAff $ tailRecM go start
132131 pure
@@ -142,22 +141,22 @@ times n
142141times _ = empty
143142
144143instance functorEvent :: Functor Event where
145- map f (Event event) = Event \notify -> event (notify <<< f)
144+ map f (Event event) = Event \emit -> event (emit <<< f)
146145
147146instance applyEvent :: Apply Event where
148147 apply = ap
149148
150149instance applicativeEvent :: Applicative Event where
151- pure a = Event \notify -> notify a *> mempty
150+ pure a = Event \emit -> emit a *> mempty
152151
153152instance bindEvent :: Bind Event where
154153 bind (Event outer) f =
155- Event \notify -> do
154+ Event \emit -> do
156155 cancelInner <- Ref .new Nothing
157156 cancelOuter <-
158157 outer \a -> do
159158 Ref .read cancelInner >>= sequence_
160- c <- subscribe (f a) notify
159+ c <- subscribe (f a) emit
161160 Ref .write (Just c) cancelInner
162161 pure do
163162 Ref .read cancelInner >>= sequence_
@@ -181,17 +180,11 @@ instance alternativeEvent :: Alternative Event
181180
182181instance altEvent :: Alt Event where
183182 alt (Event event1) (Event event2) =
184- Event \notify -> do
185- cancel1 <- event1 notify
186- cancel2 <- event2 notify
183+ Event \emit -> do
184+ cancel1 <- event1 emit
185+ cancel2 <- event2 emit
187186 pure $ cancel1 *> cancel2
188187
189- instance semigroupEvent :: Semigroup a => Semigroup (Event a ) where
190- append = lift2 append
191-
192- instance monoidEvent :: Monoid a => Monoid (Event a ) where
193- mempty = pure mempty
194-
195188instance compactableEvent :: Compactable Event where
196189 compact = filterMap identity
197190 separate = partitionMap identity
0 commit comments