Skip to content

Commit

Permalink
Updates for 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
natefaubion committed Mar 28, 2017
1 parent eddf851 commit 6da848d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
14 changes: 7 additions & 7 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
"package.json"
],
"dependencies": {
"purescript-console": "^2.0.0",
"purescript-exceptions": "^2.0.0",
"purescript-functions": "^2.0.0",
"purescript-parallel": "^2.0.0",
"purescript-transformers": "^2.0.1",
"purescript-unsafe-coerce": "^2.0.0"
"purescript-console": "^3.0.0",
"purescript-exceptions": "^3.0.0",
"purescript-functions": "^3.0.0",
"purescript-parallel": "^3.0.0",
"purescript-transformers": "^3.0.0",
"purescript-unsafe-coerce": "^3.0.0"
},
"devDependencies": {
"purescript-partial": "^1.1.2"
"purescript-partial": "^1.2.0"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"jscs": "^3.0.7",
"jshint": "^2.9.4",
"pulp": "^10.0.0",
"purescript-psa": "^0.4.0",
"purescript": "^0.10.1",
"purescript-psa": "^0.5.0-rc.1",
"purescript": "^0.11.0",
"rimraf": "^2.5.4"
}
}
21 changes: 12 additions & 9 deletions src/Control/Monad/Aff.purs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import Prelude
import Control.Alt (class Alt)
import Control.Alternative (class Alternative)
import Control.Monad.Aff.Internal (AVBox, AVar, _killVar, _putVar, _takeVar, _makeVar)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff (Eff, kind Effect)
import Control.Monad.Eff.Class (class MonadEff)
import Control.Monad.Eff.Exception (Error, EXCEPTION, throwException, error)
import Control.Monad.Error.Class (class MonadError, throwError)
import Control.Monad.Error.Class (class MonadThrow, class MonadError, throwError)
import Control.Monad.Rec.Class (class MonadRec, Step(..))
import Control.MonadPlus (class MonadZero, class MonadPlus)
import Control.Parallel (class Parallel)
Expand All @@ -47,7 +47,7 @@ import Unsafe.Coerce (unsafeCoerce)
-- | errors or produces a value of type `a`.
-- |
-- | This is moral equivalent of `ErrorT (ContT Unit (Eff e)) a`.
foreign import data Aff :: # ! -> * -> *
foreign import data Aff :: # Effect -> Type -> Type

-- | A pure asynchronous computation, having no effects other than
-- | asynchronous computation.
Expand Down Expand Up @@ -80,12 +80,12 @@ cancelWith aff c = runFn3 _cancelWith nonCanceler aff c
-- | If you do need to handle exceptions, you can use `runAff` instead, or
-- | you can handle the exception within the Aff computation, using
-- | `catchError` (or any of the other mechanisms).
launchAff :: forall e a. Aff e a -> Eff (err :: EXCEPTION | e) (Canceler e)
launchAff :: forall e a. Aff e a -> Eff (exception :: EXCEPTION | e) (Canceler e)
launchAff = lowerEx <<< runAff throwException (const (pure unit)) <<< liftEx
where
liftEx :: Aff e a -> Aff (err :: EXCEPTION | e) a
liftEx :: Aff e a -> Aff (exception :: EXCEPTION | e) a
liftEx = _unsafeInterleaveAff
lowerEx :: Eff (err :: EXCEPTION | e) (Canceler (err :: EXCEPTION | e)) -> Eff (err :: EXCEPTION | e) (Canceler e)
lowerEx :: Eff (exception :: EXCEPTION | e) (Canceler (exception :: EXCEPTION | e)) -> Eff (exception :: EXCEPTION | e) (Canceler e)
lowerEx = map (Canceler <<< map _unsafeInterleaveAff <<< cancel)

-- | Runs the asynchronous computation. You must supply an error callback and a
Expand Down Expand Up @@ -149,7 +149,7 @@ apathize :: forall e a. Aff e a -> Aff e Unit
apathize a = const unit <$> attempt a

-- | Lifts a synchronous computation and makes explicit any failure from exceptions.
liftEff' :: forall e a. Eff (err :: EXCEPTION | e) a -> Aff e (Either Error a)
liftEff' :: forall e a. Eff (exception :: EXCEPTION | e) a -> Aff e (Either Error a)
liftEff' eff = attempt (_unsafeInterleaveAff (runFn2 _liftEff nonCanceler eff))

-- | A constant canceller that always returns false.
Expand Down Expand Up @@ -183,11 +183,14 @@ instance monadAff :: Monad (Aff e)
instance monadEffAff :: MonadEff e (Aff e) where
liftEff eff = runFn2 _liftEff nonCanceler eff

