-- |Description: AtomicState Interpreters
module Polysemy.Conc.AtomicState where

import Control.Concurrent.STM (newTVarIO)

-- |Convenience wrapper around 'runAtomicStateTVar' that creates a new 'TVar'.
interpretAtomic ::
   a r .
  Member (Embed IO) r =>
  a ->
  InterpreterFor (AtomicState a) r
interpretAtomic :: forall a (r :: [(* -> *) -> * -> *]).
Member (Embed IO) r =>
a -> InterpreterFor (AtomicState a) r
interpretAtomic a
initial Sem (AtomicState a : r) a
sem = do
  TVar a
tv <- IO (TVar a) -> Sem r (TVar a)
forall (m :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Member (Embed m) r =>
m a -> Sem r a
embed (a -> IO (TVar a)
forall a. a -> IO (TVar a)
newTVarIO a
initial)
  TVar a -> Sem (AtomicState a : r) a -> Sem r a
forall (r :: [(* -> *) -> * -> *]) s a.
Member (Embed IO) r =>
TVar s -> Sem (AtomicState s : r) a -> Sem r a
runAtomicStateTVar TVar a
tv Sem (AtomicState a : r) a
sem
{-# inline interpretAtomic #-}