# withObjectManager

## Content

[**DsPdfJS API v9.1.3**](../README)

***

[DsPdfJS API](../globals) / withObjectManager

# Function: withObjectManager()

> **withObjectManager**(`target`, `propertyKey?`, `descriptor?`): `any`

Decorator function that automatically manages resources for class methods and functions.
Creates an [ObjectManager](../classes/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:**
```typescript
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:**
```typescript
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+):**
```typescript
class Service {
  @withObjectManager
  method() {
    // Automatic resource management
  }
}
```

## Throws

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