Skip to content

Commit a852a0e

Browse files
comments
1 parent 416e58c commit a852a0e

File tree

7 files changed

+63
-23
lines changed

7 files changed

+63
-23
lines changed

src/beta.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export * from './index';
1+
import * as contents from './index';
2+
import { configureExplicitResourceManagement } from './resource_management';
3+
4+
export = { ...contents, configureExplicitResourceManagement };

src/change_stream.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,11 @@ export class ChangeStream<
995995
}
996996

997997
Symbol.asyncDispose &&
998-
(ChangeStream.prototype[Symbol.asyncDispose] = async function () {
999-
await this.close();
998+
Object.defineProperty(ChangeStream.prototype, Symbol.asyncDispose, {
999+
value: async function asyncDispose(this: { close(): Promise<void> }) {
1000+
await this.close();
1001+
},
1002+
enumerable: false,
1003+
configurable: true,
1004+
writable: true
10001005
});

src/cursor/abstract_cursor.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,11 @@ class ReadableCursorStream extends Readable {
925925
}
926926

927927
Symbol.asyncDispose &&
928-
(AbstractCursor.prototype[Symbol.asyncDispose] = async function () {
929-
await this.close();
928+
Object.defineProperty(AbstractCursor.prototype, Symbol.asyncDispose, {
929+
value: async function asyncDispose(this: { close(): Promise<void> }) {
930+
await this.close();
931+
},
932+
enumerable: false,
933+
configurable: true,
934+
writable: true
930935
});

src/mongo_client.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,8 +763,13 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
763763
}
764764

765765
Symbol.asyncDispose &&
766-
(MongoClient.prototype[Symbol.asyncDispose] = async function () {
767-
await this.close();
766+
Object.defineProperty(MongoClient.prototype, Symbol.asyncDispose, {
767+
value: async function asyncDispose(this: { close(): Promise<void> }) {
768+
await this.close();
769+
},
770+
enumerable: false,
771+
configurable: true,
772+
writable: true
768773
});
769774

770775
/**

src/resource_management.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { AbstractCursor, ChangeStream } from './beta';
1+
import { ChangeStream } from './change_stream';
2+
import { AbstractCursor } from './cursor/abstract_cursor';
23
import { MongoClient } from './mongo_client';
34
import { ClientSession } from './sessions';
45

@@ -12,7 +13,6 @@ export interface AsyncDisposable {
1213
}
1314

1415
/**
15-
* @public
1616
* @beta
1717
*
1818
* Attaches `Symbol.asyncDispose` methods to the MongoClient, Cursors, sessions and change streams
@@ -26,7 +26,7 @@ export interface AsyncDisposable {
2626
* Example:
2727
*
2828
* ```typescript
29-
* import { load, MongoClient } from 'mongodb/beta';
29+
* import { configureExplicitResourceManagement, MongoClient } from 'mongodb/lib/beta';
3030
*
3131
* Symbol.asyncDispose ??= Symbol('dispose');
3232
* load();
@@ -36,22 +36,42 @@ export interface AsyncDisposable {
3636
*/
3737
export function configureExplicitResourceManagement() {
3838
Symbol.asyncDispose &&
39-
(MongoClient.prototype[Symbol.asyncDispose] = async function () {
40-
await this.close();
39+
Object.defineProperty(MongoClient.prototype, Symbol.asyncDispose, {
40+
value: async function asyncDispose(this: { close(): Promise<void> }) {
41+
await this.close();
42+
},
43+
enumerable: false,
44+
configurable: true,
45+
writable: true
4146
});
4247

4348
Symbol.asyncDispose &&
44-
(ClientSession.prototype[Symbol.asyncDispose] = async function () {
45-
await this.endSession({ force: true });
49+
Object.defineProperty(AbstractCursor.prototype, Symbol.asyncDispose, {
50+
value: async function asyncDispose(this: { close(): Promise<void> }) {
51+
await this.close();
52+
},
53+
enumerable: false,
54+
configurable: true,
55+
writable: true
4656
});
4757

4858
Symbol.asyncDispose &&
49-
(AbstractCursor.prototype[Symbol.asyncDispose] = async function () {
50-
await this.close();
59+
Object.defineProperty(ChangeStream.prototype, Symbol.asyncDispose, {
60+
value: async function asyncDispose(this: { close(): Promise<void> }) {
61+
await this.close();
62+
},
63+
enumerable: false,
64+
configurable: true,
65+
writable: true
5166
});
5267

5368
Symbol.asyncDispose &&
54-
(ChangeStream.prototype[Symbol.asyncDispose] = async function () {
55-
await this.close();
69+
Object.defineProperty(ClientSession.prototype, Symbol.asyncDispose, {
70+
value: async function asyncDispose(this: { endSession(): Promise<void> }) {
71+
await this.endSession();
72+
},
73+
enumerable: false,
74+
configurable: true,
75+
writable: true
5676
});
5777
}

src/sessions.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,13 @@ export class ClientSession
491491
}
492492

493493
Symbol.asyncDispose &&
494-
(ClientSession.prototype[Symbol.asyncDispose] = async function () {
495-
await this.endSession({ force: true });
494+
Object.defineProperty(ClientSession.prototype, Symbol.asyncDispose, {
495+
value: async function asyncDispose(this: { endSession(): Promise<void> }) {
496+
await this.endSession();
497+
},
498+
enumerable: false,
499+
configurable: true,
500+
writable: true
496501
});
497502

498503
const MAX_WITH_TRANSACTION_TIMEOUT = 120000;

test/explicit-resource-management/main.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import { Readable } from 'stream';
77
import { pipeline } from 'stream/promises';
88
import { setTimeout } from 'timers/promises';
99

10-
// @ts-expect-error Assigning readonly property.
11-
Symbol.asyncDispose ??= Symbol('dispose');
12-
1310
async function setUpCollection(client: MongoClient) {
1411
const collection = client.db('foo').collection<{ name: string }>('bar');
1512
const documents: Array<{ name: string }> = Array.from({ length: 5 }).map(i => ({

0 commit comments

Comments
 (0)