Permalink
Browse files

Add bracketHandler

  • Loading branch information...
lpsmith committed Feb 15, 2012
1 parent 75c21c7 commit df1b7248af32d75a48745fdb819502a3911438f6
Showing with 25 additions and 0 deletions.
  1. +1 −0 src/Snap/Snaplet.hs
  2. +24 −0 src/Snap/Snaplet/Internal/Types.hs
View
@@ -100,6 +100,7 @@ module Snap.Snaplet
-- * Handlers
, Handler
, reloadSite
+ , bracketHandler
-- * Serving Applications
, runSnaplet
@@ -294,6 +294,30 @@ reloadSite = failIfNotLocal $ do
else m
+------------------------------------------------------------------------------
+-- | This function brackets a Handler action in resource acquisition and
+-- release. Like 'bracketSnap', this is provided because MonadCatchIO's
+-- 'bracket' function doesn't work properly in the case of a short-circuit
+-- return from the action being bracketed.
+--
+-- In order to prevent confusion regarding the effects of the
+-- aquisition and release actions on the Handler state, this function
+-- doesn't accept Handler actions for the acquire or release actions.
+--
+-- This function will run the release action in all cases where the
+-- acquire action succeeded. This includes the following behaviors
+-- from the bracketed Snap action.
+--
+-- 1. Normal completion
+--
+-- 2. Short-circuit completion, either from calling 'fail' or 'finishWith'
+--
+-- 3. An exception being thrown.
+bracketHandler :: IO a -> (a -> IO x) -> (a -> Handler b v c) -> Handler b v c
+bracketHandler begin end f = Handler . L.Lensed $ \l v b -> do
+ bracketSnap begin end $ \a -> case f a of Handler m -> L.unlensed m l v b
+
+
------------------------------------------------------------------------------
-- | Information about a partially constructed initializer. Used to
-- automatically aggregate handlers and cleanup actions.

0 comments on commit df1b724

Please sign in to comment.