Skip to content

Commit

Permalink
Add unprotectEff (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
arybczak authored Nov 5, 2023
1 parent a780b4b commit 863dd51
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# resourcet-effectful-1.0.1.0 (2023-??-??)
* Add `allocateEff`, `allocateEff_`, `registerEff` and `releaseEff`.
* Add `allocateEff`, `allocateEff_`, `registerEff`, `releaseEff` and
`unprotectEff'.
* Re-export `ReleaseKey` and `ResourceCleanupException`.

# resourcet-effectful-1.0.0.0 (2022-07-14)
Expand Down
18 changes: 18 additions & 0 deletions src/Effectful/Resource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module Effectful.Resource
, R.register
, R.release
, R.unprotect
, ReleaseAction(..)
, unprotectEff

-- * Internal state
, R.InternalState
Expand Down Expand Up @@ -109,6 +111,22 @@ registerEff release = do
releaseEff :: Resource :> es => R.ReleaseKey -> Eff es ()
releaseEff = unsafeEff_ . R.release

-- | Action for releasing a resource.
newtype ReleaseAction = ReleaseAction
{ runReleaseAction :: forall es. Resource :> es => Eff es ()
}

-- | A variant of 'R.unprotect' adjusted to work in the 'Eff' monad.
--
-- /Note:/ if the resource was acquired using 'allocateEff', 'allocateEff_' or
-- 'registerEff' then the returned 'ReleaseAction' will run in a clone of the
-- environment it was registered in.
--
-- See the documentation of the aforementioned functions for more information.
unprotectEff :: Resource :> es => R.ReleaseKey -> Eff es (Maybe ReleaseAction)
unprotectEff key = unsafeEff_ $ do
fmap (\m -> ReleaseAction $ unsafeEff_ m) <$> R.unprotect key

----------------------------------------
-- Internal state

Expand Down

0 comments on commit 863dd51

Please sign in to comment.