Skip to content

Commit

Permalink
refactor: replace process.nextTick with queueMicrotask
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Aug 17, 2023
1 parent 45d1784 commit 09b360a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Volume, filenameToSteps, StatWatcher } from '../volume';
import hasBigInt from './hasBigInt';
import { tryGetChild, tryGetChildNode } from './util';
import { genRndStr6 } from '../node/util';
import queueMicrotask from '../queueMicrotask';

describe('volume', () => {
describe('filenameToSteps(filename): string[]', () => {
Expand Down Expand Up @@ -1215,7 +1216,7 @@ describe('volume', () => {
vol.writeFileSync('/lol.txt', '1');
setTimeout(() => {
vol.watchFile('/lol.txt', { interval: 1 }, (curr, prev) => {
process.nextTick(() => {
queueMicrotask(() => {
vol.unwatchFile('/lol.txt');
done();
});
Expand Down
4 changes: 2 additions & 2 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FsaNodeDirent } from './FsaNodeDirent';
import { AMODE } from '../consts/AMODE';
import { constants } from '../constants';
import { FsaNodeStats } from './FsaNodeStats';
import process from '../process';
import queueMicrotask from '../queueMicrotask';
import { FsSynchronousApi } from '../node/types/FsSynchronousApi';
import { FsaNodeWriteStream } from './FsaNodeWriteStream';
import { FsaNodeReadStream } from './FsaNodeReadStream';
Expand Down Expand Up @@ -84,7 +84,7 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
util.validateCallback(callback);
// This `if` branch is from Node.js
if (length === 0) {
return process.nextTick(() => {
return queueMicrotask(() => {
if (callback) callback(null, 0, buffer);
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/fsa-to-node/FsaNodeWriteStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { concurrency } from 'thingies/es6/concurrency';
import { flagsToNumber } from '../node/util';
import { FLAG } from '../consts/FLAG';
import { FsaNodeFsOpenFile } from './FsaNodeFsOpenFile';
import queueMicrotask from '../queueMicrotask';
import type { IFileSystemWritableFileStream } from '../fsa/types';
import type { IWriteStream } from '../node/types/misc';
import type { IWriteStreamOptions } from '../node/types/options';
Expand Down Expand Up @@ -86,7 +87,7 @@ export class FsaNodeWriteStream extends Writable implements IWriteStream {
const emitClose = this.options.emitClose;
await this.__mutex__(async () => {
if (this.__closed__ && emitClose) {
process.nextTick(() => this.emit('close'));
queueMicrotask(() => this.emit('close'));
return;
}
try {
Expand Down
3 changes: 2 additions & 1 deletion src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { FsCallbackApi } from './types';
import type * as misc from './types/misc';
import { ENCODING_UTF8, TEncodingExtended } from '../encoding';
import { bufferFrom } from '../internal/buffer';
import queueMicrotask from '../queueMicrotask';

export const isWin = process.platform === 'win32';

Expand Down Expand Up @@ -44,7 +45,7 @@ export function nullCheck(path, callback?) {
const er = new Error('Path must be a string without null bytes');
(er as any).code = 'ENOENT';
if (typeof callback !== 'function') throw er;
process.nextTick(callback, er);
queueMicrotask(() => { callback(er); });
return false;
}
return true;
Expand Down
11 changes: 7 additions & 4 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Stats from './Stats';
import Dirent from './Dirent';
import { Buffer, bufferAllocUnsafe, bufferFrom } from './internal/buffer';
import setImmediate from './setImmediate';
import queueMicrotask from './queueMicrotask';
import process from './process';
import setTimeoutUnref, { TSetTimeout } from './setTimeoutUnref';
import { Readable, Writable } from 'stream';
Expand Down Expand Up @@ -788,7 +789,7 @@ export class Volume implements FsCallbackApi {

// This `if` branch is from Node.js
if (length === 0) {
return process.nextTick(() => {
return queueMicrotask(() => {
if (callback) callback(null, 0, buffer);
});
}
Expand Down Expand Up @@ -1946,7 +1947,9 @@ export class StatWatcher extends EventEmitter {

stop() {
clearTimeout(this.timeoutRef);
process.nextTick(emitStop, this);
queueMicrotask(() => {
emitStop.call(this, this);
});
}
}

Expand Down Expand Up @@ -2095,7 +2098,7 @@ FsReadStream.prototype.close = function (cb) {
this.once('open', closeOnOpen);
return;
}
return process.nextTick(() => this.emit('close'));
return queueMicrotask(() => this.emit('close'));
}

// Since Node 18, there is only a getter for '.closed'.
Expand Down Expand Up @@ -2261,7 +2264,7 @@ FsWriteStream.prototype.close = function (cb) {
this.once('open', closeOnOpen);
return;
}
return process.nextTick(() => this.emit('close'));
return queueMicrotask(() => this.emit('close'));
}

// Since Node 18, there is only a getter for '.closed'.
Expand Down

0 comments on commit 09b360a

Please sign in to comment.