Skip to content

Commit 95aff1e

Browse files
committed
feat(NODE-7308): replace process.nextTick with queueMicrotask
1 parent 0910bdf commit 95aff1e

File tree

9 files changed

+22
-22
lines changed

9 files changed

+22
-22
lines changed

src/cmap/connection_pool.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
231231
this.mongoLogger = this.server.topology.client?.mongoLogger;
232232
this.component = 'connection';
233233

234-
process.nextTick(() => {
234+
queueMicrotask(() => {
235235
this.emitAndLog(ConnectionPool.CONNECTION_POOL_CREATED, new ConnectionPoolCreatedEvent(this));
236236
});
237237
}
@@ -342,7 +342,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
342342
});
343343

344344
this.waitQueue.push(waitQueueMember);
345-
process.nextTick(() => this.processWaitQueue());
345+
queueMicrotask(() => this.processWaitQueue());
346346

347347
try {
348348
timeout?.throwIfExpired();
@@ -405,7 +405,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
405405
this.destroyConnection(connection, reason);
406406
}
407407

408-
process.nextTick(() => this.processWaitQueue());
408+
queueMicrotask(() => this.processWaitQueue());
409409
}
410410

411411
/**
@@ -461,7 +461,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
461461
}
462462

463463
if (interruptInUseConnections) {
464-
process.nextTick(() => this.interruptInUseConnections(oldGeneration));
464+
queueMicrotask(() => this.interruptInUseConnections(oldGeneration));
465465
}
466466

467467
this.processWaitQueue();
@@ -702,7 +702,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
702702
this.createConnection((err, connection) => {
703703
if (!err && connection) {
704704
this.connections.push(connection);
705-
process.nextTick(() => this.processWaitQueue());
705+
queueMicrotask(() => this.processWaitQueue());
706706
}
707707
if (this.poolState === PoolState.ready) {
708708
clearTimeout(this.minPoolSizeTimer);
@@ -809,7 +809,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
809809
waitQueueMember.resolve(connection);
810810
}
811811
}
812-
process.nextTick(() => this.processWaitQueue());
812+
queueMicrotask(() => this.processWaitQueue());
813813
});
814814
}
815815
this.processingWaitQueue = false;

src/gridfs/upload.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class GridFSBucketWriteStream extends Writable {
165165
}
166166
);
167167
} else {
168-
return process.nextTick(callback);
168+
return queueMicrotask(callback);
169169
}
170170
}
171171

@@ -188,7 +188,7 @@ export class GridFSBucketWriteStream extends Writable {
188188
/** @internal */
189189
override _final(callback: (error?: Error | null) => void): void {
190190
if (this.state.streamEnd) {
191-
return process.nextTick(callback);
191+
return queueMicrotask(callback);
192192
}
193193
this.state.streamEnd = true;
194194
writeRemnant(this, callback);
@@ -220,11 +220,11 @@ export class GridFSBucketWriteStream extends Writable {
220220

221221
function handleError(stream: GridFSBucketWriteStream, error: Error, callback: Callback): void {
222222
if (stream.state.errored) {
223-
process.nextTick(callback);
223+
queueMicrotask(callback);
224224
return;
225225
}
226226
stream.state.errored = true;
227-
process.nextTick(callback, error);
227+
queueMicrotask(() => callback(error));
228228
}
229229

230230
function createChunkDoc(filesId: ObjectId, n: number, data: Buffer): GridFSChunk {
@@ -283,7 +283,7 @@ async function checkChunksIndex(stream: GridFSBucketWriteStream): Promise<void>
283283

284284
function checkDone(stream: GridFSBucketWriteStream, callback: Callback): void {
285285
if (stream.done) {
286-
return process.nextTick(callback);
286+
return queueMicrotask(callback);
287287
}
288288

289289
if (stream.state.streamEnd && stream.state.outstandingRequests === 0 && !stream.state.errored) {
@@ -327,7 +327,7 @@ function checkDone(stream: GridFSBucketWriteStream, callback: Callback): void {
327327
return;
328328
}
329329

330-
process.nextTick(callback);
330+
queueMicrotask(callback);
331331
}
332332

333333
async function checkIndexes(stream: GridFSBucketWriteStream): Promise<void> {
@@ -425,7 +425,7 @@ function doWrite(
425425
if (stream.pos + inputBuf.length < stream.chunkSizeBytes) {
426426
inputBuf.copy(stream.bufToStore, stream.pos);
427427
stream.pos += inputBuf.length;
428-
process.nextTick(callback);
428+
queueMicrotask(callback);
429429
return;
430430
}
431431

@@ -530,7 +530,7 @@ function writeRemnant(stream: GridFSBucketWriteStream, callback: Callback): void
530530

531531
function isAborted(stream: GridFSBucketWriteStream, callback: Callback<void>): boolean {
532532
if (stream.state.aborted) {
533-
process.nextTick(callback, new MongoAPIError('Stream has been aborted'));
533+
queueMicrotask(() => callback(new MongoAPIError('Stream has been aborted')));
534534
return true;
535535
}
536536
return false;

src/sdam/monitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
426426
function monitorServer(monitor: Monitor) {
427427
return (callback: Callback) => {
428428
if (monitor.s.state === STATE_MONITORING) {
429-
process.nextTick(callback);
429+
queueMicrotask(callback);
430430
return;
431431
}
432432
stateTransition(monitor, STATE_MONITORING);

src/sdam/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
414414
error.addErrorLabel(MongoErrorLabel.ResetPool);
415415
}
416416
markServerUnknown(this, error);
417-
process.nextTick(() => this.requestCheck());
417+
queueMicrotask(() => this.requestCheck());
418418
return;
419419
}
420420

src/sdam/topology.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ function processWaitQueue(topology: Topology) {
10761076
if (topology.waitQueue.length > 0) {
10771077
// ensure all server monitors attempt monitoring soon
10781078
for (const [, server] of topology.s.servers) {
1079-
process.nextTick(function scheduleServerCheck() {
1079+
queueMicrotask(function scheduleServerCheck() {
10801080
return server.requestCheck();
10811081
});
10821082
}

test/tools/runner/ee_checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ events.EventEmitter = class RequireErrorListenerEventEmitter extends EventEmitte
99
super(...args);
1010
const ctorCallSite = new Error('EventEmitter must add an error listener synchronously');
1111
ctorCallSite.stack;
12-
process.nextTick(() => {
12+
queueMicrotask(() => {
1313
const isChangeStream = this.constructor.name
1414
.toLowerCase()
1515
.includes('ChangeStream'.toLowerCase());

test/tools/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export const sleep = promisify(setTimeout);
166166
* If you are using sinon fake timers, it can end up blocking queued IO from running
167167
* awaiting a nextTick call will allow the event loop to process Networking/FS callbacks
168168
*/
169-
export const processTick = () => new Promise(resolve => process.nextTick(resolve));
169+
export const processTick = () => new Promise(resolve => queueMicrotask(resolve));
170170

171171
export function getIndicesOfAuthInUrl(connectionString: string | string[]) {
172172
const doubleSlashIndex = connectionString.indexOf('//');

test/unit/assorted/polling_srv_records_for_mongos_discovery.prose.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe('Polling Srv Records for Mongos Discovery', () => {
130130
expect(topology.description).to.have.property('type', TopologyType.Sharded);
131131
const servers = Array.from(topology.description.servers.keys());
132132
expect(servers).to.deep.equal(srvAddresses(recordSets[0]));
133-
process.nextTick(() => srvPoller.trigger(recordSets[1]));
133+
queueMicrotask(() => srvPoller.trigger(recordSets[1]));
134134

135135
await once(topology, 'topologyDescriptionChanged');
136136

@@ -296,7 +296,7 @@ describe('Polling Srv Records for Mongos Discovery', () => {
296296
const callback = args[args.length - 1] as (err: null, address: string, family: 4) => void;
297297

298298
if (hostname.includes('test.mock.test.build.10gen.cc')) {
299-
return process.nextTick(callback, null, '127.0.0.1', 4);
299+
return queueMicrotask(() => callback(null, '127.0.0.1', 4));
300300
}
301301

302302
const { wrappedMethod: lookup } = lookupStub;

test/unit/cmap/connect.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('Connect Tests', function () {
157157
const cancellationToken = new CancellationToken();
158158
// Make sure the cancel listener is added before emitting cancel
159159
cancellationToken.addListener('newListener', () => {
160-
process.nextTick(() => {
160+
queueMicrotask(() => {
161161
cancellationToken.emit('cancel');
162162
});
163163
});

0 commit comments

Comments
 (0)