-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathInMemory.purs
More file actions
49 lines (40 loc) · 1.73 KB
/
InMemory.purs
File metadata and controls
49 lines (40 loc) · 1.73 KB
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
44
45
46
47
48
49
module Hyper.Node.Session.InMemory where
import Prelude
import Control.Monad.Eff.Class (class MonadEff, liftEff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.Eff.Random (RANDOM)
import Control.Monad.Eff.Ref (REF, Ref, modifyRef, newRef, readRef)
import Data.Map (Map)
import Data.Map as Map
import Data.Newtype (unwrap)
import Hyper.Session (class SessionStore, SessionID(..))
data InMemorySessionStore session = InMemorySessionStore (Ref (Map SessionID session))
foreign import generatedSessionID ::forall eff. Eff (random :: RANDOM | eff) String
instance sessionStoreInMemorySessionStore :: ( Monad m
, MonadEff (ref:: REF, console :: CONSOLE, random :: RANDOM | e) m
)
=> SessionStore
(InMemorySessionStore session)
m
session where
newSessionID _ = do
id <- liftEff generatedSessionID
pure (SessionID id)
get (InMemorySessionStore var) id =
liftEff do
log ("Looking up session: " <> show (unwrap id))
Map.lookup id <$> readRef var
put (InMemorySessionStore var) id session = do
liftEff do
log ("Saving session: " <> unwrap id)
modifyRef var $ Map.insert id session
pure id
delete (InMemorySessionStore var) id = do
liftEff do
log ("Deleting session: " <> unwrap id)
modifyRef var $ Map.delete id
newInMemorySessionStore
:: forall e session
. Eff ( ref∷ REF | e ) (InMemorySessionStore session)
newInMemorySessionStore = InMemorySessionStore <$> newRef Map.empty