@@ -55,8 +55,8 @@ module DAP.Adaptor
5555 , sendRaw
5656 -- * Internal function used to execute actions on behalf of the DAP server
5757 -- from child threads (useful for handling asynchronous debugger events).
58- , runAdaptorWith
59- , runAdaptor
58+ , runAdaptorPoly
59+ , runAdaptorRequest
6060 , withRequest
6161 , getHandle
6262 ) where
@@ -178,7 +178,7 @@ registerNewDebugSession k v debuggerConcurrentActions = do
178178 let emptyState = AdaptorState MessageTypeEvent []
179179 debuggerThreadState <- liftIO $
180180 DebuggerThreadState
181- <$> sequence [fork $ action (runAdaptorWith lcl' emptyState) | action <- debuggerConcurrentActions]
181+ <$> sequence [fork $ action (runAdaptorPoly lcl' emptyState) | action <- debuggerConcurrentActions]
182182 liftIO . atomically $ modifyTVar' store (H. insert k (debuggerThreadState, v))
183183 logInfo $ T. pack $ " Registered new debug session: " <> unpack k
184184 setDebugSessionId k
@@ -463,24 +463,29 @@ getReverseRequestResponseBody resp = do
463463 logError (T. pack reason)
464464 liftIO $ throwIO (ParseException reason)
465465----------------------------------------------------------------------------
466- -- | Evaluates Adaptor action by using and updating the state in the MVar
467- runAdaptorWith :: AdaptorLocal app request -> AdaptorState -> Adaptor app request () -> IO ()
468- runAdaptorWith lcl st (Adaptor action) = do
466+ -- | Run an Adaptor for any parametric 'request' (i.e. this function can be
467+ -- used regardless in a non-Request scenario).
468+ runAdaptorPoly :: AdaptorLocal app request -> AdaptorState -> Adaptor app request a -> IO a
469+ runAdaptorPoly lcl st (Adaptor action) = do
469470 (es,final_st) <- runStateT (runReaderT (runExceptT action) lcl) st
470471 case es of
471472 Left err -> error (" runAdaptorWith, unhandled exception:" <> show err)
472- Right () -> case final_st of
473+ Right x -> case final_st of
473474 AdaptorState _ p ->
474475 if null p
475- then return ()
476+ then return x
476477 else error $ " runAdaptorWith, unexpected payload:" <> show p
477478----------------------------------------------------------------------------
478- -- | Utility for evaluating a monad transformer stack
479- runAdaptor :: AdaptorLocal app Request -> AdaptorState -> Adaptor app Request () -> IO ()
480- runAdaptor lcl s (Adaptor client) =
479+ -- | Run an Adaptor in the context of replying to a 'Request' (notably, this
480+ -- should be used to run the Adaptor servicing the client ('serviceClient')).
481+ --
482+ -- When 'sendError' is used to throw an error in the Adaptor, we cancel the
483+ -- current pending request with an 'ErrorResponse'.
484+ runAdaptorRequest :: AdaptorLocal app Request -> AdaptorState -> Adaptor app Request () -> IO ()
485+ runAdaptorRequest lcl s (Adaptor client) =
481486 runStateT (runReaderT (runExceptT client) lcl) s >>= \ case
482487 (Left (errorMessage, maybeMessage), s') ->
483- runAdaptor lcl s' (sendErrorResponse errorMessage maybeMessage)
488+ runAdaptorRequest lcl s' (sendErrorResponse errorMessage maybeMessage)
484489 (Right () , _) -> pure ()
485490----------------------------------------------------------------------------
486491
0 commit comments