module Polysemy.Final.IO
(
interpretFinalGlobal
, asyncToIOFinalGlobal
, resourceToIOFinalGlobal
) where
import qualified Control.Concurrent.Async as A
import qualified Control.Exception as X
import Polysemy
import Polysemy.Final
import Polysemy.Final.IO.Internal
import Polysemy.Async
import Polysemy.Resource
asyncToIOFinalGlobal :: Member (Final IO) r
=> Sem (Async ': r) a
-> Sem r a
asyncToIOFinalGlobal = interpretFinalGlobal $ \case
Async m -> do
ins <- getInspectorS
m' <- runS m
liftS $ A.async (inspect ins <$> m')
Await a -> liftS (A.wait a)
{-# INLINE asyncToIOFinalGlobal #-}
resourceToIOFinalGlobal :: Member (Final IO) r
=> Sem (Resource ': r) a
-> Sem r a
resourceToIOFinalGlobal = interpretFinalGlobal $ \case
Bracket alloc dealloc use -> do
a <- runS alloc
d <- bindS dealloc
u <- bindS use
pure $ X.bracket a d u
BracketOnError alloc dealloc use -> do
a <- runS alloc
d <- bindS dealloc
u <- bindS use
pure $ X.bracketOnError a d u
{-# INLINE resourceToIOFinalGlobal #-}