|
1 | 1 | module Control.Monad.Aff
|
2 |
| - ( Aff |
3 |
| - , Thread |
4 |
| - , Canceler(..) |
5 |
| - , attempt |
6 |
| - , bracket |
7 |
| - , runAff |
8 |
| - , launchAff |
| 2 | + ( module Internal |
9 | 3 | , forkAff
|
| 4 | + , runAff |
10 | 5 | , killThread
|
11 | 6 | , joinThread
|
12 |
| - , onComplete |
13 | 7 | ) where
|
14 | 8 |
|
15 | 9 | 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 functorAff ∷ Functor (Aff eff) where map = _map |
29 |
| -instance applyAff ∷ Apply (Aff eff) where apply = ap |
30 |
| -instance applicativeAff ∷ Applicative (Aff eff) where pure = _pure |
31 |
| -instance bindAff ∷ Bind (Aff eff) where bind = _bind |
32 |
| -instance monadAff ∷ Monad (Aff eff) |
33 |
| - |
34 |
| -instance monadRecAff ∷ MonadRec (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 monadThrowAff ∷ MonadThrow Error (Aff eff) where |
44 |
| - throwError = _throwError |
45 |
| - |
46 |
| -instance monadErrorAff ∷ MonadError 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 monadEffAff ∷ EffectRowEquals eff1 (exception ∷ EXCEPTION | 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 functorThread ∷ Functor (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(..)) |
63 | 16 |
|
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 |
89 | 19 |
|
90 | 20 | forkAff ∷ ∀ eff a. Aff eff a → Aff eff (Thread eff a)
|
91 |
| -forkAff = _unsafeSync <<< map Right <<< launchAff |
| 21 | +forkAff = unsafeLiftEff <<< map Right <<< unsafeLaunchAff |
92 | 22 |
|
93 | 23 | killThread ∷ ∀ eff a. Error → Thread eff a → Aff eff Unit
|
94 | 24 | killThread e (Thread t) = t.kill e
|
95 | 25 |
|
96 | 26 | joinThread ∷ ∀ eff a. Thread eff a → Aff eff a
|
97 | 27 | 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 |
| - |
0 commit comments