The class prototype, constructor, or function to be decorated
OptionalpropertyKey: stringThe name of the method being decorated (for class methods)
Optionaldescriptor: PropertyDescriptorThe property descriptor for the method (for class methods)
The modified property descriptor or wrapped function
This decorator provides automatic resource management for functions and class methods. It handles both synchronous and asynchronous operations, ensuring proper cleanup even when exceptions occur or promises are rejected.
Usage as a method decorator:
class PdfService {
@withObjectManager
createPdf() {
const doc = new PdfDocument(); // Automatically registered with ObjectManager
const brush = new Brush(); // Automatically registered with ObjectManager
...
@withObjectManager
async createPdfAsync() {
const doc = new PdfDocument();
await someAsyncOperation();
...
Usage as a function wrapper:
const createPdf = withObjectManager(() => {
const doc = new PdfDocument();
...
});
const createPdfAsync = withObjectManager(async () => {
const doc = new PdfDocument();
await someAsyncOperation();
...
});
Decorator function that automatically manages resources for class methods and functions. Creates an ObjectManager that is automatically disposed when the function completes. Supports both synchronous and asynchronous functions while preserving return values.