Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: switch from process.nextTick() to queueMicrotask() #941

Merged
merged 7 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1270,7 +1271,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 @@ -793,7 +794,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 @@ -1954,7 +1955,9 @@ export class StatWatcher extends EventEmitter {

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

Expand Down Expand Up @@ -2103,7 +2106,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 @@ -2269,7 +2272,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