[]
        
(Showing Draft Content)

withObjectManager

DsPdfJS API v9.1.0


DsPdfJS API / withObjectManager

Function: 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.

Parameters

target

any

The class prototype, constructor, or function to be decorated

propertyKey?

string

The name of the method being decorated (for class methods)

descriptor?

PropertyDescriptor

The property descriptor for the method (for class methods)

Returns

any

The modified property descriptor or wrapped function

Remarks

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.

Examples

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
  }
}

Throws

When resource cleanup fails. Errors are logged but not rethrown to preserve original error flow.