summaryrefslogtreecommitdiff
path: root/src/Hakyll/Preview/Poll.hs
blob: 55118b34302fbca34e5bb4d585d3523e4b3103c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
--------------------------------------------------------------------------------
-- | Interval-based implementation of preview polling
{-# LANGUAGE CPP #-}
module Hakyll.Preview.Poll
    ( previewPoll
    ) where


--------------------------------------------------------------------------------
import           Control.Applicative       ((<$>))
import           Control.Concurrent        (threadDelay)
import           Control.Monad             (filterM)
#if MIN_VERSION_directory(1,2,0)
import           Data.Time                 (getCurrentTime)
#else
import           System.Time               (getClockTime)
#endif
import           System.Directory          (doesFileExist, getModificationTime)


--------------------------------------------------------------------------------
import           Hakyll.Core.Configuration


--------------------------------------------------------------------------------
-- | A preview thread that periodically recompiles the site.
previewPoll :: Configuration  -- ^ Configuration
            -> IO [FilePath]  -- ^ Updating action
            -> IO ()          -- ^ Can block forever
previewPoll _ update = do
#if MIN_VERSION_directory(1,2,0)
    time <- getCurrentTime
#else
    time <- getClockTime
#endif
    loop time =<< update
  where
    delay = 1000000
    loop time files = do
        threadDelay delay
        files' <- filterM doesFileExist files
        filesTime <- case files' of
            []  -> return time
            _   -> maximum <$> mapM getModificationTime files'

        if filesTime > time || files' /= files
            then loop filesTime =<< update
            else loop time files'