forked from simonmar/haskell-eXchange-2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogDataSource2.hs
60 lines (44 loc) · 1.53 KB
/
LogDataSource2.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
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE
StandaloneDeriving, GADTs, TypeFamilies,
FlexibleInstances, MultiParamTypeClasses, GeneralizedNewtypeDeriving,
OverloadedStrings, DeriveDataTypeable
#-}
module LogDataSource2
( writeLog
, initDataSource
, LogRequest(..)
) where
import Data.Hashable
import Data.Typeable
import Haxl.Core
import qualified Data.Text as T
import qualified Data.Text.IO as T
-- -----------------------------------------------------------------------------
-- Request type
data LogRequest a where
WriteLog :: T.Text -> LogRequest ()
deriving instance Show (LogRequest a)
deriving instance Typeable LogRequest
instance ShowP LogRequest where showp = show
deriving instance Eq (LogRequest a)
instance Hashable (LogRequest a) where
hashWithSalt salt (WriteLog str) = hashWithSalt salt str
writeLog :: T.Text -> GenHaxl u w ()
writeLog = uncachedRequest . WriteLog
instance StateKey LogRequest where
data State LogRequest = LogDataState
initDataSource :: IO (State LogRequest)
initDataSource = return LogDataState
instance DataSourceName LogRequest where
dataSourceName _ = "LogDataSource"
instance DataSource u LogRequest where
-- State LogRequest -> Flags -> u -> PerformFetch LogRequest
fetch _state _flags _userEnv = SyncFetch doAll
where
doAll :: [BlockedFetch LogRequest] -> IO ()
doAll requests = mapM_ doOne requests
doOne :: BlockedFetch LogRequest -> IO ()
doOne (BlockedFetch (WriteLog str) var) = do
T.putStrLn $ "LOG: " <> str
putSuccess var ()