resource_management.js 2.5 KB

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