Skip to content

Commit 9335280

Browse files
address comments
1 parent a852a0e commit 9335280

File tree

10 files changed

+87
-90
lines changed

10 files changed

+87
-90
lines changed

.evergreen/run-resource-management-smoke-tests.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ echo "Building driver...finished."
99
echo "Node version: $(node -v)"
1010
cd test/explicit-resource-management
1111

12-
pwd
1312
npm i
1413
npm t
1514
mv xunit.xml ../..

src/beta.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
import * as contents from './index';
2-
import { configureExplicitResourceManagement } from './resource_management';
1+
import { type Document } from 'bson';
32

4-
export = { ...contents, configureExplicitResourceManagement };
3+
export * from './index';
4+
5+
/**
6+
* @internal
7+
*
8+
* Since we don't bundle tslib helpers, we need to polyfill this method.
9+
*
10+
* This is used in the generated JS.
11+
*/
12+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
13+
function __exportStar(mod: Document) {
14+
for (const key of Object.keys(mod)) {
15+
exports[key] = void 0;
16+
Object.defineProperty(exports, key, {
17+
enumerable: true,
18+
get: function () {
19+
return mod[key];
20+
}
21+
});
22+
}
23+
}

src/change_stream.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { type InferIdType, TypedEventEmitter } from './mongo_types';
1818
import type { AggregateOptions } from './operations/aggregate';
1919
import type { CollationOptions, OperationParent } from './operations/command';
2020
import type { ReadPreference } from './read_preference';
21-
import { type AsyncDisposable } from './resource_management';
21+
import { type AsyncDisposable, configureResourceManagement } from './resource_management';
2222
import type { ServerSessionId } from './sessions';
2323
import { filterOptions, getTopology, type MongoDBNamespace, squashError } from './utils';
2424

@@ -553,6 +553,10 @@ export class ChangeStream<
553553
{
554554
/** @beta */
555555
declare [Symbol.asyncDispose]: () => Promise<void>;
556+
/** @internal */
557+
async dispose() {
558+
await this.close();
559+
}
556560

557561
pipeline: Document[];
558562
/**
@@ -994,12 +998,4 @@ export class ChangeStream<
994998
}
995999
}
9961000

997-
Symbol.asyncDispose &&
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
1005-
});
1001+
configureResourceManagement(ChangeStream.prototype);

src/cursor/abstract_cursor.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { GetMoreOperation } from '../operations/get_more';
1717
import { KillCursorsOperation } from '../operations/kill_cursors';
1818
import { ReadConcern, type ReadConcernLike } from '../read_concern';
1919
import { ReadPreference, type ReadPreferenceLike } from '../read_preference';
20-
import { type AsyncDisposable } from '../resource_management';
20+
import { type AsyncDisposable, configureResourceManagement } from '../resource_management';
2121
import type { Server } from '../sdam/server';
2222
import { ClientSession, maybeClearPinnedConnection } from '../sessions';
2323
import { type MongoDBNamespace, squashError } from '../utils';
@@ -281,6 +281,10 @@ export abstract class AbstractCursor<
281281

282282
/** @beta */
283283
declare [Symbol.asyncDispose]: () => Promise<void>;
284+
/** @internal */
285+
async dispose() {
286+
await this.close();
287+
}
284288

285289
/** Returns current buffered documents length */
286290
bufferedCount(): number {
@@ -924,12 +928,4 @@ class ReadableCursorStream extends Readable {
924928
}
925929
}
926930

927-
Symbol.asyncDispose &&
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
935-
});
931+
configureResourceManagement(AbstractCursor.prototype);

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export {
7575
MongoUnexpectedServerResponseError,
7676
MongoWriteConcernError
7777
} from './error';
78+
export { configureExplicitResourceManagement } from './resource_management';
7879
export {
7980
AbstractCursor,
8081
// Actual driver classes exported
@@ -111,7 +112,6 @@ export { ReturnDocument } from './operations/find_and_modify';
111112
export { ProfilingLevel } from './operations/set_profiling_level';
112113
export { ReadConcernLevel } from './read_concern';
113114
export { ReadPreferenceMode } from './read_preference';
114-
export { AsyncDisposable } from './resource_management';
115115
export { ServerType, TopologyType } from './sdam/common';
116116

117117
// Helper classes
@@ -521,6 +521,7 @@ export type {
521521
ReadPreferenceLikeOptions,
522522
ReadPreferenceOptions
523523
} from './read_preference';
524+
export type { AsyncDisposable } from './resource_management';
524525
export type { ClusterTime, TimerQueue } from './sdam/common';
525526
export type {
526527
Monitor,

src/mongo_client.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { executeOperation } from './operations/execute_operation';
3434
import { RunAdminCommandOperation } from './operations/run_command';
3535
import type { ReadConcern, ReadConcernLevel, ReadConcernLike } from './read_concern';
3636
import { ReadPreference, type ReadPreferenceMode } from './read_preference';
37-
import { type AsyncDisposable } from './resource_management';
37+
import { type AsyncDisposable, configureResourceManagement } from './resource_management';
3838
import type { ServerMonitoringMode } from './sdam/monitor';
3939
import type { TagSet } from './sdam/server_description';
4040
import { readPreferenceServerSelector } from './sdam/server_selection';
@@ -407,6 +407,10 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
407407

408408
/** @beta */
409409
declare [Symbol.asyncDispose]: () => Promise<void>;
410+
/** @internal */
411+
async dispose() {
412+
await this.close();
413+
}
410414

411415
/** @internal */
412416
private checkForNonGenuineHosts() {
@@ -762,15 +766,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
762766
}
763767
}
764768

765-
Symbol.asyncDispose &&
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
773-
});
769+
configureResourceManagement(MongoClient.prototype);
774770

