@tsdotnet/disposable - v2.0.1
    Preparing search index...

    Class AsyncDisposableBaseAbstract

    Defines the contract for objects that can be disposed asynchronously to release resources.

    This interface is for objects that require asynchronous operations during disposal, such as closing database connections, flushing buffers, or performing network cleanup. This allows for simple type checking that includes types that don't explicitly declare themselves as implementing this interface but do have a disposeAsync() method.

    class AsyncDatabaseConnection implements AsyncDisposable {
    async disposeAsync(): Promise<void> {
    await this.flushPendingOperations();
    await this.closeConnection();
    }
    }

    // Usage with async/await
    const connection = new AsyncDatabaseConnection();
    try {
    // Use connection...
    } finally {
    await connection.disposeAsync();
    }

    Hierarchy

    • default
      • AsyncDisposableBase

    Implements

    Index

    Constructors

    Accessors

    • get wasDisposed(): boolean

      True if object is disposed or disposing.

      Returns boolean

    Methods

    • Protected

      Completes disposal process and executes finalizer.

      Returns void

    • Protected

      Called before disposal starts. Override in derived classes.

      Returns void

    • Is called when this object is disposed. Should NOT be called directly. Override this method to handle disposal.

      Returns Promise<void>

    • Protected

      Initiates disposal process. Returns false if already disposed.

      Returns boolean

    • Protected

      Throws ObjectDisposedException if object is disposed.

      Parameters

      • strict: boolean = false

        When true, throws if not in Alive state.

      Returns boolean

    • Asynchronously releases all resources used by the object.

      This method should be idempotent - calling it multiple times should have no additional effect after the first call completes. Implementations should:

      • Await any pending asynchronous operations
      • Close connections, streams, or handles asynchronously
      • Flush any buffers or pending data
      • Unsubscribe from async events or observables
      • Clear any references that could prevent garbage collection

      Returns Promise<void>

      A Promise that resolves when disposal is complete.

      class FileBuffer implements AsyncDisposable {
      async disposeAsync(): Promise<void> {
      await this.flush(); // Write pending data
      await this.close(); // Close file handle
      }
      }

      // Proper async disposal
      const buffer = new FileBuffer();
      try {
      // Use buffer...
      } finally {
      await buffer.disposeAsync(); // Wait for complete cleanup
      }