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

38 lines
1.2 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)
import Control.Monad (when, filterM)
2011-02-21 12:35:20 +00:00
import System.Time (getClockTime)
import Data.Set (Set)
import qualified Data.Set as S
import System.Directory (getModificationTime, doesFileExist)
2011-02-21 12:35:20 +00:00
import Hakyll.Core.Configuration
import Hakyll.Core.Identifier
2011-04-05 20:14:49 +00:00
import Hakyll.Core.Resource
2011-02-21 12:35:20 +00:00
-- | A preview thread that periodically recompiles the site.
--
previewPoll :: HakyllConfiguration -- ^ Configuration
-> Set Resource -- ^ Resources to watch
-> IO () -- ^ Action called when something changes
-> IO () -- ^ Can block forever
previewPoll _ resources callback = do
let files = map (toFilePath . unResource) $ S.toList resources
time <- getClockTime
loop files time
where
delay = 1000000
loop files time = do
threadDelay delay
files' <- filterM doesFileExist files
modified <- any (time <) <$> mapM getModificationTime files'
when (modified || files' /= files) callback
loop files' =<< getClockTime