Skip to content

Commit

Permalink
add mapAllSessions
Browse files Browse the repository at this point in the history
  • Loading branch information
agrafix committed Aug 12, 2015
1 parent 69051cf commit 2989890
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Spock.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Spock
version: 0.8.0.0
version: 0.8.1.0
synopsis: Another Haskell web framework for rapid development
description: This toolbox provides everything you need to get a quick start into web hacking with haskell:
.
Expand Down Expand Up @@ -57,6 +57,7 @@ library
case-insensitive >=1.1,
containers >=0.5,
directory >=1.2,
focus >=0.1,
hashable >=1.2,
http-types >=0.8,
hvect >= 0.2,
Expand Down
10 changes: 10 additions & 0 deletions src/Web/Spock/Internal/SessionManager.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ createSessionManager cfg =
, sm_writeSession = writeSessionImpl vaultKey cacheHM
, sm_modifySession = modifySessionImpl vaultKey cacheHM
, sm_clearAllSessions = clearAllSessionsImpl cacheHM
, sm_mapSessions = mapAllSessionsImpl cacheHM
, sm_middleware = sessionMiddleware cfg vaultKey cacheHM
, sm_addSafeAction = addSafeActionImpl vaultKey cacheHM
, sm_lookupSafeAction = lookupSafeActionImpl vaultKey cacheHM
Expand Down Expand Up @@ -289,6 +290,15 @@ clearAllSessionsImpl :: SV.SessionVault (Session conn sess st)
clearAllSessionsImpl sessionRef =
liftIO $ atomically $ SV.filterSessions (const False) sessionRef

mapAllSessionsImpl ::
SV.SessionVault (Session conn sess st)
-> (sess -> STM sess)
-> SpockAction conn sess st ()
mapAllSessionsImpl sessionRef f =
liftIO $ atomically $ flip SV.mapSessions sessionRef $ \sess ->
do newData <- f (sess_data sess)
return $ sess { sess_data = newData }

housekeepSessions :: SessionCfg sess
-> SV.SessionVault (Session conn sess st)
-> (HM.HashMap SessionId (Session conn sess st) -> IO ())
Expand Down
8 changes: 8 additions & 0 deletions src/Web/Spock/Internal/SessionVault.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Control.Applicative
import Control.Concurrent.STM (STM)
import Control.Monad
import Data.Hashable
import Focus as F
import qualified ListT as L
import qualified STMContainers.Map as STMMap
import qualified Data.Text as T
Expand Down Expand Up @@ -55,3 +56,10 @@ filterSessions cond sv =
map getSessionKey $
filter (not . cond) allVals
forM_ deleteKeys $ flip deleteSession sv

-- | Perform action on all sessions
mapSessions :: IsSession s => (s -> STM s) -> SessionVault s -> STM ()
mapSessions f sv@(SessionVault smap) =
do allVals <- toList sv
forM_ allVals $ \sess ->
STMMap.focus (F.adjustM f) (getSessionKey sess) smap
2 changes: 2 additions & 0 deletions src/Web/Spock/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Web.Spock.Internal.Wire
#else
import Control.Applicative
#endif
import Control.Concurrent.STM
import Control.Monad.Base
import Control.Monad.Reader
import Control.Monad.Trans.Control
Expand Down Expand Up @@ -200,6 +201,7 @@ data SessionManager conn sess st
, sm_readSession :: SpockAction conn sess st sess
, sm_writeSession :: sess -> SpockAction conn sess st ()
, sm_modifySession :: forall a. (sess -> (sess, a)) -> SpockAction conn sess st a
, sm_mapSessions :: (sess -> STM sess) -> SpockAction conn sess st ()
, sm_clearAllSessions :: SpockAction conn sess st ()
, sm_middleware :: Middleware
, sm_addSafeAction :: PackedSafeAction conn sess st -> SpockAction conn sess st SafeActionHash
Expand Down
10 changes: 9 additions & 1 deletion src/Web/Spock/Shared.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module Web.Spock.Shared
, SessionPersistCfg(..), readShowSessionPersist
, SessionId
, getSessionId, readSession, writeSession
, modifySession, modifySession', modifyReadSession, clearAllSessions
, modifySession, modifySession', modifyReadSession, mapAllSessions, clearAllSessions
-- * Internals for extending Spock
, getSpockHeart, runSpockIO, WebStateM, WebState
)
Expand All @@ -45,6 +45,7 @@ import Web.Spock.Internal.SessionManager
import Web.Spock.Internal.Types
import Web.Spock.Internal.CoreAction
import Control.Monad
import Control.Concurrent.STM (STM)
import System.Directory
import qualified Web.Spock.Internal.Wire as W
import qualified Network.Wai as Wai
Expand Down Expand Up @@ -106,6 +107,13 @@ clearAllSessions =
do mgr <- getSessMgr
sm_clearAllSessions mgr

-- | Apply a transformation to all sessions. Be careful with this, as this
-- may cause many STM transaction retries.
mapAllSessions :: (sess -> STM sess) -> SpockAction conn sess st ()
mapAllSessions f =
do mgr <- getSessMgr
sm_mapSessions mgr f

-- | Simple session persisting configuration. DO NOT USE IN PRODUCTION
readShowSessionPersist :: (Read a, Show a) => FilePath -> SessionPersistCfg a
readShowSessionPersist fp =
Expand Down

0 comments on commit 2989890

Please # to comment.