Skip to content
This repository has been archived by the owner on Jan 17, 2020. It is now read-only.

Commit

Permalink
use supervise
Browse files Browse the repository at this point in the history
  • Loading branch information
kritzcreek committed Jan 24, 2018
1 parent 7fb93e1 commit 8d2e0b6
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/Control/Monad/Aff/Reattempt.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ module Control.Monad.Aff.Reattempt where

import Prelude

import Control.Monad.Aff (Aff, delay, forkAff, killFiber)
import Control.Monad.Aff (Aff, delay, forkAff, supervise)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Exception (Error, error)
import Control.Monad.Eff.Ref (newRef, readRef, writeRef, REF)
import Control.Monad.Error.Class (catchError, throwError)
import Data.Time.Duration (Milliseconds)
Expand All @@ -20,17 +19,14 @@ import Data.Time.Duration (Milliseconds)
-- | will succeed. When no attempts succeed the `Aff` returned by `reattempt` will fail
-- | with the `Error` raised by the last attempt.
reattempt e a. Milliseconds Aff (ref REF | e) a Aff (ref REF | e) a
reattempt ms aff = do
reattempt ms aff = supervise do
elapsed ← liftEff $ newRef false
forkedTimeout ← forkAff do
_ ← forkAff do
delay ms
liftEff $ writeRef elapsed true
let attempt = aff `catchError` \error → do
shouldRethrow ← liftEff $ readRef elapsed
if shouldRethrow
then throwError (error Error)
then throwError error
else attempt
result ← attempt
-- Process continues after returned aff succeeds if forked timeout isn't cancelled
_ ← killFiber (error "") forkedTimeout
pure result
attempt

0 comments on commit 8d2e0b6

Please sign in to comment.