@@ -27,6 +27,7 @@ module Control.Monad.Aff
2727 , BracketConditions
2828 , generalBracket
2929 , nonCanceler
30+ , effCanceler
3031 , module Exports
3132 ) where
3233
@@ -154,6 +155,7 @@ newtype Fiber eff a = Fiber
154155 , kill ∷ Fn.Fn2 Error (Either Error Unit → Eff eff Unit ) (Eff eff (Eff eff Unit ))
155156 , join ∷ (Either Error a → Eff eff Unit ) → Eff eff (Eff eff Unit )
156157 , onComplete ∷ OnComplete eff a → Eff eff (Eff eff Unit )
158+ , isSuspended ∷ Eff eff Boolean
157159 }
158160
159161instance functorFiber ∷ Functor (Fiber eff ) where
@@ -168,12 +170,14 @@ instance applicativeFiber ∷ Applicative (Fiber eff) where
168170-- | Invokes pending cancelers in a fiber and runs cleanup effects. Blocks
169171-- | until the fiber has fully exited.
170172killFiber ∷ ∀ eff a . Error → Fiber eff a → Aff eff Unit
171- killFiber e (Fiber t) = makeAff \k → Canceler <<< const <<< liftEff <$> Fn .runFn2 t.kill e k
173+ killFiber e (Fiber t) = liftEff t.isSuspended >>= if _
174+ then liftEff $ void $ Fn .runFn2 t.kill e (const (pure unit))
175+ else makeAff \k → effCanceler <$> Fn .runFn2 t.kill e k
172176
173177-- | Blocks until the fiber completes, yielding the result. If the fiber
174178-- | throws an exception, it is rethrown in the current fiber.
175179joinFiber ∷ ∀ eff a . Fiber eff a → Aff eff a
176- joinFiber (Fiber t) = makeAff \k → Canceler <<< const <<< liftEff <$> t.join k
180+ joinFiber (Fiber t) = makeAff \k → effCanceler <$> t.join k
177181
178182-- | A cancellation effect for actions run via `makeAff`. If a `Fiber` is
179183-- | killed, and an async action is pending, the canceler will be called to
@@ -194,6 +198,10 @@ instance monoidCanceler ∷ Monoid (Canceler eff) where
194198nonCanceler ∷ ∀ eff . Canceler eff
195199nonCanceler = Canceler (const (pure unit))
196200
201+ -- | A canceler from an Eff action.
202+ effCanceler ∷ ∀ eff . Eff eff Unit → Canceler eff
203+ effCanceler = Canceler <<< const <<< liftEff
204+
197205-- | Forks an `Aff` from an `Eff` context, returning the `Fiber`.
198206launchAff ∷ ∀ eff a . Aff eff a → Eff eff (Fiber eff a )
199207launchAff aff = do
0 commit comments