@@ -24,6 +24,7 @@ import Prelude
2424
2525import Control.Alt (class Alt )
2626import Control.Alternative (class Alternative )
27+ import Control.Monad.Aff.Internal (AVBox , AVar , _killVar , _putVar , _takeVar , _makeVar )
2728import Control.Monad.Cont.Class (class MonadCont )
2829import Control.Monad.Eff (Eff )
2930import Control.Monad.Eff.Class (class MonadEff )
@@ -39,6 +40,8 @@ import Data.Foldable (class Foldable, foldl)
3940import Data.Function.Uncurried (Fn2 , Fn3 , runFn2 , runFn3 )
4041import Data.Monoid (class Monoid , mempty )
4142
43+ import Unsafe.Coerce (unsafeCoerce )
44+
4245-- | An asynchronous computation with effects `e`. The computation either
4346-- | errors or produces a value of type `a`.
4447-- |
@@ -57,7 +60,7 @@ type PureAff a = forall e. Aff e a
5760newtype Canceler e = Canceler (Error -> Aff e Boolean )
5861
5962-- | Unwraps the canceler function from the newtype that wraps it.
60- cancel :: forall e . Canceler e -> Error -> Aff e Boolean
63+ cancel :: forall e . Canceler e -> Error -> Aff e Boolean
6164cancel (Canceler f) = f
6265
6366-- | This function allows you to attach a custom canceler to an asynchronous
@@ -207,8 +210,8 @@ instance monoidCanceler :: Monoid (Canceler e) where
207210
208211instance monadParAff :: MonadPar (Aff e ) where
209212 par f ma mb = do
210- va <- _makeVar nonCanceler
211- vb <- _makeVar nonCanceler
213+ va <- makeVar
214+ vb <- makeVar
212215 c1 <- forkAff (putOrKill va =<< attempt ma)
213216 c2 <- forkAff (putOrKill vb =<< attempt mb)
214217 f <$> (takeVar va) <*> (takeVar vb)
@@ -219,8 +222,8 @@ instance monadParAff :: MonadPar (Aff e) where
219222instance monadRaceAff :: MonadRace (Aff e ) where
220223 stall = throwError $ error " Stalled"
221224 race a1 a2 = do
222- va <- _makeVar nonCanceler -- the `a` value
223- ve <- _makeVar nonCanceler -- the error count (starts at 0)
225+ va <- makeVar -- the `a` value
226+ ve <- makeVar -- the error count (starts at 0)
224227 putVar ve 0
225228 c1 <- forkAff $ either (maybeKill va ve) (putVar va) =<< attempt a1
226229 c2 <- forkAff $ either (maybeKill va ve) (putVar va) =<< attempt a2
@@ -232,28 +235,20 @@ instance monadRaceAff :: MonadRace (Aff e) where
232235 if e == 1 then killVar va err else pure unit
233236 putVar ve (e + 1 )
234237
235- -- ------------------------------
236-
237- foreign import data AVar :: * -> *
238+ makeVar :: forall e a . Aff e (AVar a )
239+ makeVar = fromAVBox $ _makeVar nonCanceler
238240
239241takeVar :: forall e a . AVar a -> Aff e a
240- takeVar q = runFn2 _takeVar nonCanceler q
242+ takeVar q = fromAVBox $ runFn2 _takeVar nonCanceler q
241243
242244putVar :: forall e a . AVar a -> a -> Aff e Unit
243- putVar q a = runFn3 _putVar nonCanceler q a
245+ putVar q a = fromAVBox $ runFn3 _putVar nonCanceler q a
244246
245247killVar :: forall e a . AVar a -> Error -> Aff e Unit
246- killVar q e = runFn3 _killVar nonCanceler q e
247-
248- foreign import _makeVar :: forall e a . Canceler e -> Aff e (AVar a )
249-
250- foreign import _takeVar :: forall e a . Fn2 (Canceler e ) (AVar a ) (Aff e a )
251-
252- foreign import _putVar :: forall e a . Fn3 (Canceler e ) (AVar a ) a (Aff e Unit )
253-
254- foreign import _killVar :: forall e a . Fn3 (Canceler e ) (AVar a ) Error (Aff e Unit )
248+ killVar q e = fromAVBox $ runFn3 _killVar nonCanceler q e
255249
256- -- ------------------------------
250+ fromAVBox :: forall a e . AVBox a -> Aff e a
251+ fromAVBox = unsafeCoerce
257252
258253foreign import _cancelWith :: forall e a . Fn3 (Canceler e ) (Aff e a ) (Canceler e ) (Aff e a )
259254
0 commit comments