{-# LANGUAGE BangPatterns #-}
module Printer (
  printer, -- printer function to fork
  )
where

import Control.Concurrent (MVar, takeMVar)
import Control.Monad (replicateM)
import Data.Time (DiffTime)
import Kospi (Quote)

-- make sure printed output is not interleaved as several threads make
-- calls to print concurrently.
printer :: MVar (Maybe [(DiffTime, Quote)]) -> IO ()
{-# INLINE printer #-}
printer !m = go 
  where
    go = do msg <- takeMVar m
            case msg of
              Just xs -> do mapM_ (putStrLn . show) xs
                            go
              _    -> return ()
 
