CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
resource_management.ts75 linesDownload Raw Back to src
1/**2 * @public3 */4export interface AsyncDisposable {5  /**6   * @beta7   * @experimental8   */9  [Symbol.asyncDispose](): Promise<void>;10 11  /**12   * @internal13   *14   * A method that wraps disposal semantics for a given resource in the class.15   */16  asyncDispose(): Promise<void>;17}18 19/** @internal */20export function configureResourceManagement(target: AsyncDisposable) {21  Symbol.asyncDispose &&22    Object.defineProperty(target, Symbol.asyncDispose, {23      value: async function asyncDispose(this: AsyncDisposable) {24        await this.asyncDispose();25      },26      enumerable: false,27      configurable: true,28      writable: true29    });30}31 32/**33 * @beta34 * @experimental35 *36 * Attaches `Symbol.asyncDispose` methods to the MongoClient, Cursors, sessions and change streams37 * if Symbol.asyncDispose is defined.38 *39 * It's usually not necessary to call this method - the driver attempts to attach these methods40 * itself when its loaded.  However, sometimes the driver may be loaded before `Symbol.asyncDispose`41 * is defined, in which case it is necessary to call this method directly.  This can happen if the42 * application is polyfilling `Symbol.asyncDispose`.43 *44 * Example:45 *46 * ```typescript47 * import { configureExplicitResourceManagement, MongoClient } from 'mongodb/lib/beta';48 *49 * Symbol.asyncDispose ??= Symbol('dispose');50 * load();51 *52 * await using client = new MongoClient(...);53 * ```54 */55export function configureExplicitResourceManagement() {56  // We must import lazily here, because there's a circular dependency between the resource management57  // file and each resources' file.  We could move `configureResourceManagement` to a separate58  // function, but keeping all resource-management related code together seemed preferable and I chose59  // lazy requiring of resources instead.60 61  // eslint-disable-next-line @typescript-eslint/no-require-imports62  const { MongoClient } = require('./mongo_client');63  // eslint-disable-next-line @typescript-eslint/no-require-imports64  const { ClientSession } = require('./sessions');65  // eslint-disable-next-line @typescript-eslint/no-require-imports66  const { AbstractCursor } = require('./cursor/abstract_cursor');67  // eslint-disable-next-line @typescript-eslint/no-require-imports68  const { ChangeStream } = require('./change_stream');69 70  configureResourceManagement(MongoClient.prototype);71  configureResourceManagement(ClientSession.prototype);72  configureResourceManagement(AbstractCursor.prototype);73  configureResourceManagement(ChangeStream.prototype);74}75