/
NowHs.hs
61 lines (44 loc) · 1.61 KB
/
NowHs.hs
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
50
51
52
53
54
55
56
57
58
59
60
61
{-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances, MultiParamTypeClasses, DeriveDataTypeable #-}
module NowHs where
import Data.Typeable
import Control.Monad.State
import Control.Monad.Error
import Control.Monad.Reader
import Control.Exception
import Control.Concurrent
import Control.Concurrent.STM
import qualified Network.WebSockets as WS
data NowHsError
= NoSuchFunction String
| IncorrectNumArgs
| JSONParseError String
deriving (Show, Typeable)
instance Error NowHsError
instance Exception NowHsError
newtype NowHsT s m a = NowHs { unNowHs :: ReaderT (TVar s) (ErrorT NowHsError m) a }
deriving (Functor, Monad, MonadIO,
MonadError NowHsError)
instance MonadTrans (NowHsT s) where
lift = NowHs . lift . lift
mapNowHsT :: (m (Either NowHsError a) -> n (Either NowHsError b)) -> NowHsT s m a -> NowHsT s n b
mapNowHsT f = NowHs . mapReaderT (mapErrorT f) . unNowHs
forkNowHs :: MonadIO m => NowHs s () -> NowHsT s m ThreadId
forkNowHs now= do
let mapping io = do
liftIO $ fmap Right $ forkIO (fmap (either throw id) io)
tid <- mapNowHsT mapping now
return tid
type Prot = WS.Hybi00
type NowHs s = NowHsT s IO
type NowHsWs s = NowHsT s (WS.WebSockets Prot)
instance (MonadIO m) => MonadState s (NowHsT s m) where
put aasd = do
tv <- NowHs ask
liftIO . atomically $ writeTVar tv aasd
get = do
tv <- NowHs ask
liftIO . atomically $ readTVar tv
--class (Monad m) => MonadNowHs m where
-- liftNowHs :: NowHs a -> m a
--instance MonadNowHs (NowHsT (WS.WebSockets Prot)) where
-- liftNowHs = id