forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWatcher.hs
More file actions
53 lines (48 loc) · 1.81 KB
/
Watcher.hs
File metadata and controls
53 lines (48 loc) · 1.81 KB
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
49
50
51
52
53
-----------------------------------------------------------------------------
--
-- Module : Language.PureScript.Ide.Watcher
-- Description : File watcher for externs files
-- Copyright : Christoph Hegemann 2016
-- License : MIT (http://opensource.org/licenses/MIT)
--
-- Maintainer : Christoph Hegemann <christoph.hegemann1337@gmail.com>
-- Stability : experimental
--
-- |
-- File watcher for externs files
-----------------------------------------------------------------------------
module Language.PureScript.Ide.Watcher
( watcher
) where
import Protolude
import Control.Concurrent.STM
import Language.PureScript.Ide.Externs
import Language.PureScript.Ide.State
import Language.PureScript.Ide.Types
import Language.PureScript.Ide.Util
import System.FilePath
import System.FSNotify
-- | Reloads an ExternsFile from Disc. If the Event indicates the ExternsFile
-- was deleted we don't do anything.
reloadFile :: TVar IdeState -> Event -> IO ()
reloadFile _ Removed{} = pure ()
reloadFile ref ev = do
let fp = eventPath ev
ef' <- runLogger LogDefault (runExceptT (readExternFile fp))
case ef' of
Left _ -> pure ()
Right ef -> do
void $ atomically (insertExternsSTM ref ef *> populateStage3STM ref)
putStrLn ("Reloaded File at: " ++ fp)
-- | Installs filewatchers for the given directory and reloads ExternsFiles when
-- they change on disc
watcher :: Bool -> TVar IdeState -> FilePath -> IO ()
watcher polling stateVar fp =
withManagerConf
(defaultConfig { confDebounce = NoDebounce
, confUsePolling = polling
}) $ \mgr -> do
_ <- watchTree mgr fp
(\ev -> takeFileName (eventPath ev) == "externs.json")
(reloadFile stateVar)
forever (threadDelay 100000)