Skip to content

Commit ebc938c

Browse files
committed
feat: remove callbacks from sessions.ts
1 parent 143e4cc commit ebc938c

File tree

1 file changed

+27
-53
lines changed

1 file changed

+27
-53
lines changed

src/sessions.ts

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
isPromiseLike,
3737
List,
3838
maxWireVersion,
39-
maybeCallback,
4039
now,
4140
uuidV4
4241
} from './utils';
@@ -246,47 +245,32 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
246245
* Ends this session on the server
247246
*
248247
* @param options - Optional settings. Currently reserved for future use
249-
* @param callback - Optional callback for completion of this operation
250248
*/
251-
endSession(): Promise<void>;
252-
endSession(options: EndSessionOptions): Promise<void>;
253-
/** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */
254-
endSession(callback: Callback<void>): void;
255-
/** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */
256-
endSession(options: EndSessionOptions, callback: Callback<void>): void;
257-
endSession(
258-
options?: EndSessionOptions | Callback<void>,
259-
callback?: Callback<void>
260-
): void | Promise<void> {
261-
if (typeof options === 'function') (callback = options), (options = {});
262-
const finalOptions = { force: true, ...options };
263-
264-
return maybeCallback(async () => {
265-
try {
266-
if (this.inTransaction()) {
267-
await this.abortTransaction();
268-
}
269-
if (!this.hasEnded) {
270-
const serverSession = this[kServerSession];
271-
if (serverSession != null) {
272-
// release the server session back to the pool
273-
this.sessionPool.release(serverSession);
274-
// Make sure a new serverSession never makes it onto this ClientSession
275-
Object.defineProperty(this, kServerSession, {
276-
value: ServerSession.clone(serverSession),
277-
writable: false
278-
});
279-
}
280-
// mark the session as ended, and emit a signal
281-
this.hasEnded = true;
282-
this.emit('ended', this);
249+
async endSession(options?: EndSessionOptions): Promise<void> {
250+
try {
251+
if (this.inTransaction()) {
252+
await this.abortTransaction();
253+
}
254+
if (!this.hasEnded) {
255+
const serverSession = this[kServerSession];
256+
if (serverSession != null) {
257+
// release the server session back to the pool
258+
this.sessionPool.release(serverSession);
259+
// Make sure a new serverSession never makes it onto this ClientSession
260+
Object.defineProperty(this, kServerSession, {
261+
value: ServerSession.clone(serverSession),
262+
writable: false
263+
});
283264
}
284-
} catch {
285-
// spec indicates that we should ignore all errors for `endSessions`
286-
} finally {
287-
maybeClearPinnedConnection(this, finalOptions);
265+
// mark the session as ended, and emit a signal
266+
this.hasEnded = true;
267+
this.emit('ended', this);
288268
}
289-
}, callback);
269+
} catch {
270+
// spec indicates that we should ignore all errors for `endSessions`
271+
} finally {
272+
maybeClearPinnedConnection(this, { force: true, ...options });
273+
}
290274
}
291275

292276
/**
@@ -420,26 +404,16 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
420404

421405
/**
422406
* Commits the currently active transaction in this session.
423-
*
424-
* @param callback - An optional callback, a Promise will be returned if none is provided
425407
*/
426-
commitTransaction(): Promise<Document>;
427-
/** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */
428-
commitTransaction(callback: Callback<Document>): void;
429-
commitTransaction(callback?: Callback<Document>): Promise<Document> | void {
430-
return maybeCallback(async () => endTransactionAsync(this, 'commitTransaction'), callback);
408+
async commitTransaction(): Promise<Document> {
409+
return endTransactionAsync(this, 'commitTransaction');
431410
}
432411

433412
/**
434413
* Aborts the currently active transaction in this session.
435-
*
436-
* @param callback - An optional callback, a Promise will be returned if none is provided
437414
*/
438-
abortTransaction(): Promise<Document>;
439-
/** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */
440-
abortTransaction(callback: Callback<Document>): void;
441-
abortTransaction(callback?: Callback<Document>): Promise<Document> | void {
442-
return maybeCallback(async () => endTransactionAsync(this, 'abortTransaction'), callback);
415+
async abortTransaction(): Promise<Document> {
416+
return endTransactionAsync(this, 'abortTransaction');
443417
}
444418

445419
/**

0 commit comments

Comments
 (0)