-- Copyright (c) 2014-present, Facebook, Inc.
-- All rights reserved.
--
-- This source code is distributed under the terms of a BSD license,
-- found in the LICENSE file.

-- | Internal utilities only.
--
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
module Haxl.Core.Util
  ( atomicallyOnBlocking
  , compose
  , textShow
  , trace_
  ) where

import Data.Text (Text)
import Debug.Trace (trace)
import qualified Data.Text as Text

import Control.Concurrent.STM
import Control.Exception

atomicallyOnBlocking :: Exception e => e -> STM a -> IO a
atomicallyOnBlocking :: e -> STM a -> IO a
atomicallyOnBlocking e
e STM a
stm =
  IO a -> (BlockedIndefinitelyOnSTM -> IO a) -> IO a
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch (STM a -> IO a
forall a. STM a -> IO a
atomically STM a
stm)
        (\BlockedIndefinitelyOnSTM
BlockedIndefinitelyOnSTM -> e -> IO a
forall a e. Exception e => e -> a
throw e
e)

-- | Composes a list of endofunctions.
compose :: [a -> a] -> a -> a
compose :: [a -> a] -> a -> a
compose = ((a -> a) -> (a -> a) -> a -> a) -> (a -> a) -> [a -> a] -> a -> a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) a -> a
forall a. a -> a
id

textShow :: (Show a) => a -> Text
textShow :: a -> Text
textShow = String -> Text
Text.pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show

-- | This function can be used to trace a bunch of lines to stdout when
-- debugging haxl core.
trace_ :: String -> a -> a
trace_ :: String -> a -> a
trace_ String
_ = a -> a
forall a. a -> a
id
--trace_ = Debug.Trace.trace