@@ -27,6 +27,7 @@ module Control.Monad.Aff
27
27
, BracketConditions
28
28
, generalBracket
29
29
, nonCanceler
30
+ , effCanceler
30
31
, module Exports
31
32
) where
32
33
@@ -154,6 +155,7 @@ newtype Fiber eff a = Fiber
154
155
, kill ∷ Fn.Fn2 Error (Either Error Unit → Eff eff Unit ) (Eff eff (Eff eff Unit ))
155
156
, join ∷ (Either Error a → Eff eff Unit ) → Eff eff (Eff eff Unit )
156
157
, onComplete ∷ OnComplete eff a → Eff eff (Eff eff Unit )
158
+ , isSuspended ∷ Eff eff Boolean
157
159
}
158
160
159
161
instance functorFiber ∷ Functor (Fiber eff ) where
@@ -168,12 +170,14 @@ instance applicativeFiber ∷ Applicative (Fiber eff) where
168
170
-- | Invokes pending cancelers in a fiber and runs cleanup effects. Blocks
169
171
-- | until the fiber has fully exited.
170
172
killFiber ∷ ∀ 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
172
176
173
177
-- | Blocks until the fiber completes, yielding the result. If the fiber
174
178
-- | throws an exception, it is rethrown in the current fiber.
175
179
joinFiber ∷ ∀ 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
177
181
178
182
-- | A cancellation effect for actions run via `makeAff`. If a `Fiber` is
179
183
-- | killed, and an async action is pending, the canceler will be called to
@@ -194,6 +198,10 @@ instance monoidCanceler ∷ Monoid (Canceler eff) where
194
198
nonCanceler ∷ ∀ eff . Canceler eff
195
199
nonCanceler = Canceler (const (pure unit))
196
200
201
+ -- | A canceler from an Eff action.
202
+ effCanceler ∷ ∀ eff . Eff eff Unit → Canceler eff
203
+ effCanceler = Canceler <<< const <<< liftEff
204
+
197
205
-- | Forks an `Aff` from an `Eff` context, returning the `Fiber`.
198
206
launchAff ∷ ∀ eff a . Aff eff a → Eff eff (Fiber eff a )
199
207
launchAff aff = do
0 commit comments