on master help sign in
Merge pull request #21 from jargv/master

Added the ability to include user actions in the wai development handler

commit6bea22d5bfed4553a354bae5a32da064b42ab8c5 authorMichael Snoyman <michael@snoyman.com> dateWed 23 Nov 2011, 21:30 (15 years ago) parent6b5ef06bef8a42
2 files changed+46−11
wai-handler-devel/Network/Wai/Handler/DevelServer.hs+25−11 wai-handler-devel/run-onreloadapp.hs+21
wai-handler-devel/Network/Wai/Handler/DevelServer.hs+25−11
@@ -6,6 +6,8 @@ module Network.Wai.Handler.DevelServer
66 ( run
77 , runQuit
88 , runNoWatch
9+ , runWithReloadActions
10+ , runQuitWithReloadActions
911 ) where
1012
1113import Language.Haskell.Interpreter hiding (typeOf)
@@ -37,14 +39,18 @@ runNoWatch :: Int -> ModuleName -> FunctionName
3739 -> (FilePath -> IO [FilePath]) -> IO ()
3840runNoWatch port modu func extras = do
3941 ah <- initAppHolder
40− _ <- reload modu func extras Nothing ah
42+ _ <- reload modu func extras Nothing ah []
4143 Warp.run port $ toApp ah
4244
4345runQuit :: Int -> ModuleName -> FunctionName -> (FilePath -> IO [FilePath])
4446 -> IO ()
45−runQuit port modu func extras = do
47+runQuit port modu func extras = runQuitWithReloadActions port modu func extras []
48+
49+runQuitWithReloadActions :: Int -> ModuleName -> FunctionName -> (FilePath -> IO [FilePath])
50+ -> [IO (IO ())] -> IO ()
51+runQuitWithReloadActions port modu func extras actions = do
4652 sig <- newEmptyMVar
47− _ <- forkIO $ run port modu func extras (Just sig)
53+ _ <- forkIO $ runWithReloadActions port modu func extras (Just sig) actions
4854 go sig
4955 where
5056 go sig = do
@@ -57,12 +63,18 @@ runQuit port modu func extras = do
5763 go sig
5864 _ -> go sig
5965
60−run :: Int -> ModuleName -> FunctionName -> (FilePath -> IO [FilePath]) -> Maybe (MVar ())
61− -> IO ()
62−run port modu func extras msig = do
66+runWithReloadActions :: Int -> ModuleName -> FunctionName -> (FilePath -> IO [FilePath])
67+ -> Maybe (MVar ()) -> [IO (IO ())] -> IO ()
68+runWithReloadActions port modu func extras msig initActions = do
69+ actions <- mapM id initActions
6370 ah <- initAppHolder
64− _ <- forkIO $ fillApp modu func extras ah msig
71+ _ <- forkIO $ fillApp modu func extras ah msig actions
6572 Warp.run port $ toApp ah
73+ return ()
74+
75+run :: Int -> ModuleName -> FunctionName -> (FilePath -> IO [FilePath]) -> Maybe (MVar ())
76+ -> IO ()
77+run port modu func extras msig = runWithReloadActions port modu func extras msig []
6678
6779{-
6880startApp :: Queue -> Handler -> IO ()
@@ -93,8 +105,8 @@ constSE :: x -> SomeException -> x
93105constSE = const
94106
95107fillApp :: String -> String
96− -> (FilePath -> IO [FilePath]) -> AppHolder -> Maybe (MVar ()) -> IO ()
97−fillApp modu func dirs ah msig =
108+ -> (FilePath -> IO [FilePath]) -> AppHolder -> Maybe (MVar ()) -> [IO ()] -> IO ()
109+fillApp modu func dirs ah msig actions =
98110 go Nothing []
99111 where
100112 go prevError prevFiles = do
@@ -107,7 +119,7 @@ fillApp modu func dirs ah msig =
107119 return $ times /= map snd prevFiles
108120 (newError, newFiles) <-
109121 if toReload
110− then reload modu func dirs prevError ah
122+ then reload modu func dirs prevError ah actions
111123 else return (prevError, prevFiles)
112124 threadDelay 1000000
113125 go newError newFiles
@@ -116,8 +128,9 @@ reload :: String -> String
116128 -> (FilePath -> IO [FilePath])
117129 -> Maybe SomeException
118130 -> AppHolder
131+ -> [IO ()]
119132 -> IO (Maybe SomeException, [(FilePath, ClockTime)])
120−reload modu func extras prevError ah = do
133+reload modu func extras prevError ah actions = do
121134 case prevError of
122135 Nothing -> putStrLn "Attempting to interpret your app..."
123136 _ -> return ()
@@ -136,6 +149,7 @@ reload modu func extras prevError ah = do
136149 E.handle onInitErr $ do
137150 swapApp (\f -> app $ f . debug) ah
138151 times <- getTimes files
152+ sequence_ actions
139153 return (Nothing, zip files times)
140154 where
141155 onInitErr e = do
wai-handler-devel/run-onreloadapp.hsnew file+21
@@ -0,0 +1,21 @@
1+
2+import Network.Wai.Handler.DevelServer (runQuitWithReloadActions)
3+import System.Directory(findExecutable)
4+import System.Process(readProcess)
5+
6+main = runQuitWithReloadActions 4000 "SmallApp" "smallApp" (const $ return []) [browserRefresh]
7+
8+browserRefresh = do
9+ exe <- findExecutable "xdotool"
10+ case exe of
11+ Nothing -> do
12+ putStrLn "Install xdotool for automatic browser refresh"
13+ return $ return ()
14+ Just xdotool -> do
15+ putStrLn "Please click on the browser window..."
16+ pid <- readProcess xdotool ["selectwindow"] ""
17+ return $ do
18+ current <- readProcess xdotool ["getwindowfocus"] ""
19+ let cmds = ["windowfocus", pid, "key", "F5", "windowfocus", current]
20+ _ <- readProcess xdotool cmds ""
21+ return ()