<commit_before>-----------------------------------------------------------------------------
-- |
-- Module : KD8ZRC.Mapview.Utility.Concurrent
-- Copyright : (C) 2015 <NAME>
-- License : MIT (see LICENSE file)
-- Maintainer : <NAME> <<EMAIL>>
-- Stability : experimental
-- Portability : ghc
--
-- Provides callbacks to aid in concurrent applications (e.g., updating a 'Chan'
-- at various stages).
----------------------------------------------------------------------------
module KD8ZRC.Mapview.Utility.Concurrent where

import qualified Control.Concurrent.Chan as Chan
import Control.Monad.IO.Class
import qualified Data.ByteString.Char8 as BS
import KD8ZRC.Mapview.Types

--------------------------------------------------------------------------------
-- Raw Packet Callbacks
--------------------------------------------------------------------------------

writeChanRaw :: Chan.Chan BS.ByteString -> PacketLineCallback t
writeChanRaw ch =
  PacketLineCallback (\raw -> liftIO $ Chan.writeChan ch raw)<commit_msg>Move writeChanJsonPkt to Concurrent from -noexc<commit_after>-----------------------------------------------------------------------------
-- |
-- Module : KD8ZRC.Mapview.Utility.Concurrent
-- Copyright : (C) 2016 <NAME>
-- License : MIT (see LICENSE file)
-- Maintainer : <NAME> <<EMAIL>>
-- Stability : experimental
-- Portability : ghc
--
-- Provides callbacks to aid in concurrent applications (e.g., updating a 'Chan'
-- at various stages).
----------------------------------------------------------------------------
module KD8ZRC.Mapview.Utility.Concurrent where

import qualified Control.Concurrent.Chan as Chan
import Control.Monad.IO.Class
import Data.Aeson (ToJSON, encode)
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import KD8ZRC.Mapview.Types

--------------------------------------------------------------------------------
-- Raw Packet Callbacks
--------------------------------------------------------------------------------

writeChanRaw :: Chan.Chan BS.ByteString -> PacketLineCallback t
writeChanRaw ch =
  PacketLineCallback (\raw -> liftIO $ Chan.writeChan ch raw)

-- | If we know that we can JSON-encode a packet, then we can use this fact to
-- send an encoded version of the packet to the web-socket automagically.
writeChanJsonPkt :: ToJSON t => Chan.Chan BS.ByteString -> ParsedPacketCallback t
writeChanJsonPkt ch =
  ParseSuccessCallback (
    \pkt -> liftIO $ Chan.writeChan ch (BSL.toStrict . encode $ pkt))
{'fraction_non_alphanumeric': 0.3286908077994429, 'fraction_numerical': 0.006764822920811779, 'mean_word_length': 1.494047619047619, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4541780', 'n_tokens_mistral': 631, 'n_tokens_neox': 585, 'n_words': 189}