resource-effect-0.1.1: A port of the package 'resourcet' for extensible effects.

Safe HaskellTrustworthy

Control.Eff.Resource

Description

Allocate resources which are guaranteed to be released.

For more information, see the resourcet package.

Synopsis

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.

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.

allocateSource

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.