module System.FSNotify.Listener
( debounce
, epsilonDefault
, FileListener(..)
, StopListening
, newDebouncePayload
) where
import Data.IORef (newIORef)
import Data.Time (diffUTCTime, NominalDiffTime)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Prelude hiding (FilePath)
import System.FSNotify.Types
import System.FilePath
type StopListening = IO ()
class FileListener sessionType where
initSession :: IO (Maybe sessionType)
killSession :: sessionType -> IO ()
listen :: WatchConfig -> sessionType -> FilePath -> ActionPredicate -> EventChannel -> IO StopListening
listenRecursive :: WatchConfig -> sessionType -> FilePath -> ActionPredicate -> EventChannel -> IO StopListening
usesPolling :: sessionType -> Bool
epsilonDefault :: NominalDiffTime
epsilonDefault :: NominalDiffTime
epsilonDefault = NominalDiffTime
0.001
eventDefault :: Event
eventDefault :: Event
eventDefault = FilePath -> UTCTime -> Bool -> Event
Added FilePath
"" (NominalDiffTime -> UTCTime
posixSecondsToUTCTime NominalDiffTime
0) Bool
False
debounce :: NominalDiffTime -> Event -> Event -> Bool
debounce :: NominalDiffTime -> Event -> Event -> Bool
debounce NominalDiffTime
epsilon Event
e1 Event
e2 =
Event -> FilePath
eventPath Event
e1 FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== Event -> FilePath
eventPath Event
e2 Bool -> Bool -> Bool
&& NominalDiffTime
timeDiff NominalDiffTime -> NominalDiffTime -> Bool
forall a. Ord a => a -> a -> Bool
> -NominalDiffTime
epsilon Bool -> Bool -> Bool
&& NominalDiffTime
timeDiff NominalDiffTime -> NominalDiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< NominalDiffTime
epsilon
where
timeDiff :: NominalDiffTime
timeDiff = UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime (Event -> UTCTime
eventTime Event
e2) (Event -> UTCTime
eventTime Event
e1)
newDebouncePayload :: Debounce -> IO DebouncePayload
newDebouncePayload :: Debounce -> IO DebouncePayload
newDebouncePayload Debounce
DebounceDefault = Event -> IO (IORef Event)
forall a. a -> IO (IORef a)
newIORef Event
eventDefault IO (IORef Event)
-> (IORef Event -> IO DebouncePayload) -> IO DebouncePayload
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= DebouncePayload -> IO DebouncePayload
forall (m :: * -> *) a. Monad m => a -> m a
return (DebouncePayload -> IO DebouncePayload)
-> (IORef Event -> DebouncePayload)
-> IORef Event
-> IO DebouncePayload
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DebounceData -> DebouncePayload
forall a. a -> Maybe a
Just (DebounceData -> DebouncePayload)
-> (IORef Event -> DebounceData) -> IORef Event -> DebouncePayload
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NominalDiffTime -> IORef Event -> DebounceData
DebounceData NominalDiffTime
epsilonDefault
newDebouncePayload (Debounce NominalDiffTime
epsilon) = Event -> IO (IORef Event)
forall a. a -> IO (IORef a)
newIORef Event
eventDefault IO (IORef Event)
-> (IORef Event -> IO DebouncePayload) -> IO DebouncePayload
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= DebouncePayload -> IO DebouncePayload
forall (m :: * -> *) a. Monad m => a -> m a
return (DebouncePayload -> IO DebouncePayload)
-> (IORef Event -> DebouncePayload)
-> IORef Event
-> IO DebouncePayload
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DebounceData -> DebouncePayload
forall a. a -> Maybe a
Just (DebounceData -> DebouncePayload)
-> (IORef Event -> DebounceData) -> IORef Event -> DebouncePayload
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NominalDiffTime -> IORef Event -> DebounceData
DebounceData NominalDiffTime
epsilon
newDebouncePayload Debounce
NoDebounce = DebouncePayload -> IO DebouncePayload
forall (m :: * -> *) a. Monad m => a -> m a
return DebouncePayload
forall a. Maybe a
Nothing