Skip to content

Commit

Permalink
Queue now deals with pure values, no execution (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdp authored May 19, 2020
1 parent 70b863e commit 20c9de5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/Wire/Event.purs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ create = do
pure do
Ref.write true unsubscribing
Ref.modify_ (Array.deleteBy unsafeRefEq subscriber) subscribers
pure { event, push: (queue.push <<< pure), cancel: queue.kill }
pure { event, push: queue.push, cancel: queue.kill }

makeEvent :: forall a. (Subscriber a -> Effect Canceller) -> Event a
makeEvent = Event
Expand Down Expand Up @@ -145,7 +145,7 @@ instance functorEvent :: Functor Event where
map f (Event event) =
Event \emit -> do
queue <- Queue.create (emit <<< f)
cancel <- event (queue.push <<< pure)
cancel <- event queue.push
pure do
cancel
queue.kill
Expand Down
6 changes: 2 additions & 4 deletions src/Wire/Event/Queue.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import Prelude
import Control.Monad.Rec.Class (forever)
import Effect (Effect)
import Effect.AVar as AVar
import Effect.Aff (Aff)
import Effect.Aff as Aff
import Effect.Aff.AVar as AffVar
import Effect.Class (liftEffect)

create :: forall a b. (a -> Effect b) -> Effect { push :: Aff a -> Effect Unit, kill :: Effect Unit }
create :: forall a b. (a -> Effect b) -> Effect { push :: a -> Effect Unit, kill :: Effect Unit }
create consumer = do
queue <- AVar.empty
fiber <-
(Aff.launchAff <<< forever) do
aff <- AffVar.take queue
a <- aff
a <- AffVar.take queue
liftEffect do consumer a
let
killFiber = Aff.launchAff_ do Aff.killFiber (Aff.error "killing queue consumer") fiber
Expand Down
6 changes: 5 additions & 1 deletion src/Wire/Event/Time.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ delay offset event = do
ms = fromDuration offset
Event.makeEvent \emit -> do
queue <- Queue.create emit
cancel <- Event.subscribe event \a -> queue.push do Aff.delay ms *> pure a
cancel <-
Event.subscribe event \a ->
Aff.launchAff_ do
Aff.delay ms
liftEffect do queue.push a
pure do
cancel
queue.kill
Expand Down
12 changes: 5 additions & 7 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module Test.Main where

import Prelude
import Control.Alt ((<|>))
import Data.Array as Array
import Data.FoldableWithIndex (foldlWithIndex)
import Data.Int as Int
import Data.List.Lazy (range)
import Data.String.CodeUnits as CodeUnits
import Effect (Effect)
import Effect.Class.Console as Console
Expand All @@ -14,13 +12,13 @@ import Wire.Event as Event

main :: Effect Unit
main = do
void $ Event.subscribe (Event.distinct (sumFromOneToOneHundred <|> sumFromOneToOneHundred) >>= pure <<< formatNumber <<< show) do Console.log
void $ Event.subscribe ((\a b -> "Hi " <> show (a + b)) <$> Event.range 1 5 <*> (Event.range 6 10 >>= pure)) do Console.logShow

sumFromOneToOneHundred :: Event Number
sumFromOneToOneHundred =
range 1 100
# Event.fromFoldable
sumOfSquaresFromOneToOneThousand :: Event Number
sumOfSquaresFromOneToOneThousand =
Event.range 1 1_000
# map Int.toNumber
# map (\x -> x * x)
# Event.fold (+) 0.0

formatNumber :: String -> String
Expand Down

0 comments on commit 20c9de5

Please sign in to comment.