Skip to content

Commit 637e913

Browse files
committed
Fill in missing functions
1 parent 93846c9 commit 637e913

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/Control/Monad/Aff.purs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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`.
184187
instance monoidCancelerMonoid (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`.
188195
launchAff eff a. Aff eff a Eff eff (Fiber eff a)
@@ -238,6 +245,15 @@ never = makeAff \_ → pure mempty
238245
liftEff' eff a. Eff (exception EXCEPTION | eff) a Aff eff a
239246
liftEff' = 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.
243259
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)
247263
atomically eff a. Aff eff a Aff eff a
248264
atomically 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

test/Test/Main.purs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import Data.Either (Either(..), isLeft, isRight)
2020
import Data.Foldable (sum)
2121
import Data.Maybe (Maybe(..))
2222
import Data.Monoid (mempty)
23-
import Data.Time (Millisecond)
2423
import Data.Time.Duration (Milliseconds(..))
2524
import Data.Traversable (traverse)
2625
import Test.Assert (assert', ASSERT)

0 commit comments

Comments
 (0)