Skip to content

Commit ab38cbf

Browse files
initial commit
1 parent 9a5e611 commit ab38cbf

File tree

7 files changed

+49
-11
lines changed

7 files changed

+49
-11
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"source-map-support": "^0.5.21",
111111
"ts-node": "^10.9.2",
112112
"tsd": "^0.31.0",
113-
"typescript": "5.0",
113+
"typescript": "5.3",
114114
"typescript-cached-transpile": "^0.0.6",
115115
"v8-heapsnapshot": "^1.3.1",
116116
"yargs": "^17.7.2"

src/change_stream.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ export interface UpdateDescription<TSchema extends Document = Document> {
525525
disambiguatedPaths?: Document;
526526
}
527527

528+
// @ts-expect-error Assigning to a readonly property.
529+
Symbol.asyncDispose ??= Symbol('asyncDispose');
530+
528531
/** @public */
529532
export type ChangeStreamEvents<
530533
TSchema extends Document = Document,
@@ -544,9 +547,17 @@ export type ChangeStreamEvents<
544547
* @public
545548
*/
546549
export class ChangeStream<
547-
TSchema extends Document = Document,
548-
TChange extends Document = ChangeStreamDocument<TSchema>
549-
> extends TypedEventEmitter<ChangeStreamEvents<TSchema, TChange>> {
550+
TSchema extends Document = Document,
551+
TChange extends Document = ChangeStreamDocument<TSchema>
552+
>
553+
extends TypedEventEmitter<ChangeStreamEvents<TSchema, TChange>>
554+
implements AsyncDisposable
555+
{
556+
async [Symbol.asyncDispose]() {
557+
console.error('ChangeStream[Symbol.asyncDispose]() called.');
558+
await this.close();
559+
}
560+
550561
pipeline: Document[];
551562
/**
552563
* @remarks WriteConcern can still be present on the options because

src/cursor/abstract_cursor.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { once } from 'events';
12
import { Readable, Transform } from 'stream';
23

34
import { type BSONSerializeOptions, type Document, Long, pluckBSONSerializeOptions } from '../bson';
@@ -122,6 +123,9 @@ export type AbstractCursorEvents = {
122123
[AbstractCursor.CLOSE](): void;
123124
};
124125

126+
// @ts-expect-error Assigning to a readonly property.
127+
Symbol.asyncDispose ??= Symbol('asyncDispose');
128+
125129
/** @public */
126130
export abstract class AbstractCursor<
127131
TSchema = any,
@@ -275,6 +279,12 @@ export abstract class AbstractCursor<
275279
return !!this.cursorClient.topology?.loadBalanced;
276280
}
277281

282+
async [Symbol.asyncDispose]() {
283+
const name = this.constructor.name;
284+
console.error(name + '[Symbol.asyncDispose]() called.');
285+
await this.close();
286+
}
287+
278288
/** Returns current buffered documents length */
279289
bufferedCount(): number {
280290
return this.documents?.length ?? 0;

src/mongo_client.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ export type MongoClientEvents = Pick<TopologyEvents, (typeof MONGO_CLIENT_EVENTS
325325

326326
const kOptions = Symbol('options');
327327

328+
// @ts-expect-error Assigning to a readonly property.
329+
Symbol.asyncDispose ??= Symbol('asyncDispose');
330+
328331
/**
329332
* The **MongoClient** class is a class that allows for making Connections to MongoDB.
330333
* @public
@@ -344,7 +347,7 @@ const kOptions = Symbol('options');
344347
* await client.insertOne({ name: 'spot', kind: 'dog' });
345348
* ```
346349
*/
347-
export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
350+
export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements AsyncDisposable {
348351
/** @internal */
349352
s: MongoClientPrivate;
350353
/** @internal */
@@ -404,6 +407,11 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
404407
this.checkForNonGenuineHosts();
405408
}
406409

410+
async [Symbol.asyncDispose]() {
411+
console.error('MongoClient[Symbol.asyncDispose]() called.');
412+
await this.close();
413+
}
414+
407415
/** @internal */
408416
private checkForNonGenuineHosts() {
409417
const documentDBHostnames = this[kOptions].hosts.filter((hostAddress: HostAddress) =>

src/sessions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ export interface EndSessionOptions {
9999
forceClear?: boolean;
100100
}
101101

102+
// @ts-expect-error Assigning to a readonly property.
103+
Symbol.asyncDispose ??= Symbol('asyncDispose');
104+
102105
/**
103106
* A class representing a client session on the server
104107
*
@@ -287,6 +290,11 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
287290
}
288291
}
289292

293+
async [Symbol.asyncDispose]() {
294+
console.error('ClientSession[Symbol.asyncDispose]() called.');
295+
await this.endSession({ force: true });
296+
}
297+
290298
/**
291299
* Advances the operationTime for a ClientSession.
292300
*

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"lib": [
1212
"es2021",
1313
"ES2022.Error",
14-
"ES2022.Object"
14+
"ES2022.Object",
15+
"ESNext.Disposable"
1516
],
1617
// We don't make use of tslib helpers, all syntax used is supported by target engine
1718
"importHelpers": false,

0 commit comments

Comments
 (0)