Skip to content

Commit 1eff561

Browse files
committed
Remove Either from Sync constructor
1 parent c7f2c4f commit 1eff561

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

src/Control/Monad/Aff.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import Control.Monad.Aff.Internal (Aff, AffModality, ParAff, Thread, Canceler(..
1212
import Control.Monad.Eff (Eff)
1313
import Control.Monad.Eff.Class (liftEff)
1414
import Control.Monad.Eff.Exception (Error)
15-
import Data.Either (Either(..))
15+
import Data.Either (Either)
1616

1717
runAff eff a. (Either Error a Eff (AffModality eff) Unit) Aff eff a Eff (async ASYNC | eff) Unit
1818
runAff k aff = void $ launchAff $ liftEff <<< k =<< attempt aff
1919

2020
forkAff eff a. Aff eff a Aff eff (Thread eff a)
21-
forkAff = unsafeLiftEff <<< map Right <<< unsafeLaunchAff
21+
forkAff = unsafeLiftEff <<< unsafeLaunchAff
2222

2323
killThread eff a. Error Thread eff a Aff eff Unit
2424
killThread e (Thread t) = t.kill e

src/Control/Monad/Aff/Internal.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ efficiency sake.
88
data Aff eff a
99
= Pure a
1010
| Throw Error
11-
| Sync (Eff eff (Either Error a))
11+
| Sync (Eff eff a)
1212
| Async ((Either Error a -> Eff eff Unit) -> Eff eff (Canceler eff))
1313
| forall b. Attempt (Aff eff b) ?(Either Error b -> a)
1414
| forall b. Bracket (Aff eff b) (b -> Aff eff Unit) (b -> Aff eff a)
@@ -86,16 +86,6 @@ exports.bracket = function (acquire) {
8686
};
8787
};
8888

89-
exports._liftEff = function (left, right, eff) {
90-
return new Aff(SYNC, function () {
91-
try {
92-
return right(eff());
93-
} catch (error) {
94-
return left(error);
95-
}
96-
});
97-
};
98-
9989
exports._makeAff = function (left, right, aff) {
10090
return new Aff(ASYNC, function (k) {
10191
return function () {
@@ -229,7 +219,7 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
229219

230220
case SYNC:
231221
status = BLOCKED;
232-
result = step._1();
222+
result = runSync(step._1);
233223
if (isLeft(result)) {
234224
status = RETURN;
235225
fail = result;
@@ -405,6 +395,14 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
405395
}
406396
}
407397

398+
function runSync (eff) {
399+
try {
400+
return right(eff());
401+
} catch (error) {
402+
return left(error)
403+
}
404+
}
405+
408406
function addJoinCallback (cb) {
409407
var jid = joinId++;
410408
joins[jid] = cb;

src/Control/Monad/Aff/Internal.purs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import Data.Monoid (class Monoid, mempty)
3939
import Data.Newtype (class Newtype)
4040
import Data.Time.Duration (Milliseconds(..))
4141
import Partial.Unsafe (unsafeCrashWith)
42-
import Type.Row.Effect.Equality (class EffectRowEquals, effTo)
42+
import Type.Row.Effect.Equality (class EffectRowEquals)
4343
import Unsafe.Coerce (unsafeCoerce)
4444

4545
foreign import data Aff ∷ # Effect Type Type
@@ -100,7 +100,10 @@ instance monadErrorAff ∷ MonadError Error (Aff eff) where
100100
Right r → pure r
101101

102102
instance monadEffAffEffectRowEquals eff1 (exceptionEXCEPTION, asyncASYNC | eff2) MonadEff eff1 (Aff eff2) where
103-
liftEff eff = Fn.runFn3 _liftEff Left Right (effTo eff)
103+
liftEff eff = unsafeLiftEff (coerceEff eff)
104+
where
105+
coerceEff Eff eff1 ~> Eff eff2
106+
coerceEff = unsafeCoerce
104107

105108
newtype ParAff eff a = ParAff (Aff eff a)
106109

@@ -116,7 +119,7 @@ instance applyParAff ∷ Apply (ParAff eff) where
116119
Thread t3 ← unsafeLaunchAff do
117120
f ← attempt t1.join
118121
a ← attempt t2.join
119-
unsafeLiftEff (Right <$> k (f <*> a))
122+
unsafeLiftEff (k (f <*> a))
120123
pure $ Canceler \err →
121124
parSequence_
122125
[ t3.kill err
@@ -142,18 +145,15 @@ instance altParAff ∷ Alt (ParAff eff) where
142145
Thread t2 ← unsafeLaunchAff a2
143146

144147
let
145-
lift a. Eff eff a Aff eff a
146-
lift = unsafeLiftEff <<< map Right
147-
148148
earlyError =
149149
error "Alt ParAff: early exit"
150150

151151
runK t r = do
152-
res ← lift $ unsafeRunRef $ readRef ref
152+
res ← unsafeLiftEff $ unsafeRunRef $ readRef ref
153153
case res, r of
154-
Nothing, Left _ → lift $ unsafeRunRef $ writeRef ref (Just r)
155-
Nothing, Right _ → t.kill earlyError *> lift (k r)
156-
Just r', _ → t.kill earlyError *> lift (k r')
154+
Nothing, Left _ → unsafeLiftEff $ unsafeRunRef $ writeRef ref (Just r)
155+
Nothing, Right _ → t.kill earlyError *> unsafeLiftEff (k r)
156+
Just r', _ → t.kill earlyError *> unsafeLiftEff (k r')
157157

158158
Thread t3 ← unsafeLaunchAff $ runK t2 =<< attempt t1.join
159159
Thread t4 ← unsafeLaunchAff $ runK t1 =<< attempt t2.join
@@ -216,17 +216,9 @@ foreign import _bind ∷ ∀ eff a b. Aff eff a → (a → Aff eff b) → Aff ef
216216
foreign import _delay a eff. Fn.Fn2 (Unit Either a Unit) Number (Aff eff Unit)
217217
foreign import attempt eff a. Aff eff a Aff eff (Either Error a)
218218
foreign import bracket eff a b. Aff eff a (a Aff eff Unit) (a Aff eff b) Aff eff b
219-
foreign import unsafeLiftEff eff a. Eff eff (Either Error a) Aff eff a
219+
foreign import unsafeLiftEff eff a. Eff eff a Aff eff a
220220
foreign import unsafeMakeAff eff a. ((Either Error a Eff eff Unit) Eff eff (Canceler eff)) Aff eff a
221221

222-
foreign import _liftEff
223-
eff a
224-
. Fn.Fn3
225-
(Error Either Error a)
226-
(a Either Error a)
227-
(Eff (AffModality eff) a)
228-
(Aff eff a)
229-
230222
foreign import _makeAff
231223
eff a
232224
. Fn.Fn3

0 commit comments

Comments
 (0)