@@ -2,43 +2,47 @@ module Hyper.Node.Session.InMemory where
2
2
3
3
import Prelude
4
4
5
- import Control.Monad.Aff (Aff )
6
- import Control.Monad.Aff.AVar (AVAR , AVar , makeVar , putVar , readVar , takeVar )
7
- import Control.Monad.Aff.Class (class MonadAff , liftAff )
8
- import Control.Monad.Aff.Console (CONSOLE , log )
5
+ import Control.Monad.Eff.Class (class MonadEff , liftEff )
6
+ import Control.Monad.Eff (Eff )
7
+ import Control.Monad.Eff.Console (CONSOLE , log )
8
+ import Control.Monad.Eff.Random (RANDOM )
9
+ import Control.Monad.Eff.Ref (REF , Ref , modifyRef , newRef , readRef )
9
10
import Data.Map (Map )
10
11
import Data.Map as Map
11
12
import Data.Newtype (unwrap )
12
13
import Hyper.Session (class SessionStore , SessionID (..))
13
14
14
- data InMemorySessionStore session = InMemorySessionStore (AVar (Map SessionID session ))
15
+ data InMemorySessionStore session = InMemorySessionStore (Ref (Map SessionID session ))
16
+
17
+ foreign import generatedSessionID :: forall eff . Eff (random :: RANDOM | eff ) String
15
18
16
19
instance sessionStoreInMemorySessionStore :: ( Monad m
17
- , MonadAff ( avar :: AVAR , console :: CONSOLE | e ) m
20
+ , MonadEff ( ref :: REF , console :: CONSOLE , random :: RANDOM | e ) m
18
21
)
19
22
=> SessionStore
20
23
(InMemorySessionStore session )
21
24
m
22
25
session where
23
- newSessionID _ =
24
- pure (SessionID " new-id" )
26
+ newSessionID _ = do
27
+ id <- liftEff generatedSessionID
28
+ pure (SessionID id)
25
29
26
30
get (InMemorySessionStore var) id =
27
- liftAff do
31
+ liftEff do
28
32
log (" Looking up session: " <> show (unwrap id))
29
- Map .lookup id <$> readVar var
33
+ Map .lookup id <$> readRef var
30
34
31
35
put (InMemorySessionStore var) id session = do
32
- liftAff do
36
+ liftEff do
33
37
log (" Saving session: " <> unwrap id)
34
- Map .insert id session <$> takeVar var >>= flip putVar var
38
+ modifyRef var $ Map .insert id session
35
39
36
40
delete (InMemorySessionStore var) id = do
37
- liftAff do
41
+ liftEff do
38
42
log (" Deleting session: " <> unwrap id)
39
- Map .delete id <$> takeVar var >>= flip putVar var
43
+ modifyRef var $ Map .delete id
40
44
41
45
newInMemorySessionStore
42
46
:: forall e session
43
- . Aff ( avar ∷ AVAR | e ) (InMemorySessionStore session )
44
- newInMemorySessionStore = InMemorySessionStore <$> makeVar Map .empty
47
+ . Eff ( ref ∷ REF | e ) (InMemorySessionStore session )
48
+ newInMemorySessionStore = InMemorySessionStore <$> newRef Map .empty
0 commit comments