on b287c82 help sign in
48 lines 1.7 kB history raw
1--------------------------------------------------------------------------------
2-- | Interval-based implementation of preview polling
3{-# LANGUAGE CPP #-}
4module Hakyll.Preview.Poll
5 ( previewPoll
6 ) where
9--------------------------------------------------------------------------------
10import Control.Applicative ((<$>))
11import Control.Concurrent (threadDelay)
12import Control.Monad (filterM)
13#if MIN_VERSION_directory(1,2,0)
14import Data.Time (getCurrentTime)
15#else
16import System.Time (getClockTime)
17#endif
18import System.Directory (doesFileExist, getModificationTime)
21--------------------------------------------------------------------------------
22import Hakyll.Core.Configuration
25--------------------------------------------------------------------------------
26-- | A preview thread that periodically recompiles the site.
27previewPoll :: Configuration -- ^ Configuration
28 -> IO [FilePath] -- ^ Updating action
29 -> IO () -- ^ Can block forever
30previewPoll _ update = do
31#if MIN_VERSION_directory(1,2,0)
32 time <- getCurrentTime
33#else
34 time <- getClockTime
35#endif
36 loop time =<< update
37 where
38 delay = 1000000
39 loop time files = do
40 threadDelay delay
41 files' <- filterM doesFileExist files
42 filesTime <- case files' of
43 [] -> return time
44 _ -> maximum <$> mapM getModificationTime files'
46 if filesTime > time || files' /= files
47 then loop filesTime =<< update
48 else loop time files'