@@ -3,7 +3,6 @@ module Control.Monad.Aff
3
3
, Fiber
4
4
, ParAff (..)
5
5
, Canceler (..)
6
- , BracketConditions
7
6
, makeAff
8
7
, launchAff
9
8
, launchSuspendedAff
@@ -14,14 +13,18 @@ module Control.Monad.Aff
14
13
, spawnAff
15
14
, spawnSuspendedAff
16
15
, liftEff'
17
- , bracket
18
- , generalBracket
16
+ , attempt
19
17
, delay
20
18
, never
21
19
, finally
22
20
, atomically
23
21
, killFiber
24
22
, joinFiber
23
+ , cancelWith
24
+ , bracket
25
+ , BracketConditions
26
+ , generalBracket
27
+ , nonCanceler
25
28
, module Exports
26
29
) where
27
30
@@ -182,7 +185,11 @@ instance semigroupCanceler ∷ Semigroup (Canceler eff) where
182
185
183
186
-- | A no-op `Canceler` can be constructed with `mempty`.
184
187
instance 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))
186
193
187
194
-- | Forks an `Aff` from an `Eff` context, returning the `Fiber`.
188
195
launchAff ∷ ∀ eff a . Aff eff a → Eff eff (Fiber eff a )
@@ -238,6 +245,15 @@ never = makeAff \_ → pure mempty
238
245
liftEff' ∷ ∀ eff a . Eff (exception ∷ EXCEPTION | eff ) a → Aff eff a
239
246
liftEff' = liftEff <<< unsafeCoerceEff
240
247
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
+
241
257
-- | Runs the first effect after the second, regardless of whether it completed
242
258
-- | successfully or the fiber was cancelled.
243
259
finally ∷ ∀ 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)
247
263
atomically ∷ ∀ eff a . Aff eff a → Aff eff a
248
264
atomically a = bracket a (const (pure unit)) pure
249
265
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
+
250
277
-- | Guarantees resource acquisition and cleanup. The first effect may acquire
251
278
-- | some resource, while the second will dispose of it. The third effect makes
252
279
-- | use of the resource. Disposal is always run last, regardless. Neither
0 commit comments