<commit_before>{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE OverloadedStrings #-}
module Socket where

import Control.Monad.Trans (MonadIO(liftIO))
import Control.Monad (forever)
import Control.Concurrent (threadDelay, forkIO)
import Control.Exception as E
import qualified Data.ByteString.Char8 as BSC
import qualified Filesystem.Path.CurrentOS as FP
import qualified Network.WebSockets as WS
import qualified System.FSNotify.Devel as NDevel
import qualified System.FSNotify as Notify

import qualified Generate

fileChangeApp :: FilePath -> WS.ServerApp
fileChangeApp watchedFile pendingConnection =
  do connection <- WS.acceptRequest pendingConnection
     _ <- forkIO $ keepAlive connection
     notifyManager <- liftIO $ Notify.startManager
     updateOnChange watchedFile connection notifyManager
     Notify.stopManager notifyManager

keepAlive :: WS.Connection -> IO ()
keepAlive connection =
  E.catch alive handler
  where
    alive =
      do WS.sendPing connection $ BSC.pack "ping"
         threadDelay (10 * 1000000) -- 10 seconds
         keepAlive connection
    handler :: E.SomeException -> IO ()
    handler _ = return ()

updateOnChange ::  FilePath -> WS.Connection -> Notify.WatchManager -> IO ()
updateOnChange watchedFile connection manager =
  do _ <- NDevel.treeExtExists manager "." "elm" (sendHotSwap watchedFile connection)
     forever $ threadDelay 10000000 -- related to https://ghc.haskell.org/trac/ghc/ticket/5544

sendHotSwap :: FilePath -> WS.Connection -> FP.FilePath -> IO ()
sendHotSwap watchedFile connection _ =
    do result <- liftIO $ Generate.js watchedFile
       WS.sendTextData connection $ BSC.pack result<commit_msg>Try to be really careful and clear about cleaning up threads<commit_after>{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE OverloadedStrings #-}
module Socket where

import Control.Monad.Trans (MonadIO(liftIO))
import Control.Concurrent (threadDelay, forkIO, killThread, ThreadId)
import Control.Exception (catch, SomeException)
import qualified Data.ByteString.Char8 as BSC
import qualified Filesystem.Path.CurrentOS as FP
import qualified Network.WebSockets as WS
import qualified System.FSNotify.Devel as NDevel
import qualified System.FSNotify as Notify

import qualified Generate

fileChangeApp :: FilePath -> WS.ServerApp
fileChangeApp watchedFile pendingConnection =
 do connection <- WS.acceptRequest pendingConnection
    Notify.withManager $ \notifyManager -> do
        _ <- NDevel.treeExtExists notifyManager "." "elm" (sendHotSwap watchedFile connection)
        keepAlive connection


sendHotSwap :: FilePath -> WS.Connection -> FP.FilePath -> IO ()
sendHotSwap watchedFile connection _ =
 do result <- liftIO (Generate.js watchedFile)
    WS.sendTextData connection (BSC.pack result)


keepAlive :: WS.Connection -> IO ()
keepAlive connection =
    loop
  where
    loop :: IO ()
    loop = do
      pingThread <- forkIO ping
      listen pingThread

    ping :: IO ()
    ping = do
      threadDelay (10 * 1000000) -- 10 seconds
      WS.sendPing connection ("ping" :: BSC.ByteString) `catch` connectionClosed

    connectionClosed :: SomeException -> IO ()
    connectionClosed _ = return ()

    listen :: ThreadId -> IO ()
    listen pingThread = do
      pong <- WS.receive connection
      case pong of
        WS.DataMessage _ -> listen pingThread
        WS.ControlMessage controlMessage ->
            case controlMessage of
              WS.Ping _ -> listen pingThread
              WS.Pong _ -> loop
              WS.Close _ ->
                  killThread pingThread >> return ()
{'fraction_non_alphanumeric': 0.08405960078718021, 'fraction_numerical': 0.010120888389091932, 'mean_word_length': 3.4143920595533497, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '1254522', 'n_tokens_mistral': 1046, 'n_tokens_neox': 953, 'n_words': 299}