Created
September 9, 2022 14:01
-
-
Save nicolashery/ce5d627090553d8615d61f607078e778 to your computer and use it in GitHub Desktop.
Revisions
-
nicolashery created this gist
Sep 9, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ module AsyncMonadBaseControlExample where import Blammo.Logging (LoggingT) import Control.Concurrent.Async.Lifted.Safe (concurrently) import Control.Monad.Base (MonadBase) import Control.Monad.Reader (MonadIO, MonadReader, MonadTrans (lift), ReaderT) import Control.Monad.Trans.Control (MonadBaseControl (liftBaseWith, restoreM), StM) import Data.Text.Lazy qualified as TL import Web.Scotty.Trans (ActionT, text) data AppEnv = AppEnv newtype App a = App { unApp :: ReaderT AppEnv (LoggingT IO) a } deriving (Functor, Applicative, Monad, MonadIO, MonadReader AppEnv, MonadBase IO) -- Instance copied from: -- https://stackoverflow.com/questions/28137838/creating-monadbasecontrol-instance-for-newtype instance MonadBaseControl IO App where type StM App a = a liftBaseWith f = App $ liftBaseWith $ \runInBase -> f (runInBase . unApp) restoreM = App . restoreM executeTaskA :: App TL.Text executeTaskA = undefined executeTaskB :: App TL.Text executeTaskB = undefined exampleHandler :: ActionT TL.Text App () exampleHandler = do -- ... (resultA, resultB) <- lift $ concurrently executeTaskA executeTaskB -- ... text $ mconcat [resultA, "\n", resultB] This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ module AsyncMonadUnliftIOExample where import Blammo.Logging (LoggingT) import Control.Monad.Reader (MonadIO, MonadReader, MonadTrans (lift), ReaderT) import Data.Text.Lazy qualified as TL import UnliftIO (MonadUnliftIO) import UnliftIO.Async (concurrently) import Web.Scotty.Trans (ActionT, text) data AppEnv = AppEnv newtype App a = App { unApp :: ReaderT AppEnv (LoggingT IO) a } deriving (Functor, Applicative, Monad, MonadIO, MonadReader AppEnv, MonadUnliftIO) executeTaskA :: App TL.Text executeTaskA = undefined executeTaskB :: App TL.Text executeTaskB = undefined exampleHandler :: ActionT TL.Text App () exampleHandler = do -- ... (resultA, resultB) <- lift $ concurrently executeTaskA executeTaskB -- ... text $ mconcat [resultA, "\n", resultB]