@@ -16,6 +16,7 @@ import Control.Monad.Extra
16
16
import Control.Monad.IO.Class
17
17
import Control.Monad.Trans.Class (lift )
18
18
import Data.Foldable (for_ )
19
+ import Data.Functor (($>) )
19
20
import qualified Data.HashMap.Strict as HMap
20
21
import qualified Data.Text as T
21
22
import Data.Unique
@@ -46,6 +47,24 @@ noProgressReporting = return $ ProgressReporting
46
47
, progressStop = pure ()
47
48
}
48
49
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
+
49
68
-- | A 'ProgressReporting' that enqueues Begin and End notifications in a new
50
69
-- thread, with a grace period (nothing will be sent if 'KickCompleted' arrives
51
70
-- before the end of the grace period).
@@ -58,10 +77,9 @@ delayedProgressReporting
58
77
-> IO ProgressReporting
59
78
delayedProgressReporting before after lspEnv optProgressStyle = do
60
79
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
65
83
inProgress :: NormalizedFilePath -> Action a -> Action a
66
84
inProgress = withProgressVar inProgressVar
67
85
return ProgressReporting {.. }
0 commit comments