jdisposer
v0.0.1
Published
Manage cleanup of resources with AbortSignal integration.
Readme
jdisposer - A Resource Management Library
Manage cleanup of resources with AbortSignal integration.
Sample Usage
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-opAPI
makeDisposer(): Creates a new Disposer instance
Disposer 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
