Manage cleanup of resources with AbortSignal integration.
npm install jdisposerManage cleanup of resources with AbortSignal integration.
``typescript
import { makeDisposer } from 'jdisposer'
const disposer = makeDisposer()
// Use with fetch API
fetch('/api/data', { signal: disposer.signal })
// Add cleanup functions
disposer.add(() => console.log('Cleanup 1'))
disposer.add(() => console.log('Cleanup 2'))
// Clean up everything (aborts signal and calls cleanup functions)
disposer.dispose() // Subsequent calls are no-op
`
makeDisposer(): Creates a new Disposer instanceDisposer object has the following properties:dispose()
- : Abort signal and call all cleanup functions (no-op if already disposed)signal
- : AbortSignal that's aborted when disposedadd(fn?)
- : Add cleanup function
- Ignores falsy values
- Calls immediately if already disposed
- Functions called in reverse order on dispose
makeReset()`: Creates a resettable disposer pattern
- Returns a reset function that when called:
- Disposes the current disposer
- Creates and returns a new disposer instance
- Useful for managing resources that need periodic cleanup and recreation