All files / src resource_management.ts

33.33% Statements 4/12
100% Branches 2/2
66.66% Functions 2/3
33.33% Lines 4/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                                      564x 1880x     4x                                                             564x                                        
/**
 * @public
 */
export interface AsyncDisposable {
  /**
   * @beta
   * @experimental
   */
  [Symbol.asyncDispose](): Promise<void>;
 
  /**
   * @internal
   *
   * A method that wraps disposal semantics for a given resource in the class.
   */
  asyncDispose(): Promise<void>;
}
 
/** @internal */
export function configureResourceManagement(target: AsyncDisposable) {
  Symbol.asyncDispose &&
    Object.defineProperty(target, Symbol.asyncDispose, {
      value: async function asyncDispose(this: AsyncDisposable) {
        await this.asyncDispose();
      },
      enumerable: false,
      configurable: true,
      writable: true
    });
}
 
/**
 * @beta
 * @experimental
 *
 * Attaches `Symbol.asyncDispose` methods to the MongoClient, Cursors, sessions and change streams
 * if Symbol.asyncDispose is defined.
 *
 * It's usually not necessary to call this method - the driver attempts to attach these methods
 * itself when its loaded.  However, sometimes the driver may be loaded before `Symbol.asyncDispose`
 * is defined, in which case it is necessary to call this method directly.  This can happen if the
 * application is polyfilling `Symbol.asyncDispose`.
 *
 * Example:
 *
 * ```typescript
 * import { configureExplicitResourceManagement, MongoClient } from 'mongodb/lib/beta';
 *
 * Symbol.asyncDispose ??= Symbol('dispose');
 * load();
 *
 * await using client = new MongoClient(...);
 * ```
 */
export function configureExplicitResourceManagement() {
  // We must import lazily here, because there's a circular dependency between the resource management
  // file and each resources' file.  We could move `configureResourceManagement` to a separate
  // function, but keeping all resource-management related code together seemed preferable and I chose
  // lazy requiring of resources instead.
 
  // eslint-disable-next-line @typescript-eslint/no-require-imports
  const { MongoClient } = require('./mongo_client');
  // eslint-disable-next-line @typescript-eslint/no-require-imports
  const { ClientSession } = require('./sessions');
  // eslint-disable-next-line @typescript-eslint/no-require-imports
  const { AbstractCursor } = require('./cursor/abstract_cursor');
  // eslint-disable-next-line @typescript-eslint/no-require-imports
  const { ChangeStream } = require('./change_stream');
 
  configureResourceManagement(MongoClient.prototype);
  configureResourceManagement(ClientSession.prototype);
  configureResourceManagement(AbstractCursor.prototype);
  configureResourceManagement(ChangeStream.prototype);
}