Skip to content

Commit c7f2c4f

Browse files
committed
Move things around, add a bunch of tests and instances
1 parent 9fe5a0b commit c7f2c4f

File tree

8 files changed

+1012
-696
lines changed

8 files changed

+1012
-696
lines changed

src/Control/Monad/Aff.js

Lines changed: 0 additions & 441 deletions
This file was deleted.

src/Control/Monad/Aff.purs

Lines changed: 11 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,27 @@
11
module Control.Monad.Aff
2-
( Aff
3-
, Thread
4-
, Canceler(..)
5-
, attempt
6-
, bracket
7-
, runAff
8-
, launchAff
2+
( module Internal
93
, forkAff
4+
, runAff
105
, killThread
116
, joinThread
12-
, onComplete
137
) where
148

159
import Prelude
16-
import Data.Function.Uncurried as Fn
17-
import Control.Monad.Eff (Eff, kind Effect)
18-
import Control.Monad.Eff.Class (class MonadEff, liftEff)
19-
import Control.Monad.Eff.Exception (EXCEPTION, Error)
20-
import Control.Monad.Error.Class (class MonadError, class MonadThrow)
21-
import Control.Monad.Rec.Class (class MonadRec, Step(..))
22-
import Data.Either (Either(..), isLeft)
23-
import Partial.Unsafe (unsafeCrashWith)
24-
import Type.Row.Effect.Equality (class EffectRowEquals, effTo)
25-
26-
foreign import data Aff :: # Effect Type Type
27-
28-
instance functorAffFunctor (Aff eff) where map = _map
29-
instance applyAffApply (Aff eff) where apply = ap
30-
instance applicativeAffApplicative (Aff eff) where pure = _pure
31-
instance bindAffBind (Aff eff) where bind = _bind
32-
instance monadAffMonad (Aff eff)
33-
34-
instance monadRecAffMonadRec (Aff eff) where
35-
tailRecM k = go
36-
where
37-
go a = do
38-
res ← k a
39-
case res of
40-
Done r → pure r
41-
Loop b → go b
42-
43-
instance monadThrowAffMonadThrow Error (Aff eff) where
44-
throwError = _throwError
45-
46-
instance monadErrorAffMonadError Error (Aff eff) where
47-
catchError aff k = do
48-
res ← attempt aff
49-
case res of
50-
Left err → k err
51-
Right r → pure r
52-
53-
instance monadEffAffEffectRowEquals eff1 (exceptionEXCEPTION | eff2) MonadEff eff1 (Aff eff2) where
54-
liftEff eff = Fn.runFn3 _liftEff Left Right (effTo eff)
55-
56-
newtype Thread eff a = Thread
57-
{ kill Error Aff eff Unit
58-
, join Aff eff a
59-
}
60-
61-
instance functorThreadFunctor (Thread eff) where
62-
map f (Thread { kill, join }) = Thread { kill, join: f <$> join }
10+
import Control.Monad.Aff.Internal (ASYNC, Aff, AffModality, Thread(..), attempt, launchAff, unsafeLaunchAff, unsafeLiftEff)
11+
import Control.Monad.Aff.Internal (Aff, AffModality, ParAff, Thread, Canceler(..), ASYNC, attempt, bracket, delay, launchAff, makeAff, nonCanceler) as Internal
12+
import Control.Monad.Eff (Eff)
13+
import Control.Monad.Eff.Class (liftEff)
14+
import Control.Monad.Eff.Exception (Error)
15+
import Data.Either (Either(..))
6316

64-
newtype Canceler eff = Canceler (Error Aff eff Unit)
65-
66-
attempt eff a. Aff eff a Aff eff (Either Error a)
67-
attempt = _attempt
68-
69-
bracket eff a b. Aff eff a (a Aff eff Unit) (a Aff eff b) Aff eff b
70-
bracket = _bracket
71-
72-
launchAff eff a. Aff eff a Eff eff (Thread eff a)
73-
launchAff aff = Fn.runFn6 _drainAff isLeft unsafeFromLeft unsafeFromRight Left Right aff
74-
where
75-
unsafeFromLeft x y. Either x y x
76-
unsafeFromLeft = case _ of
77-
Left a → a
78-
Right _ → unsafeCrashWith "unsafeFromLeft: Right"
79-
80-
unsafeFromRight x y. Either x y y
81-
unsafeFromRight = case _ of
82-
Right a → a
83-
Left _ → unsafeCrashWith "unsafeFromRight: Left"
84-
85-
runAff eff a. (Either Error a Eff (exception EXCEPTION | eff) Unit) Aff eff a Eff eff Unit
86-
runAff k aff = do
87-
thread ← launchAff aff
88-
onComplete k thread
17+
runAff eff a. (Either Error a Eff (AffModality eff) Unit) Aff eff a Eff (async ASYNC | eff) Unit
18+
runAff k aff = void $ launchAff $ liftEff <<< k =<< attempt aff
8919

9020
forkAff eff a. Aff eff a Aff eff (Thread eff a)
91-
forkAff = _unsafeSync <<< map Right <<< launchAff
21+
forkAff = unsafeLiftEff <<< map Right <<< unsafeLaunchAff
9222

9323
killThread eff a. Error Thread eff a Aff eff Unit
9424
killThread e (Thread t) = t.kill e
9525

9626
joinThread eff a. Thread eff a Aff eff a
9727
joinThread (Thread t) = t.join
98-
99-
onComplete eff a. (Either Error a Eff (exception EXCEPTION | eff) Unit) Thread eff a Eff eff Unit
100-
onComplete k t = void $ launchAff do
101-
res ← attempt (joinThread t)
102-
liftEff (k res)
103-
104-
foreign import _pure eff a. a Aff eff a
105-
foreign import _throwError eff a. Error Aff eff a
106-
foreign import _unsafeSync eff a. Eff eff (Either Error a) Aff eff a
107-
foreign import _unsafeAsync eff a. ((Either Error a Eff eff Unit) Eff eff (Canceler eff)) Aff eff a
108-
foreign import _map eff a b. (a b) Aff eff a Aff eff b
109-
foreign import _bind eff a b. Aff eff a (a Aff eff b) Aff eff b
110-
foreign import _attempt eff a. Aff eff a Aff eff (Either Error a)
111-
foreign import _bracket eff a b. Aff eff a (a Aff eff Unit) (a Aff eff b) Aff eff b
112-
113-
foreign import _liftEff
114-
forall eff1 eff2 a
115-
. Fn.Fn3
116-
(Error Either Error a)
117-
(a Either Error a)
118-
(Eff (exception EXCEPTION | eff1) a)
119-
(Aff eff2 a)
120-
121-
foreign import _drainAff
122-
eff a
123-
. Fn.Fn6
124-
(Either Error a Boolean)
125-
(Either Error a Error)
126-
(Either Error a a)
127-
(Error Either Error a)
128-
(a Either Error a)
129-
(Aff eff a)
130-
(Eff eff (Thread eff a))
131-

src/Control/Monad/Aff/Class.purs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Control.Monad.Aff.Class where
22

33
import Prelude
44

5-
import Control.Monad.Aff (Aff)
5+
import Control.Monad.Aff (Aff, ASYNC)
66
import Control.Monad.Cont.Trans (ContT)
77
import Control.Monad.Eff.Class (class MonadEff)
88
import Control.Monad.Eff.Exception (EXCEPTION)
@@ -17,32 +17,32 @@ import Control.Monad.Writer.Trans (WriterT)
1717

1818
import Data.Monoid (class Monoid)
1919

20-
class MonadEff (exceptionEXCEPTION | eff) m <= MonadAff eff m | m -> eff where
20+
class MonadEff (exceptionEXCEPTION, asyncASYNC | eff) m MonadAff eff m | m eff where
2121
liftAff :: forall a. Aff eff a -> m a
2222

23-
instance monadAffAff :: MonadAff e (Aff e) where
23+
instance monadAffAff MonadAff e (Aff e) where
2424
liftAff = id
2525

26-
instance monadAffContT :: MonadAff eff m => MonadAff eff (ContT r m) where
26+
instance monadAffContT MonadAff eff m MonadAff eff (ContT r m) where
2727
liftAff = lift <<< liftAff
2828

29-
instance monadAffExceptT :: MonadAff eff m => MonadAff eff (ExceptT e m) where
29+
instance monadAffExceptT MonadAff eff m MonadAff eff (ExceptT e m) where
3030
liftAff = lift <<< liftAff
3131

32-
instance monadAffListT :: MonadAff eff m => MonadAff eff (ListT m) where
32+
instance monadAffListT MonadAff eff m MonadAff eff (ListT m) where
3333
liftAff = lift <<< liftAff
3434

35-
instance monadAffMaybe :: MonadAff eff m => MonadAff eff (MaybeT m) where
35+
instance monadAffMaybe MonadAff eff m MonadAff eff (MaybeT m) where
3636
liftAff = lift <<< liftAff
3737

38-
instance monadAffReader :: MonadAff eff m => MonadAff eff (ReaderT r m) where
38+
instance monadAffReader MonadAff eff m MonadAff eff (ReaderT r m) where
3939
liftAff = lift <<< liftAff
4040

41-
instance monadAffRWS :: (MonadAff eff m, Monoid w) => MonadAff eff (RWST r w s m) where
41+
instance monadAffRWS (MonadAff eff m, Monoid w) MonadAff eff (RWST r w s m) where
4242
liftAff = lift <<< liftAff
4343

44-
instance monadAffState :: MonadAff eff m => MonadAff eff (StateT s m) where
44+
instance monadAffState MonadAff eff m MonadAff eff (StateT s m) where
4545
liftAff = lift <<< liftAff
4646

47-
instance monadAffWriter :: (MonadAff eff m, Monoid w) => MonadAff eff (WriterT w m) where
47+
instance monadAffWriter (MonadAff eff m, Monoid w) MonadAff eff (WriterT w m) where
4848
liftAff = lift <<< liftAff

0 commit comments

Comments
 (0)