Safe Haskell | Trustworthy |
---|
Control.Eff.Resource
Description
Allocate resources which are guaranteed to be released.
For more information, see the resourcet
package.
- type Resource m = State (ResourceState m)
- data ResourceState m
- data ReleaseKey
- runResource :: (Typeable1 m, Monad m, SetMember Lift (Lift m) r) => Eff (Resource (m ()) :> r) a -> Eff r a
- allocate :: (Typeable1 m, Monad m, Member (Resource (m ())) r, SetMember Lift (Lift m) r) => m a -> (a -> m ()) -> Eff r (ReleaseKey, a)
- register :: (Typeable1 m, Member (Resource (m ())) r) => m () -> Eff r ReleaseKey
- release :: (Typeable1 m, SetMember Lift (Lift m) r, Member (Resource (m ())) r) => ReleaseKey -> Eff r ()
- unprotect :: (Typeable1 m, Member (Resource (m ())) r) => ReleaseKey -> Eff r (Maybe (m ()))
Documentation
type Resource m = State (ResourceState m)Source
The Resource effect. This effect keeps track of all registered actions,
and calls them upon exit (via runResource
). Actions may be registered
via register, or resources may be allocated atomically via allocate.
allocate corresponds closely to bracket.
Releasing may be performed before exit via the release function. This is a highly recommended optimization, as it will ensure that scarce resources are freed early. Note that calling release will deregister the action, so that a release action will only ever be called once.
data ResourceState m Source
A resource's state. Type parameter m
is the Monad the resource
deallocation will run in.
Instances
data ReleaseKey Source
A lookup key for a specific release action. This value
is returned by register
and allocate
, and is passed to release
.
Instances
runResource :: (Typeable1 m, Monad m, SetMember Lift (Lift m) r) => Eff (Resource (m ()) :> r) a -> Eff r aSource
Unwrap a Resource
effect, and call all registered release actions.
Arguments
:: (Typeable1 m, Monad m, Member (Resource (m ())) r, SetMember Lift (Lift m) r) | |
=> m a | allocate |
-> (a -> m ()) | free resource |
-> Eff r (ReleaseKey, a) |
Perform some allocation, and automatically register a cleanup action.
register :: (Typeable1 m, Member (Resource (m ())) r) => m () -> Eff r ReleaseKeySource
Register some action that will be called precisely once, either when
runResource
is called or when the ReleaseKey
is passed to release
.
release :: (Typeable1 m, SetMember Lift (Lift m) r, Member (Resource (m ())) r) => ReleaseKey -> Eff r ()Source
Call a release action early, and deregister it from the list of cleanup actions to be performed.
unprotect :: (Typeable1 m, Member (Resource (m ())) r) => ReleaseKey -> Eff r (Maybe (m ()))Source
Unprotect resource from cleanup actions, this allowes you to send resource into another resourcet process and reregister it there.
It returns an release action that should be run in order to clean resource or Nothing in case if resource is already freed.