Skip to content

Commit 48ac3f4

Browse files
committed
feat: remove callbacks from cursors
1 parent f523b7f commit 48ac3f4

File tree

3 files changed

+49
-122
lines changed

3 files changed

+49
-122
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 39 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { ReadConcern, ReadConcernLike } from '../read_concern';
2121
import { ReadPreference, ReadPreferenceLike } from '../read_preference';
2222
import type { Server } from '../sdam/server';
2323
import { ClientSession, maybeClearPinnedConnection } from '../sessions';
24-
import { Callback, List, maybeCallback, MongoDBNamespace, ns } from '../utils';
24+
import { Callback, List, MongoDBNamespace, ns } from '../utils';
2525

2626
/** @internal */
2727
const kId = Symbol('id');
@@ -352,60 +352,43 @@ export abstract class AbstractCursor<
352352
return new ReadableCursorStream(this);
353353
}
354354

355-
hasNext(): Promise<boolean>;
356-
/** @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 */
357-
hasNext(callback: Callback<boolean>): void;
358-
hasNext(callback?: Callback<boolean>): Promise<boolean> | void {
359-
return maybeCallback(async () => {
360-
if (this[kId] === Long.ZERO) {
361-
return false;
362-
}
355+
async hasNext(): Promise<boolean> {
356+
if (this[kId] === Long.ZERO) {
357+
return false;
358+
}
363359

364-
if (this[kDocuments].length !== 0) {
365-
return true;
366-
}
360+
if (this[kDocuments].length !== 0) {
361+
return true;
362+
}
367363

368-
const doc = await nextAsync<TSchema>(this, true);
364+
const doc = await nextAsync<TSchema>(this, true);
369365

370-
if (doc) {
371-
this[kDocuments].unshift(doc);
372-
return true;
373-
}
366+
if (doc) {
367+
this[kDocuments].unshift(doc);
368+
return true;
369+
}
374370

375-
return false;
376-
}, callback);
371+
return false;
377372
}
378373

379374
/** Get the next available document from the cursor, returns null if no more documents are available. */
380-
next(): Promise<TSchema | null>;
381-
/** @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 */
382-
next(callback: Callback<TSchema | null>): void;
383-
/** @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 */
384-
next(callback?: Callback<TSchema | null>): Promise<TSchema | null> | void;
385-
next(callback?: Callback<TSchema | null>): Promise<TSchema | null> | void {
386-
return maybeCallback(async () => {
387-
if (this[kId] === Long.ZERO) {
388-
throw new MongoCursorExhaustedError();
389-
}
375+
async next(): Promise<TSchema | null> {
376+
if (this[kId] === Long.ZERO) {
377+
throw new MongoCursorExhaustedError();
378+
}
390379

391-
return nextAsync(this, true);
392-
}, callback);
380+
return nextAsync(this, true);
393381
}
394382

395383
/**
396384
* Try to get the next available document from the cursor or `null` if an empty batch is returned
397385
*/
398-
tryNext(): Promise<TSchema | null>;
399-
/** @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 */
400-
tryNext(callback: Callback<TSchema | null>): void;
401-
tryNext(callback?: Callback<TSchema | null>): Promise<TSchema | null> | void {
402-
return maybeCallback(async () => {
403-
if (this[kId] === Long.ZERO) {
404-
throw new MongoCursorExhaustedError();
405-
}
386+
async tryNext(): Promise<TSchema | null> {
387+
if (this[kId] === Long.ZERO) {
388+
throw new MongoCursorExhaustedError();
389+
}
406390

407-
return nextAsync(this, false);
408-
}, callback);
391+
return nextAsync(this, false);
409392
}
410393

411394
/**
@@ -414,57 +397,37 @@ export abstract class AbstractCursor<
414397
* If the iterator returns `false`, iteration will stop.
415398
*
416399
* @param iterator - The iteration callback.
417-
* @param callback - The end callback.
418400
*/
419-
forEach(iterator: (doc: TSchema) => boolean | void): Promise<void>;
420-
/** @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 */
421-
forEach(iterator: (doc: TSchema) => boolean | void, callback: Callback<void>): void;
422-
forEach(
423-
iterator: (doc: TSchema) => boolean | void,
424-
callback?: Callback<void>
425-
): Promise<void> | void {
401+
async forEach(iterator: (doc: TSchema) => boolean | void): Promise<void> {
426402
if (typeof iterator !== 'function') {
427403
throw new MongoInvalidArgumentError('Argument "iterator" must be a function');
428404
}
429-
return maybeCallback(async () => {
430-
for await (const document of this) {
431-
const result = iterator(document);
432-
if (result === false) {
433-
break;
434-
}
405+
for await (const document of this) {
406+
const result = iterator(document);
407+
if (result === false) {
408+
break;
435409
}
436-
}, callback);
410+
}
437411
}
438412

439-
close(): Promise<void>;
440-
/** @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 */
441-
close(callback: Callback): void;
442-
close(callback?: Callback): Promise<void> | void {
413+
async close(): Promise<void> {
443414
const needsToEmitClosed = !this[kClosed];
444415
this[kClosed] = true;
445-
446-
return maybeCallback(async () => cleanupCursorAsync(this, { needsToEmitClosed }), callback);
416+
await cleanupCursorAsync(this, { needsToEmitClosed });
447417
}
448418

449419
/**
450420
* Returns an array of documents. The caller is responsible for making sure that there
451421
* is enough memory to store the results. Note that the array only contains partial
452422
* results when this cursor had been previously accessed. In that case,
453423
* cursor.rewind() can be used to reset the cursor.
454-
*
455-
* @param callback - The result callback.
456424
*/
457-
toArray(): Promise<TSchema[]>;
458-
/** @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 */
459-
toArray(callback: Callback<TSchema[]>): void;
460-
toArray(callback?: Callback<TSchema[]>): Promise<TSchema[]> | void {
461-
return maybeCallback(async () => {
462-
const array = [];
463-
for await (const document of this) {
464-
array.push(document);
465-
}
466-
return array;
467-
}, callback);
425+
async toArray(): Promise<TSchema[]> {
426+
const array = [];
427+
for await (const document of this) {
428+
array.push(document);
429+
}
430+
return array;
468431
}
469432

470433
/**
@@ -903,7 +866,7 @@ class ReadableCursorStream extends Readable {
903866
}
904867

905868
override _destroy(error: Error | null, callback: (error?: Error | null) => void): void {
906-
this._cursor.close(err => process.nextTick(callback, err || error));
869+
this._cursor.close().finally(() => callback(error));
907870
}
908871

909872
private _readNext() {

src/cursor/aggregation_cursor.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,14 @@ export class AggregationCursor<TSchema = any> extends AbstractCursor<TSchema> {
7777
}
7878

7979
/** Execute the explain for the cursor */
80-
explain(): Promise<Document>;
81-
explain(verbosity: ExplainVerbosityLike): Promise<Document>;
82-
/** @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 */
83-
explain(callback: Callback): void;
84-
explain(
85-
verbosity?: ExplainVerbosityLike | Callback,
86-
callback?: Callback<Document>
87-
): Promise<Document> | void {
88-
if (typeof verbosity === 'function') (callback = verbosity), (verbosity = true);
89-
if (verbosity == null) verbosity = true;
90-
80+
explain(verbosity?: ExplainVerbosityLike): Promise<Document> {
9181
return executeOperation(
9282
this.client,
9383
new AggregateOperation(this.namespace, this[kPipeline], {
9484
...this[kOptions], // NOTE: order matters here, we may need to refine this
9585
...this.cursorOptions,
96-
explain: verbosity
97-
}),
98-
callback
86+
explain: verbosity ?? true
87+
})
9988
);
10089
}
10190

src/cursor/find_cursor.ts

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
101101
limit && limit > 0 && numReturned + batchSize > limit ? limit - numReturned : batchSize;
102102

103103
if (batchSize <= 0) {
104-
return this.close(callback);
104+
// @ts-expect-error: TODO...
105+
return this.close().finally(() => callback());
105106
}
106107
}
107108

@@ -121,58 +122,32 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
121122
* Get the count of documents for this cursor
122123
* @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead
123124
*/
124-
count(): Promise<number>;
125-
/** @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead. */
126-
count(options: CountOptions): Promise<number>;
127-
/** @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead. 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 */
128-
count(callback: Callback<number>): void;
129-
/** @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead. 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 */
130-
count(options: CountOptions, callback: Callback<number>): void;
131-
count(
132-
options?: CountOptions | Callback<number>,
133-
callback?: Callback<number>
134-
): Promise<number> | void {
125+
async count(options?: CountOptions): Promise<number> {
135126
emitWarningOnce(
136127
'cursor.count is deprecated and will be removed in the next major version, please use `collection.estimatedDocumentCount` or `collection.countDocuments` instead '
137128
);
138129
if (typeof options === 'boolean') {
139130
throw new MongoInvalidArgumentError('Invalid first parameter to count');
140131
}
141-
142-
if (typeof options === 'function') (callback = options), (options = {});
143-
options = options ?? {};
144-
145132
return executeOperation(
146133
this.client,
147134
new CountOperation(this.namespace, this[kFilter], {
148135
...this[kBuiltOptions], // NOTE: order matters here, we may need to refine this
149136
...this.cursorOptions,
150137
...options
151-
}),
152-
callback
138+
})
153139
);
154140
}
155141

156142
/** Execute the explain for the cursor */
157-
explain(): Promise<Document>;
158-
explain(verbosity?: ExplainVerbosityLike): Promise<Document>;
159-
/** @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 */
160-
explain(callback: Callback): void;
161-
explain(
162-
verbosity?: ExplainVerbosityLike | Callback,
163-
callback?: Callback<Document>
164-
): Promise<Document> | void {
165-
if (typeof verbosity === 'function') (callback = verbosity), (verbosity = true);
166-
if (verbosity == null) verbosity = true;
167-
143+
async explain(verbosity?: ExplainVerbosityLike): Promise<Document> {
168144
return executeOperation(
169145
this.client,
170146
new FindOperation(undefined, this.namespace, this[kFilter], {
171147
...this[kBuiltOptions], // NOTE: order matters here, we may need to refine this
172148
...this.cursorOptions,
173-
explain: verbosity
174-
}),
175-
callback
149+
explain: verbosity ?? true
150+
})
176151
);
177152
}
178153

0 commit comments

Comments
 (0)