or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

array-helpers.mdasync-generators.mdclass-helpers.mddecorators.mdindex.mdmodule-interop.mdobject-helpers.mdprivate-members.mdresource-management.mdtype-utilities.md
tile.json

resource-management.mddocs/

Resource Management Helpers

Resource management helpers provide modern JavaScript resource management and disposal patterns, supporting the using declaration proposal for automatic resource cleanup and explicit disposal.

Capabilities

Using Declaration Support

using

Registers a resource for automatic disposal, implementing the using declaration proposal for deterministic cleanup of resources.

/**
 * Registers a resource for automatic disposal using Symbol.dispose or Symbol.asyncDispose
 * @param stack - Disposal stack to track resources
 * @param value - Resource object with dispose method
 * @param isAwait - Whether to use async disposal (Symbol.asyncDispose)
 * @returns The original resource value
 */
function using(stack: any[], value: any, isAwait?: boolean): any;

Usage Example:

import _using from '@babel/runtime/helpers/esm/using';

// File resource with automatic cleanup
const stack = [];
const file = _using(stack, {
  [Symbol.dispose]() {
    console.log('File closed');
  }
});

usingCtx

Creates a resource context for tracking disposable resources in a scoped manner.

/**
 * Creates a resource context for scoped resource management
 * @param value - Resource value to manage
 * @returns Resource context object
 */
function usingCtx(value: any): any;

Resource Disposal

dispose

Processes the disposal stack, calling dispose methods on registered resources and handling errors with proper aggregation.

/**
 * Disposes of all resources in the stack, handling errors appropriately
 * @param stack - Stack of resources to dispose
 * @param error - Existing error to propagate
 * @param hasError - Whether an error already occurred
 * @returns Promise for async disposal or void for sync disposal
 */
function dispose(stack: any[], error?: any, hasError?: boolean): any;

Usage Example:

import _using from '@babel/runtime/helpers/esm/using';
import _dispose from '@babel/runtime/helpers/esm/dispose';

// Complete resource management pattern
const stack = [];
try {
  const resource = _using(stack, createResource());
  // Use resource...
} catch (error) {
  await _dispose(stack, error, true);
} finally {
  await _dispose(stack);
}

Error Handling

dispose_SuppressedError

Creates suppressed error objects for proper error aggregation during resource disposal, matching the SuppressedError proposal.

/**
 * Creates a SuppressedError for aggregating disposal errors
 * @param error - Primary error
 * @param suppressed - Suppressed error from disposal
 * @returns SuppressedError instance
 */
function dispose_SuppressedError(error: any, suppressed: any): Error;

Implementation Details

Symbol Support

Resource management helpers support both native and polyfilled symbols:

  • Symbol.dispose for synchronous disposal
  • Symbol.asyncDispose for asynchronous disposal
  • Fallback to Symbol.for() for polyfilled environments

Error Aggregation

The disposal system properly aggregates errors:

  1. Primary errors are preserved
  2. Disposal errors are captured as suppressed errors
  3. Multiple disposal errors are chained appropriately
  4. Both sync and async disposal patterns are supported

Stack Management

Resources are managed in LIFO (Last-In-First-Out) order:

  1. Resources added to stack in registration order
  2. Disposal occurs in reverse order (stack.pop())
  3. Each resource tracks its disposal method and async flag
  4. Partial disposal continues even if individual resources fail

Types

// Resource management types
interface DisposableResource {
  [Symbol.dispose](): void;
}

interface AsyncDisposableResource {
  [Symbol.asyncDispose](): Promise<void>;
}

interface ResourceStackEntry {
  v: any;           // Resource value
  d: Function;      // Dispose function
  a: boolean;       // Is async disposal
}

// Error handling
interface SuppressedError extends Error {
  error: any;
  suppressed: any;
  stack: string;
}