import { CleanupCallback } from '../types' ; let cleanupCallbacks = [ ] ; async function cleanup ( ) { for ( const callback of cleanupCallbacks ) { await callback ( ) ; } cleanupCallbacks = [ ] ; } function addCleanup ( callback ) { cleanupCallbacks = [ callback , ... cleanupCallbacks ] ; return ( ) => removeCleanup ( callback ) ; } function removeCleanup ( callback ) { cleanupCallbacks = cleanupCallbacks . filter ( ( cb ) => cb !== callback ) ; } function skipAutoCleanup ( ) { try { return ! ! process . env . RHTL_SKIP_AUTO_CLEANUP ; } catch { return false ; } } function autoRegisterCleanup ( ) { if ( typeof afterEach === 'function' && ! skipAutoCleanup ( ) ) { afterEach ( async ( ) => { await cleanup ( ) ; } ) ; } } export { cleanup , addCleanup , removeCleanup , autoRegisterCleanup } ;