Commit ba05744d authored by Sumit Sahrawat's avatar Sumit Sahrawat

Seamless click handling

Evaluate accumulated widget messages in the Ghc Monad, after a comm_msg
recieved from the frontend is handled.
parent 37e4c2a6
...@@ -147,12 +147,14 @@ runKernel kernelOpts profileSrc = do ...@@ -147,12 +147,14 @@ runKernel kernelOpts profileSrc = do
-- We handle comm messages and normal ones separately. The normal ones are a standard -- We handle comm messages and normal ones separately. The normal ones are a standard
-- request/response style, while comms can be anything, and don't necessarily require a response. -- request/response style, while comms can be anything, and don't necessarily require a response.
if isCommMessage request if isCommMessage request
then liftIO $ do then do
oldState <- takeMVar state oldState <- liftIO $ takeMVar state
let replier = writeChan (iopubChannel interface) let replier = writeChan (iopubChannel interface)
newState <- handleComm replier oldState request replyHeader widgetMessageHandler = widgetHandler replier replyHeader
putMVar state newState tempState <- liftIO $ handleComm replier oldState request replyHeader
writeChan (shellReplyChannel interface) SendNothing newState <- flushWidgetMessages tempState [] widgetMessageHandler
liftIO $ putMVar state newState
liftIO $ writeChan (shellReplyChannel interface) SendNothing
else do else do
-- Create the reply, possibly modifying kernel state. -- Create the reply, possibly modifying kernel state.
oldState <- liftIO $ takeMVar state oldState <- liftIO $ takeMVar state
......
...@@ -8,6 +8,7 @@ This module exports all functions used for evaluation of IHaskell input. ...@@ -8,6 +8,7 @@ This module exports all functions used for evaluation of IHaskell input.
module IHaskell.Eval.Evaluate ( module IHaskell.Eval.Evaluate (
interpret, interpret,
evaluate, evaluate,
flushWidgetMessages,
Interpreter, Interpreter,
liftIO, liftIO,
typeCleaner, typeCleaner,
...@@ -325,18 +326,16 @@ evaluate kernelState code output widgetHandler = do ...@@ -325,18 +326,16 @@ evaluate kernelState code output widgetHandler = do
Just disps -> evalResult evalOut <> disps Just disps -> evalResult evalOut <> disps
helpStr = evalPager evalOut helpStr = evalPager evalOut
-- Capture all widget messages queued during code execution
messagesIO <- extractValue "IHaskell.Eval.Widgets.relayWidgetMessages"
messages <- liftIO messagesIO
let commMessages = evalMsgs evalOut ++ messages
-- Output things only if they are non-empty. -- Output things only if they are non-empty.
let empty = noResults result && null helpStr let empty = noResults result && null helpStr
unless empty $ unless empty $
liftIO $ output $ FinalResult result [plain helpStr] [] liftIO $ output $ FinalResult result [plain helpStr] []
-- Handle all the widget messages let tempMsgs = evalMsgs evalOut
newState <- liftIO $ widgetHandler (evalState evalOut) commMessages tempState = evalState evalOut { evalMsgs = [] }
-- Handle the widget messages
newState <- flushWidgetMessages tempState tempMsgs widgetHandler
case evalStatus evalOut of case evalStatus evalOut of
Success -> runUntilFailure newState rest Success -> runUntilFailure newState rest
...@@ -344,12 +343,25 @@ evaluate kernelState code output widgetHandler = do ...@@ -344,12 +343,25 @@ evaluate kernelState code output widgetHandler = do
storeItCommand execCount = Statement $ printf "let it%d = it" execCount storeItCommand execCount = Statement $ printf "let it%d = it" execCount
extractValue :: Typeable a => String -> Interpreter a extractValue :: Typeable a => String -> Interpreter a
extractValue expr = do extractValue expr = do
compiled <- dynCompileExpr expr compiled <- dynCompileExpr expr
case fromDynamic compiled of case fromDynamic compiled of
Nothing -> error "Error casting types in Evaluate.hs" Nothing -> error "Error casting types in Evaluate.hs"
Just result -> return result Just result -> return result
flushWidgetMessages :: KernelState
-> [WidgetMsg]
-> (KernelState -> [WidgetMsg] -> IO KernelState)
-> Interpreter KernelState
flushWidgetMessages state evalMsgs widgetHandler = do
-- Capture all widget messages queued during code execution
messagesIO <- extractValue "IHaskell.Eval.Widgets.relayWidgetMessages"
messages <- liftIO messagesIO
-- Handle all the widget messages
let commMessages = evalMsgs ++ messages
liftIO $ widgetHandler state commMessages
safely :: KernelState -> Interpreter EvalOut -> Interpreter EvalOut safely :: KernelState -> Interpreter EvalOut -> Interpreter EvalOut
safely state = ghandle handler . ghandle sourceErrorHandler safely state = ghandle handler . ghandle sourceErrorHandler
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment