on master help sign in
Add low level InternalState API to Effectful.Resource
commit30a28558429bbb20660c17a36d5680afe49f2300 authorAndrzej Rybczak <andrzej@rybczak.net> dateMon 26 Jul 2021, 20:15 (5 years ago) parent50cdcbf
1 file changed+25
effectful/src/Effectful/Resource.hs+25
effectful/src/Effectful/Resource.hs+25
@@ -11,6 +11,13 @@ module Effectful.Resource
1111 , R.register
1212 , R.release
1313 , R.unprotect
14+
15+ -- * Internal state
16+ , R.InternalState
17+ , getInternalState
18+ , runInternalState
19+ , R.createInternalState
20+ , R.closeInternalState
1421 ) where
1522
1623import Control.Exception
@@ -40,6 +47,24 @@ runResource m = unsafeEff $ \es0 -> do
4047 RI.stateCleanupChecked Nothing istate
4148 pure a
4249
50+----------------------------------------
51+-- Internal state
52+
53+-- | Get the 'R.InternalState' of the current 'Resource' effect.
54+getInternalState :: Resource :> es => Eff es R.InternalState
55+getInternalState = do
56+ IdE (Resource istate) <- getEffect
57+ pure istate
58+
59+-- | Run the 'Resource' effect with existing 'R.InternalState'.
60+--
61+-- /Note:/ the 'R.InternalState' will not be closed at the end.
62+runInternalState :: R.InternalState -> Eff (Resource : es) a -> Eff es a
63+runInternalState istate = evalEffect $ IdE (Resource istate)
64+
65+----------------------------------------
66+-- Orphan instance
67+
4368instance (IOE :> es, Resource :> es) => R.MonadResource (Eff es) where
4469 liftResourceT (RI.ResourceT m) = unsafeEff $ \es -> do
4570 getEnv es >>= \(IdE (Resource istate)) -> m istate