Browse Source

rpc backend: if we dont have stream data, send json Null rather than hang

Pat Hickey 9 years ago
parent
commit
52c3f5f168
2 changed files with 6 additions and 10 deletions
  1. 3 2
      src/Gidl/Backend/Rpc.hs
  2. 3 8
      support/rpc/Base.hs.template

+ 3 - 2
src/Gidl/Backend/Rpc.hs

@@ -182,7 +182,7 @@ webServerImports hasConsumer = stack $
   , text "import           Control.Concurrent.STM"
   , text "import           Control.Monad (forever)"
   , text "import           Control.Monad.IO.Class (liftIO)"
-  , text "import           Data.Aeson (encode)"
+  , text "import           Data.Aeson (encode,Value(Null))"
   ]
 
 
@@ -288,7 +288,8 @@ readStream :: Doc -> MethodName -> Doc
 readStream state name = nest 2 $ text "Snap.method Snap.GET $"
   </> doStmts
     [ text "x <- liftIO (atomically (readTSampleVar" <+> svar <> text "))"
-    , text "Snap.writeLBS (encode x)"
+    , text "let e = case x of Just v -> encode v; Nothing -> encode Null"
+    , text "Snap.writeLBS e"
     ]
   where
   svar = parens (fieldName name <+> state)

+ 3 - 8
support/rpc/Base.hs.template

@@ -10,7 +10,7 @@ import           Data.Map.Strict (Map)
 import qualified Data.Map.Strict as Map
 import           Control.Concurrent (forkIO)
 import           Control.Concurrent.STM
-                     (atomically,STM,retry,TVar,newTVar,writeTVar,readTVar
+                     (atomically,STM,TVar,newTVar,writeTVar,readTVar
                      ,TQueue,readTQueue,writeTQueue
                      ,TMVar,newEmptyTMVarIO,takeTMVar,putTMVar,newTVarIO
                      ,modifyTVar)
@@ -69,13 +69,8 @@ newTSampleVarIO  = atomically (TSampleVar `fmap` newTVar Nothing)
 writeTSampleVar :: TSampleVar a -> a -> STM ()
 writeTSampleVar (TSampleVar tv) a = writeTVar tv (Just a)
 
-readTSampleVar :: TSampleVar a -> STM a
-readTSampleVar (TSampleVar tv) =
-  do mb <- readTVar tv
-     case mb of
-       Just a  -> return a
-       Nothing -> retry
-
+readTSampleVar :: TSampleVar a -> STM (Maybe a)
+readTSampleVar (TSampleVar tv) = readTVar tv
 
 -- Response Handling -----------------------------------------------------------