Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit 5119d84

Browse files
committed
dezalgo over maybeCallback as a single point of nextTick
1 parent 60c087e commit 5119d84

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

prelude/bootstrap.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-multi-spaces */
22
/* eslint-disable no-underscore-dangle */
33
/* eslint-disable prefer-rest-params */
4+
/* eslint-disable prefer-spread */
45

56
/* global EXECPATH_FD */
67
/* global PAYLOAD_POSITION */
@@ -162,9 +163,33 @@ function findNativeAddon (path) {
162163
}
163164

164165
// /////////////////////////////////////////////////////////////////
165-
// PAYLOAD /////////////////////////////////////////////////////////
166+
// FLOW UTILS //////////////////////////////////////////////////////
166167
// /////////////////////////////////////////////////////////////////
167168

169+
function asap (cb) {
170+
process.nextTick(cb);
171+
}
172+
173+
function dezalgo (cb) {
174+
if (!cb) return cb;
175+
176+
var sync = true;
177+
asap(function () {
178+
sync = false;
179+
});
180+
181+
return function zalgoSafe () {
182+
var args = arguments;
183+
if (sync) {
184+
asap(function () {
185+
cb.apply(undefined, args);
186+
});
187+
} else {
188+
cb.apply(undefined, args);
189+
}
190+
};
191+
}
192+
168193
function rethrow (error) {
169194
if (error) throw error;
170195
}
@@ -174,6 +199,10 @@ function rethrowBytesRead (error, bytesRead) {
174199
return bytesRead;
175200
}
176201

202+
// /////////////////////////////////////////////////////////////////
203+
// PAYLOAD /////////////////////////////////////////////////////////
204+
// /////////////////////////////////////////////////////////////////
205+
177206
function payloadCopy (source, target, targetStart, sourceStart, sourceEnd, cb) {
178207
var cb2 = cb || rethrowBytesRead;
179208
if (sourceStart >= source[1]) return cb2(null, 0);
@@ -636,7 +665,7 @@ var modifyNativeAddonWin32 = (function () {
636665
return ancestor.read.apply(fs, arguments);
637666
}
638667

639-
var callback = maybeCallback(arguments);
668+
var callback = dezalgo(maybeCallback(arguments));
640669
readFromSnapshot(fd, buffer, offset, length, position, callback);
641670
};
642671

0 commit comments

Comments
 (0)