Skip to content

Commit

Permalink
Merge pull request #941 from SukkaW/queue-microtask
Browse files Browse the repository at this point in the history
refactor: switch from `process.nextTick()` to `queueMicrotask()`
  • Loading branch information
streamich authored Oct 7, 2023
2 parents 7fc1f7e + cd919f6 commit c955827
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 15 deletions.
4 changes: 0 additions & 4 deletions src/__tests__/process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ describe('process', () => {
it('.cwd()', () => {
expect(typeof proc.cwd()).toBe('string');
});
it('.nextTick()', done => {
expect(typeof proc.nextTick).toBe('function');
proc.nextTick(done);
});
it('.env', () => {
expect(typeof proc.env).toBe('object');
});
Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/queueMicrotask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import queueMicrotask from '../queueMicrotask';

describe('queueMicrotask', () => {
it('Is a function', () => {
expect(typeof queueMicrotask).toBe('function');
});
it('Execute callback on next event loop cycle', done => {
queueMicrotask(done);
});
});
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';
import { constants } from '../constants';

const { O_RDWR, O_SYMLINK } = constants;
Expand Down Expand Up @@ -1361,7 +1362,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
5 changes: 4 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,9 @@ 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
2 changes: 0 additions & 2 deletions src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export interface IProcess {
cwd(): string;

platform: string;
nextTick: (callback: (...args) => void, ...args) => void;
emitWarning: (message: string, type: string) => void;
env: {};
}
Expand Down Expand Up @@ -39,7 +38,6 @@ export function createProcess(): IProcess {
const p: IProcess = maybeReturnProcess() || ({} as IProcess);

if (!p.cwd) p.cwd = () => '/';
if (!p.nextTick) p.nextTick = require('./setImmediate').default;
if (!p.emitWarning)
p.emitWarning = (message, type) => {
// tslint:disable-next-line:no-console
Expand Down
4 changes: 4 additions & 0 deletions src/queueMicrotask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default typeof queueMicrotask === 'function' ? queueMicrotask : <typeof queueMicrotask>(cb =>
Promise.resolve()
.then(() => cb())
.catch(() => {}));
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 @@ -803,7 +804,7 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {

// 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 @@ -2059,7 +2060,9 @@ export class StatWatcher extends EventEmitter {

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

Expand Down Expand Up @@ -2208,7 +2211,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 @@ -2374,7 +2377,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 c955827

Please sign in to comment.