Skip to content

Commit abba73f

Browse files
jimmyhucoowickstrom
authored andcommitted
replace AVar with Ref and fix session id bug
1 parent 5b7d7fc commit abba73f

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

bower.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
"purescript-http-methods": "^3.0.0",
3333
"purescript-indexed-monad": "^0.3.0",
3434
"purescript-smolder": "^7.0.0",
35-
"purescript-aff": "^4.0.0"
35+
"purescript-aff": "^4.0.0",
36+
"purescript-random": "^3.0.0",
37+
"purescript-refs": "^3.0.0"
3638
},
3739
"devDependencies": {
3840
"purescript-psci-support": "^3.0.0",

examples/Sessions.purs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ module Examples.Sessions where
33
import Prelude
44
import Control.IxMonad ((:*>), (:>>=))
55
import Control.Monad.Aff (launchAff)
6-
import Control.Monad.Aff.AVar (AVAR)
76
import Control.Monad.Aff.Console (log)
87
import Control.Monad.Eff (Eff)
98
import Control.Monad.Eff.Class (liftEff)
109
import Control.Monad.Eff.Console (CONSOLE)
1110
import Control.Monad.Eff.Exception (EXCEPTION)
11+
import Control.Monad.Eff.Random (RANDOM)
12+
import Control.Monad.Eff.Ref (REF)
1213
import Data.Maybe (Maybe(..))
1314
import Data.MediaType.Common (textHTML)
1415
import Hyper.Cookies (cookies)
@@ -23,9 +24,9 @@ import Node.HTTP (HTTP)
2324

2425
newtype MySession = MySession { userId :: Int }
2526

26-
main :: forall e. Eff (exception :: EXCEPTION, avar :: AVAR, console :: CONSOLE, http :: HTTP | e) Unit
27+
main :: forall e. Eff (exception :: EXCEPTION, ref :: REF, console :: CONSOLE, http :: HTTP, random ::RANDOM | e) Unit
2728
main = void $ launchAff do
28-
store <- newInMemorySessionStore
29+
store <- liftEff newInMemorySessionStore
2930
liftEff (runServer defaultOptionsWithLogging (components store) app)
3031
where
3132
components store =

src/Hyper/Node/Session/InMemory.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
exports.generatedSessionID = function() {
4+
return String((new Date()).getTime() + Math.random());
5+
};

src/Hyper/Node/Session/InMemory.purs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,47 @@ module Hyper.Node.Session.InMemory where
22

33
import Prelude
44

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)
910
import Data.Map (Map)
1011
import Data.Map as Map
1112
import Data.Newtype (unwrap)
1213
import Hyper.Session (class SessionStore, SessionID(..))
1314

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
1518

1619
instance sessionStoreInMemorySessionStore :: ( Monad m
17-
, MonadAff (avar :: AVAR, console :: CONSOLE | e) m
20+
, MonadEff (ref:: REF, console :: CONSOLE, random :: RANDOM | e) m
1821
)
1922
=> SessionStore
2023
(InMemorySessionStore session)
2124
m
2225
session where
23-
newSessionID _ =
24-
pure (SessionID "new-id")
26+
newSessionID _ = do
27+
id <- liftEff generatedSessionID
28+
pure (SessionID id)
2529

2630
get (InMemorySessionStore var) id =
27-
liftAff do
31+
liftEff do
2832
log ("Looking up session: " <> show (unwrap id))
29-
Map.lookup id <$> readVar var
33+
Map.lookup id <$> readRef var
3034

3135
put (InMemorySessionStore var) id session = do
32-
liftAff do
36+
liftEff do
3337
log ("Saving session: " <> unwrap id)
34-
Map.insert id session <$> takeVar var >>= flip putVar var
38+
modifyRef var $ Map.insert id session
3539

3640
delete (InMemorySessionStore var) id = do
37-
liftAff do
41+
liftEff do
3842
log ("Deleting session: " <> unwrap id)
39-
Map.delete id <$> takeVar var >>= flip putVar var
43+
modifyRef var $ Map.delete id
4044

4145
newInMemorySessionStore
4246
:: 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

Comments
 (0)