hakyll/src-interval/Hakyll/Web/Preview/Poll.hs

36 lines
1.1 KiB
Haskell
Raw Normal View History

2011-02-21 12:35:20 +00:00
-- | Interval-based implementation of preview polling, for the platforms which
-- are not supported by inotify.
--
2011-02-23 09:11:55 +00:00
module Hakyll.Web.Preview.Poll
2011-02-21 12:35:20 +00:00
( previewPoll
) where
import Control.Applicative ((<$>))
import Control.Concurrent (threadDelay)
2011-05-27 19:00:59 +00:00
import Control.Monad (filterM)
2011-02-21 12:35:20 +00:00
import System.Time (getClockTime)
import System.Directory (getModificationTime, doesFileExist)
2011-02-21 12:35:20 +00:00
import Hakyll.Core.Configuration
-- | A preview thread that periodically recompiles the site.
--
previewPoll :: HakyllConfiguration -- ^ Configuration
2011-05-27 19:00:59 +00:00
-> IO [FilePath] -- ^ Updating action
2011-02-21 12:35:20 +00:00
-> IO () -- ^ Can block forever
2011-05-27 19:00:59 +00:00
previewPoll _ update = do
2011-02-21 12:35:20 +00:00
time <- getClockTime
2011-05-27 19:00:59 +00:00
loop time =<< update
2011-02-21 12:35:20 +00:00
where
delay = 1000000
2011-05-27 19:00:59 +00:00
loop time files = do
2011-02-21 12:35:20 +00:00
threadDelay delay
files' <- filterM doesFileExist files
2011-05-27 19:00:59 +00:00
filesTime <- case files' of
[] -> return time
_ -> maximum <$> mapM getModificationTime files'
if filesTime > time || files' /= files
then loop filesTime =<< update
else loop time files'