module Remover where
import IOExts

type Remover = IO ()
type RemoverRef = IORef (IO ())

callRemoverRef :: RemoverRef -> IO ()
callRemoverRef ref = do readIORef ref >>= id;writeIORef ref (return ())

callRemover :: RemoverRef -> IO () -> IO () 
callRemover ref rmnew = do {rmold <- readIORef ref;writeIORef ref rmnew;rmold}

mkRemover :: Remover -> IO Remover
mkRemover rm = do
 ref <- newIORef rm
 return $ callRemoverRef ref

callMbRemover :: IORef (Maybe (IO ())) -> Maybe (IO ()) -> IO ()
callMbRemover removerRef mb = do
 mb' <- readIORef removerRef
 maybe (return ()) id mb'
 writeIORef removerRef mb

