Skip to content

Instantly share code, notes, and snippets.

@mwotton
Created June 1, 2014 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwotton/6a8620e2289b4ea21fa7 to your computer and use it in GitHub Desktop.
Save mwotton/6a8620e2289b4ea21fa7 to your computer and use it in GitHub Desktop.
waiApp :: Parseable a => ([(Text, Query a)] -> IO (Async (), MVar (Text,Row))) -> Application
waiApp worker = \req -> do
-- could make this a Source, but we expect it to be relatively small.
body <- lazyRequestBody req
case decode body of
Nothing -> error "booo"
Just l -> do
let (badQ,goodQ) = partitionEithers $ map (\((name::Text),q) -> (name,) `fmap` parsePythia q) l
started <- worker goodQ
return (responseSource status200 [] $ do
yield $ Chunk $ fromLazyByteString "["
source started $= DCC.intersperse (Chunk $ fromLazyByteString ",\n")
yield $ Chunk $ fromLazyByteString "]"
)
source :: (Async (), MVar (Text,Row))
-> Source IO (Flush Builder)
source (task,mvar) = do
l <- liftIO $ race (wait task) (takeMVar mvar)
case l of
Left _ -> do
liftIO $ putStrLn "Source done"
Right x -> do
yield $ Chunk (fromLazyByteString $ encode x)
source (task,mvar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment