[]
DsPdfJS API / withObjectManager
withObjectManager(
target,propertyKey?,descriptor?):any
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.
any
The class prototype, constructor, or function to be decorated
string
The name of the method being decorated (for class methods)
PropertyDescriptor
The property descriptor for the method (for class methods)
any
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();
...
});
Usage with new-style decorators (TypeScript 5.0+):
class Service {
@withObjectManager
method() {
// Automatic resource management
}
}
When resource cleanup fails. Errors are logged but not rethrown to preserve original error flow.