-- | 'TVar' helpers for static 'Concurrent' effect.
--
-- @since 0.1
module Effectful.Concurrent.STM.TVar.Static
  ( -- * TVar
    newTVarA,
    readTVarA,
    writeTVarA,
    modifyTVarA',

    -- * Re-exports
    Concurrent,
    runConcurrent,
    TVar,
  )
where

import Control.Concurrent.STM.TVar (TVar)
import Control.Concurrent.STM.TVar qualified as TVar
import Effectful (Eff, type (:>))
import Effectful.Concurrent.STM (Concurrent, atomically, runConcurrent)

-- | @since 0.1
newTVarA :: (Concurrent :> es) => a -> Eff es (TVar a)
newTVarA :: forall (es :: [Effect]) a.
(Concurrent :> es) =>
a -> Eff es (TVar a)
newTVarA = STM (TVar a) -> Eff es (TVar a)
forall (es :: [Effect]) a. (Concurrent :> es) => STM a -> Eff es a
atomically (STM (TVar a) -> Eff es (TVar a))
-> (a -> STM (TVar a)) -> a -> Eff es (TVar a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> STM (TVar a)
forall a. a -> STM (TVar a)
TVar.newTVar

-- | @since 0.1
readTVarA :: (Concurrent :> es) => TVar a -> Eff es a
readTVarA :: forall (es :: [Effect]) a. (Concurrent :> es) => TVar a -> Eff es a
readTVarA = STM a -> Eff es a
forall (es :: [Effect]) a. (Concurrent :> es) => STM a -> Eff es a
atomically (STM a -> Eff es a) -> (TVar a -> STM a) -> TVar a -> Eff es a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar a -> STM a
forall a. TVar a -> STM a
TVar.readTVar

-- | @since 0.1
writeTVarA :: (Concurrent :> es) => TVar a -> a -> Eff es ()
writeTVarA :: forall (es :: [Effect]) a.
(Concurrent :> es) =>
TVar a -> a -> Eff es ()
writeTVarA TVar a
var = STM () -> Eff es ()
forall (es :: [Effect]) a. (Concurrent :> es) => STM a -> Eff es a
atomically (STM () -> Eff es ()) -> (a -> STM ()) -> a -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar a -> a -> STM ()
forall a. TVar a -> a -> STM ()
TVar.writeTVar TVar a
var

-- | @since 0.1
modifyTVarA' :: (Concurrent :> es) => TVar a -> (a -> a) -> Eff es ()
modifyTVarA' :: forall (es :: [Effect]) a.
(Concurrent :> es) =>
TVar a -> (a -> a) -> Eff es ()
modifyTVarA' TVar a
var = STM () -> Eff es ()
forall (es :: [Effect]) a. (Concurrent :> es) => STM a -> Eff es a
atomically (STM () -> Eff es ())
-> ((a -> a) -> STM ()) -> (a -> a) -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar a -> (a -> a) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
TVar.modifyTVar' TVar a
var