3 changed files with 0 additions and 48 deletions
@ -1,46 +0,0 @@ |
|||||||
|
|
||||||
module TickTable ( |
|
||||||
mkTickTable, |
|
||||||
TickKey(..), |
|
||||||
getTick, |
|
||||||
TickTableH |
|
||||||
) where |
|
||||||
|
|
||||||
import ATrade.Types (DataType (..), Tick (..), |
|
||||||
TickerId (..)) |
|
||||||
import Control.Concurrent (forkIO) |
|
||||||
import Control.Concurrent.BoundedChan (BoundedChan, readChan) |
|
||||||
import Control.Concurrent.MVar (newEmptyMVar) |
|
||||||
import Control.Concurrent.MVar (readMVar) |
|
||||||
import Control.Concurrent.STM (TVar, atomically, modifyTVar', |
|
||||||
newTVarIO, readTVarIO) |
|
||||||
import Control.Monad (forever, void) |
|
||||||
import Control.Monad.Extra (whileM) |
|
||||||
import Data.IORef (readIORef) |
|
||||||
import qualified Data.Map.Strict as M |
|
||||||
import qualified Data.Text as T |
|
||||||
import System.ZMQ4 (Context) |
|
||||||
|
|
||||||
data TickKey = TickKey TickerId DataType |
|
||||||
deriving (Show, Ord, Eq) |
|
||||||
|
|
||||||
data TickTable = TickTable { |
|
||||||
ticks :: !(M.Map TickKey Tick) |
|
||||||
} |
|
||||||
type TickTableH = TVar TickTable |
|
||||||
|
|
||||||
mkTickTable :: BoundedChan Tick -> IO (TVar TickTable) |
|
||||||
mkTickTable chan = do |
|
||||||
shutdownMVar <- newEmptyMVar |
|
||||||
r <- newTVarIO TickTable { ticks = M.empty } |
|
||||||
void $ forkIO $ tickTableThread r shutdownMVar |
|
||||||
return r |
|
||||||
where |
|
||||||
tickTableThread r shutdownMVar = whileM $ do |
|
||||||
t <- readChan chan |
|
||||||
atomically $ modifyTVar' r (\s -> s { ticks = M.insert (TickKey (security t) (datatype t)) t $! ticks s }) |
|
||||||
readMVar shutdownMVar |
|
||||||
|
|
||||||
getTick :: TickTableH -> TickKey -> IO (Maybe Tick) |
|
||||||
getTick r key = M.lookup key . ticks <$> readTVarIO r |
|
||||||
|
|
||||||
Loading…
Reference in new issue