775771
/**
776772
* Parsed Mongo Client Options.

src/resource_management.ts

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
import { ChangeStream } from './change_stream';
2-
import { AbstractCursor } from './cursor/abstract_cursor';
3-
import { MongoClient } from './mongo_client';
4-
import { ClientSession } from './sessions';
5-
61
/**
72
* @public
8-
* @experimental
93
*/
104
export interface AsyncDisposable {
115
/** @beta */
12-
[Symbol.asyncDispose]: () => Promise<void>;
6+
[Symbol.asyncDispose](): Promise<void>;
7+
8+
/**
9+
* @internal
10+
*
11+
* A method that wraps disposal semantics for a given resource in the class.
12+
*/
13+
dispose(): Promise<void>;
14+
}
15+
16+
/** @internal */
17+
export function configureResourceManagement(target: AsyncDisposable) {
18+
Symbol.asyncDispose &&
19+
Object.defineProperty(target, Symbol.asyncDispose, {
20+
value: async function asyncDispose(this: AsyncDisposable) {
21+
await this.dispose();
22+
},
23+
enumerable: false,
24+
configurable: true,
25+
writable: true
26+
});
1327
}
1428

1529
/**
@@ -35,43 +49,22 @@ export interface AsyncDisposable {
3549
* ```
3650
*/
3751
export function configureExplicitResourceManagement() {
38-
Symbol.asyncDispose &&
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
46-
});
52+
// We must import lazily here, because there's a circular dependency between the resource management
53+
// file and each resources' file. We could move `configureResourceManagement` to a separate
54+
// function, but keeping all resource-management related code together seemed preferable and I chose
55+
// lazy requiring of resources instead.
4756

48-
Symbol.asyncDispose &&
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
56-
});
57+
// eslint-disable-next-line @typescript-eslint/no-var-requires
58+
const { MongoClient } = require('./mongo_client');
59+
// eslint-disable-next-line @typescript-eslint/no-var-requires
60+
const { ClientSession } = require('./sessions');
61+
// eslint-disable-next-line @typescript-eslint/no-var-requires
62+
const { AbstractCursor } = require('./cursor/abstract_cursor');
63+
// eslint-disable-next-line @typescript-eslint/no-var-requires
64+
const { ChangeStream } = require('./change_stream');
5765

58-
Symbol.asyncDispose &&
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
66-
});
67-
68-
Symbol.asyncDispose &&
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
76-
});
66+
configureResourceManagement(MongoClient.prototype);
67+
configureResourceManagement(ClientSession.prototype);
68+
configureResourceManagement(AbstractCursor.prototype);
69+
configureResourceManagement(ChangeStream.prototype);
7770
}

src/sessions.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { executeOperation } from './operations/execute_operation';
2727
import { RunAdminCommandOperation } from './operations/run_command';
2828
import { ReadConcernLevel } from './read_concern';
2929
import { ReadPreference } from './read_preference';
30-
import { type AsyncDisposable } from './resource_management';
30+
import { type AsyncDisposable, configureResourceManagement } from './resource_management';
3131
import { _advanceClusterTime, type ClusterTime, TopologyType } from './sdam/common';
3232
import {
3333
isTransactionCommand,
@@ -292,6 +292,10 @@ export class ClientSession
292292
}
293293
/** @beta */
294294
declare [Symbol.asyncDispose]: () => Promise<void>;
295+
/** @internal */
296+
async dispose() {
297+
await this.endSession({ force: true });
298+
}
295299

296300
/**
297301
* Advances the operationTime for a ClientSession.
@@ -490,15 +494,7 @@ export class ClientSession
490494
}
491495
}
492496

493-
Symbol.asyncDispose &&
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
501-
});
497+
configureResourceManagement(ClientSession.prototype);
502498

503499
const MAX_WITH_TRANSACTION_TIMEOUT = 120000;
504500
const NON_DETERMINISTIC_WRITE_CONCERN_ERRORS = new Set([

src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type { CommandOperationOptions, OperationParent } from './operations/comm
3131
import type { Hint, OperationOptions } from './operations/operation';
3232
import { ReadConcern } from './read_concern';
3333
import { ReadPreference } from './read_preference';
34+
import type { AsyncDisposable } from './resource_management';
3435
import { ServerType } from './sdam/common';
3536
import type { Server } from './sdam/server';
3637
import type { Topology } from './sdam/topology';

test/unit/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const EXPECTED_EXPORTS = [
1616
'AbstractCursor',
1717
'Admin',
1818
'AggregationCursor',
19-
'AsyncDisposable',
2019
'AuthMechanism',
2120
'AutoEncryptionLoggerLevel',
2221
'BatchType',
@@ -32,6 +31,7 @@ const EXPECTED_EXPORTS = [
3231
'ClientSession',
3332
'Code',
3433
'Collection',
34+
'configureExplicitResourceManagement',
3535
'CommandFailedEvent',
3636
'CommandStartedEvent',
3737
'CommandSucceededEvent',

0 commit comments

Comments
 (0)