-- | Allows users to catch and throw errors on the error channel of the
-- | Allows users to throw errors on the error channel of the
-- | asynchronous computation. See documentation in `purescript-transformers`.
instance monadErrorAff :: MonadError Error (Aff e) where
instance monadThrowAff :: MonadThrow Error (Aff e) where
throwError e = runFn2 _throwError nonCanceler e

-- | Allows users to catch errors on the error channel of the
-- | asynchronous computation. See documentation in `purescript-transformers`.
instance monadErrorAff :: MonadError Error (Aff e) where
catchError aff ex = attempt aff >>= either ex pure

instance altAff :: Alt (Aff e) where
Expand Down
3 changes: 2 additions & 1 deletion src/Control/Monad/Aff/AVar.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import Prelude
import Control.Monad.Aff (Aff, nonCanceler)
import Control.Monad.Aff.Internal (AVar) as Exports
import Control.Monad.Aff.Internal (AVBox, AVar, _killVar, _putVar, _takeVar, _peekVar, _makeVar)
import Control.Monad.Eff (kind Effect)
import Control.Monad.Eff.Exception (Error())

import Data.Function.Uncurried (runFn3, runFn2)

import Unsafe.Coerce (unsafeCoerce)

foreign import data AVAR :: !
foreign import data AVAR :: Effect

type AffAVar e a = Aff (avar :: AVAR | e) a

Expand Down
4 changes: 2 additions & 2 deletions src/Control/Monad/Aff/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import Control.Monad.Eff.Exception (Error)

import Data.Function.Uncurried (Fn2, Fn3)

foreign import data AVar :: * -> *
foreign import data AVar :: Type -> Type

foreign import data AVBox :: * -> *
foreign import data AVBox :: Type -> Type

foreign import _makeVar :: forall c a. c -> AVBox (AVar a)

Expand Down
14 changes: 7 additions & 7 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ test_apathize = do
test_putTakeVar :: TestAVar Unit
test_putTakeVar = do
v <- makeVar
forkAff (later $ putVar v 1.0)
_ <- forkAff (later $ putVar v 1.0)
a <- takeVar v
log ("Success: Value " <> show a)

test_peekVar :: TestAVar Unit
test_peekVar = do
timeout 1000 do
v <- makeVar
forkAff (later $ putVar v 1.0)
_ <- forkAff (later $ putVar v 1.0)
a1 <- peekVar v
a2 <- takeVar v
when (a1 /= a2) do
Expand All @@ -101,7 +101,7 @@ test_peekVar = do
timeout 1000 do
x <- makeVar
res <- makeVar' 1
forkAff do
_ <- forkAff do
c <- peekVar x
putVar x 1000
d <- peekVar x
Expand Down Expand Up @@ -176,13 +176,13 @@ test_semigroupCanceler =

test_cancelLater :: TestAVar Unit
test_cancelLater = do
c <- forkAff $ (do pure "Binding"
c <- forkAff $ (do _ <- pure "Binding"
_ <- later' 100 $ log ("Failure: Later was not canceled!")
pure "Binding")
v <- cancel c (error "Cause")
log (if v then "Success: Canceled later" else "Failure: Did not cancel later")

test_cancelLaunchLater :: forall e. Eff (console :: CONSOLE, err :: EXCEPTION | e) Unit
test_cancelLaunchLater :: forall e. Eff (console :: CONSOLE, exception :: EXCEPTION | e) Unit
test_cancelLaunchLater = do
c <- launchAff $ later' 100 $ log ("Failure: Later was not canceled!")
void $ launchAff $ (do v <- cancel c (error "Cause")
Expand Down Expand Up @@ -249,7 +249,7 @@ loopAndBounce n = do
all :: forall eff. Int -> Aff (console :: CONSOLE, avar :: AVAR | eff) Unit
all n = do
var <- makeVar' 0
forkAll $ replicateArray n (modifyVar (_ + 1) var)
_ <- forkAll $ replicateArray n (modifyVar (_ + 1) var)
count <- takeVar var
log ("Forked " <> show count)

Expand All @@ -262,7 +262,7 @@ cancelAll n = do
delay :: forall eff. Int -> Aff eff Unit
delay n = later' n (pure unit)

main :: Eff (console :: CONSOLE, avar :: AVAR, err :: EXCEPTION) Unit
main :: Eff (console :: CONSOLE, avar :: AVAR, exception :: EXCEPTION) Unit
main = do
Eff.log "Testing kill of later launched in separate Aff"
test_cancelLaunchLater
Expand Down

0 comments on commit 6da848d

Please sign in to comment.