Skip to content

Commit 0dda71b

Browse files
committed
correctly implement progressStop
1 parent 0b8c12d commit 0dda71b

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

ghcide/src/Development/IDE/Core/ProgressReporting.hs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Control.Monad.Extra
1616
import Control.Monad.IO.Class
1717
import Control.Monad.Trans.Class (lift)
1818
import Data.Foldable (for_)
19+
import Data.Functor (($>))
1920
import qualified Data.HashMap.Strict as HMap
2021
import qualified Data.Text as T
2122
import Data.Unique
@@ -46,6 +47,24 @@ noProgressReporting = return $ ProgressReporting
4647
, progressStop = pure ()
4748
}
4849

50+
data State
51+
= NotStarted
52+
| Completed
53+
| Stopped
54+
| Running (Async ())
55+
56+
data Transition = Event ProgressEvent | StopProgress
57+
58+
updateState :: IO () -> Transition -> State -> IO State
59+
updateState _ _ Stopped = pure Stopped
60+
updateState start (Event KickStarted) NotStarted = Running <$> async start
61+
updateState start (Event KickStarted) Completed = Running <$> async start
62+
updateState start (Event KickStarted) (Running a) = cancel a >> Running <$> async start
63+
updateState _ (Event KickCompleted) (Running a) = cancel a $> Completed
64+
updateState _ (Event KickCompleted) st = pure st
65+
updateState _ StopProgress (Running a) = cancel a $> Stopped
66+
updateState _ StopProgress st = pure st
67+
4968
-- | A 'ProgressReporting' that enqueues Begin and End notifications in a new
5069
-- thread, with a grace period (nothing will be sent if 'KickCompleted' arrives
5170
-- before the end of the grace period).
@@ -58,10 +77,9 @@ delayedProgressReporting
5877
-> IO ProgressReporting
5978
delayedProgressReporting before after lspEnv optProgressStyle = do
6079
inProgressVar <- newVar (HMap.empty @NormalizedFilePath @Int)
61-
progressThread <- newVar =<< async (pure ())
62-
let progressUpdate KickStarted = writeVar progressThread =<< async (mRunLspT lspEnv $ lspShakeProgress inProgressVar)
63-
progressUpdate KickCompleted = readVar progressThread >>= cancel
64-
progressStop = progressUpdate KickCompleted
80+
progressState <- newVar NotStarted
81+
let progressUpdate event = modifyVar_ progressState $ updateState (mRunLspT lspEnv $ lspShakeProgress inProgressVar) (Event event)
82+
progressStop = modifyVar_ progressState $ updateState (mRunLspT lspEnv $ lspShakeProgress inProgressVar) StopProgress
6583
inProgress :: NormalizedFilePath -> Action a -> Action a
6684
inProgress = withProgressVar inProgressVar
6785
return ProgressReporting{..}

0 commit comments

Comments
 (0)