@@ -3,7 +3,6 @@ module Control.Monad.Aff
33 , Fiber
44 , ParAff (..)
55 , Canceler (..)
6- , BracketConditions
76 , makeAff
87 , launchAff
98 , launchSuspendedAff
@@ -14,14 +13,18 @@ module Control.Monad.Aff
1413 , spawnAff
1514 , spawnSuspendedAff
1615 , liftEff'
17- , bracket
18- , generalBracket
16+ , attempt
1917 , delay
2018 , never
2119 , finally
2220 , atomically
2321 , killFiber
2422 , joinFiber
23+ , cancelWith
24+ , bracket
25+ , BracketConditions
26+ , generalBracket
27+ , nonCanceler
2528 , module Exports
2629 ) where
2730
@@ -182,7 +185,11 @@ instance semigroupCanceler ∷ Semigroup (Canceler eff) where
182185
183186-- | A no-op `Canceler` can be constructed with `mempty`.
184187instance monoidCanceler ∷ Monoid (Canceler eff ) where
185- mempty = Canceler (const (pure unit))
188+ mempty = nonCanceler
189+
190+ -- | A canceler which does not cancel anything.
191+ nonCanceler ∷ ∀ eff . Canceler eff
192+ nonCanceler = Canceler (const (pure unit))
186193
187194-- | Forks an `Aff` from an `Eff` context, returning the `Fiber`.
188195launchAff ∷ ∀ eff a . Aff eff a → Eff eff (Fiber eff a )
@@ -238,6 +245,15 @@ never = makeAff \_ → pure mempty
238245liftEff' ∷ ∀ eff a . Eff (exception ∷ EXCEPTION | eff ) a → Aff eff a
239246liftEff' = liftEff <<< unsafeCoerceEff
240247
248+ -- | A monomorphic version of `try`. Catches thrown errors and lifts them
249+ -- | into an `Either`.
250+ attempt ∷ ∀ eff a . Aff eff a → Aff eff (Either Error a )
251+ attempt = try
252+
253+ -- | Ignores any errors.
254+ apathize ∷ ∀ eff a . Aff eff a → Aff eff Unit
255+ apathize = attempt >>> map (const unit)
256+
241257-- | Runs the first effect after the second, regardless of whether it completed
242258-- | successfully or the fiber was cancelled.
243259finally ∷ ∀ eff a . Aff eff Unit → Aff eff a → Aff eff a
@@ -247,6 +263,17 @@ finally fin a = bracket (pure unit) (const fin) (const a)
247263atomically ∷ ∀ eff a . Aff eff a → Aff eff a
248264atomically a = bracket a (const (pure unit)) pure
249265
266+ -- | Attaches a custom `Canceler` to an action. If the computation is canceled,
267+ -- | then the custom `Canceler` will be run afterwards.
268+ cancelWith ∷ ∀ eff a . Aff eff a → Canceler eff → Aff eff a
269+ cancelWith aff (Canceler cancel) =
270+ generalBracket (pure unit)
271+ { killed: \e _ → cancel e
272+ , failed: const pure
273+ , completed: const pure
274+ }
275+ (const aff)
276+
250277-- | Guarantees resource acquisition and cleanup. The first effect may acquire
251278-- | some resource, while the second will dispose of it. The third effect makes
252279-- | use of the resource. Disposal is always run last, regardless. Neither
0 commit comments