Resource management helpers provide modern JavaScript resource management and disposal patterns, supporting the using declaration proposal for automatic resource cleanup and explicit disposal.
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');
}
});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;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);
}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;Resource management helpers support both native and polyfilled symbols:
Symbol.dispose for synchronous disposalSymbol.asyncDispose for asynchronous disposalSymbol.for() for polyfilled environmentsThe disposal system properly aggregates errors:
Resources are managed in LIFO (Last-In-First-Out) order:
// 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;
}