Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add more thread context preserving combinators #230

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions freckle-app/library/Freckle/App/Async.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module Freckle.App.Async
, ThreadContext (..)
, getThreadContext
, withThreadContext
, forConcurrently
, forConcurrently_
, mapConcurrently
, mapConcurrently_
) where

import Freckle.App.Prelude
Expand Down Expand Up @@ -39,6 +43,30 @@ foldConcurrently xs = do
context <- getThreadContext
runConc $ foldMap (conc . withThreadContext context) xs

-- | 'UnliftIO.Async.forConcurrently' but passing the thread context along
forConcurrently
:: (MonadUnliftIO m, MonadMask m, Traversable t) => t a -> (a -> m b) -> m (t b)
forConcurrently = flip mapConcurrently

-- | 'UnliftIO.Async.mapConcurrently' but passing the thread context along
mapConcurrently
:: (MonadUnliftIO m, MonadMask m, Traversable t) => (a -> m b) -> t a -> m (t b)
mapConcurrently f xs = do
context <- getThreadContext
UnliftIO.mapConcurrently (withThreadContext context . f) xs

-- | 'UnliftIO.Async.forConcurrently_' but passing the thread context along
forConcurrently_
:: (MonadUnliftIO m, MonadMask m, Traversable t) => t a -> (a -> m b) -> m ()
forConcurrently_ = flip mapConcurrently_

-- | 'UnliftIO.Async.mapConcurrently_' but passing the thread context along
mapConcurrently_
:: (MonadUnliftIO m, MonadMask m, Traversable t) => (a -> m b) -> t a -> m ()
mapConcurrently_ f xs = do
context <- getThreadContext
UnliftIO.mapConcurrently_ (withThreadContext context . f) xs

-- | Wrapper around creating "Control.Immortal" processes
--
-- Features:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import Control.Monad (guard)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 qualified as BS8
import Data.ByteString.Lazy qualified as BSL
import Data.Foldable (toList)
import Data.List (isPrefixOf)
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.List.NonEmpty qualified as NE
Expand Down Expand Up @@ -121,7 +120,7 @@ showMatchRequestWithMismatches :: MatchRequest -> NonEmpty String -> String
showMatchRequestWithMismatches mr mismatches =
showMatchRequest mr
<> "\nMismatches {"
<> concatMap ("\n " <>) (toList mismatches)
<> concatMap ("\n " <>) mismatches
<> "\n}"
<> "\n"

Expand Down
6 changes: 6 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ cradle:
- path: "freckle-app/tests"
component: "freckle-app:test:spec"

- path: "freckle-ecs/library"
component: "freckle-ecs:lib"

- path: "freckle-env/library"
component: "freckle-env:lib"

Expand Down Expand Up @@ -41,3 +44,6 @@ cradle:

- path: "freckle-prelude/library"
component: "freckle-prelude:lib"

- path: "freckle-stats/library"
component: "freckle-stats:lib"
9 changes: 8 additions & 1 deletion stack.yaml.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/topics/lock_files
# https://docs.haskellstack.org/en/stable/lock_files

packages:
- completed:
Expand Down Expand Up @@ -95,6 +95,13 @@ packages:
size: 611
original:
hackage: monad-validate-1.3.0.0
- completed:
hackage: persistent-sql-lifted-0.1.0.0@sha256:1d4e828d3b1e76071d212d3cf940e1d9effaa3f71a850030120a0ee91e3283f7,2550
pantry-tree:
sha256: 748213f3ee6b8be36451d3b8ec3d273356032b4d0ee5f7fe4819877438d5fb7b
size: 890
original:
hackage: persistent-sql-lifted-0.1.0.0
- completed:
hackage: thread-utils-context-0.3.0.4@sha256:e763da1c6cab3b6d378fb670ca74aa9bf03c9b61b6fcf7628c56363fb0e3e71e,1671
pantry-tree:
Expand Down