/
InMemory.purs
43 lines (36 loc) · 1.55 KB
/
InMemory.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Hyper.Node.Session.InMemory where
import Prelude
import Data.Map as Map
import Control.Monad.Aff (Aff)
import Control.Monad.Aff.AVar (AVAR, AVar, makeVar', modifyVar, peekVar)
import Control.Monad.Aff.Class (class MonadAff, liftAff)
import Control.Monad.Aff.Console (CONSOLE, log)
import Data.Map (Map)
import Data.Newtype (unwrap)
import Hyper.Session (class SessionStore, SessionID(..))
data InMemorySessionStore session = InMemorySessionStore (AVar (Map SessionID session))
instance sessionStoreInMemorySessionStore :: ( Monad m
, MonadAff (avar :: AVAR, console :: CONSOLE | e) m
)
=> SessionStore
(InMemorySessionStore session)
m
session where
newSessionID _ =
pure (SessionID "new-id")
get (InMemorySessionStore var) id =
liftAff do
log ("Looking up session: " <> show (unwrap id))
Map.lookup id <$> peekVar var
put (InMemorySessionStore var) id session = do
liftAff do
log ("Saving session: " <> unwrap id)
modifyVar (Map.insert id session) var
delete (InMemorySessionStore var) id = do
liftAff do
log ("Deleting session: " <> unwrap id)
modifyVar (Map.delete id) var
newInMemorySessionStore
:: forall e session
. Aff ( avar ∷ AVAR | e ) (InMemorySessionStore session)
newInMemorySessionStore = InMemorySessionStore <$> makeVar' Map.empty