Skip to content

Commit

Permalink
Drop stm-conduit dep and directly use stm-chans instead
Browse files Browse the repository at this point in the history
  • Loading branch information
barrucadu committed Feb 2, 2018
1 parent b6d5e08 commit 1b2763d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Network/IRC/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ module Network.IRC.Client
) where

import Control.Concurrent.STM (newTVarIO)
import Control.Concurrent.STM.TBMChan (newTBMChanIO)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.ByteString (ByteString)
import qualified Data.Conduit.Network.TLS as TLS
import Data.Conduit.TMChan (newTBMChanIO)
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T
Expand Down
29 changes: 23 additions & 6 deletions Network/IRC/Client/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ module Network.IRC.Client.Internal
import Control.Applicative ((<$>))
import Control.Concurrent (forkIO, killThread, myThreadId, threadDelay, throwTo)
import Control.Concurrent.STM (STM, atomically, readTVar, readTVarIO, writeTVar)
import Control.Concurrent.STM.TBMChan (TBMChan, closeTBMChan, isClosedTBMChan, isEmptyTBMChan, readTBMChan, writeTBMChan, newTBMChan)
import Control.Monad (forM_, unless, void, when)
import Control.Monad.Catch (SomeException, catch)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Reader (ask, runReaderT)
import Data.ByteString (ByteString)
import Data.Conduit (Producer, Conduit, Consumer, (=$=), ($=), (=$), await, awaitForever, toProducer, yield)
import Data.Conduit.TMChan (closeTBMChan, isClosedTBMChan, isEmptyTBMChan, sourceTBMChan, writeTBMChan, newTBMChan)
import Data.Conduit ((=$=), ($=), (=$), await, awaitForever, toProducer, yield)
import qualified Data.Conduit as C
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import qualified Data.Set as S
import Data.Text (Text)
Expand All @@ -58,7 +59,7 @@ import Network.IRC.Client.Lens
-- | Config to connect to a server using the supplied connection
-- function.
setupInternal
:: (IO () -> Consumer (Either ByteString (Event ByteString)) IO () -> Producer IO (Message ByteString) -> IO ())
:: (IO () -> C.Consumer (Either ByteString (Event ByteString)) IO () -> C.Producer IO (Message ByteString) -> IO ())
-- ^ Function to start the network conduits.
-> IRC s ()
-- ^ Connect handler
Expand Down Expand Up @@ -143,13 +144,13 @@ runner = do
_ondisconnect cconf exc

-- | Forget failed decodings.
forgetful :: Monad m => Conduit (Either a b) m b
forgetful :: Monad m => C.Conduit (Either a b) m b
forgetful = awaitForever go where
go (Left _) = return ()
go (Right b) = yield b

-- | Block on receiving a message and invoke all matching handlers.
eventSink :: MonadIO m => IORef UTCTime -> IRCState s -> Consumer (Event ByteString) m ()
eventSink :: MonadIO m => IORef UTCTime -> IRCState s -> C.Consumer (Event ByteString) m ()
eventSink lastReceived ircstate = go where
go = await >>= maybe (return ()) (\event -> do
-- Record the current time.
Expand Down Expand Up @@ -183,7 +184,7 @@ isIgnored ircstate ev = do
Server _ -> False

-- |A conduit which logs everything which goes through it.
logConduit :: MonadIO m => (a -> IO ()) -> Conduit a m a
logConduit :: MonadIO m => (a -> IO ()) -> C.Conduit a m a
logConduit logf = awaitForever $ \x -> do
-- Call the logging function
liftIO $ logf x
Expand Down Expand Up @@ -315,3 +316,19 @@ timeoutBlock dt check = liftIO $ do
cond <- check
when (now < finish && not cond) wait
wait

-- | A simple wrapper around a TBMChan. As data is pushed into the
-- channel, the source will read it and pass it down the conduit
-- pipeline. When the channel is closed, the source will close also.
--
-- If the channel fills up, the pipeline will stall until values are
-- read.
--
-- From stm-conduit-3.0.0 (by Clark Gaebel <[email protected]>)
sourceTBMChan :: MonadIO m => TBMChan a -> C.Source m a
sourceTBMChan ch = loop where
loop = do
a <- liftIO . atomically $ readTBMChan ch
case a of
Just x -> yield x >> loop
Nothing -> pure ()
2 changes: 1 addition & 1 deletion Network/IRC/Client/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ module Network.IRC.Client.Internal.Types where
import Control.Applicative (Alternative)
import Control.Concurrent (ThreadId)
import Control.Concurrent.STM (TVar, atomically, readTVar, writeTVar)
import Control.Concurrent.STM.TBMChan (TBMChan)
import Control.Monad (MonadPlus)
import Control.Monad.Catch (Exception, MonadThrow, MonadCatch, MonadMask, SomeException)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Reader (MonadReader, ReaderT, asks)
import Control.Monad.State (MonadState(..))
import Data.ByteString (ByteString)
import Data.Conduit (Consumer, Producer)
import Data.Conduit.TMChan (TBMChan)
import qualified Data.Set as S
import Data.Text (Text)
import Data.Time.Clock (NominalDiffTime)
Expand Down
2 changes: 1 addition & 1 deletion irc-client.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ library
, old-locale >=1.0 && <1.1
, profunctors >=5 && <6
, stm >=2.4 && <2.5
, stm-conduit >=2.5 && <3.1
, stm-chans >=2.0 && <3.1
, text >=1.1 && <1.3
, time >=1.4 && <1.9
, tls >=1.3 && <1.5
Expand Down

0 comments on commit 1b2763d

Please sign in to comment.