Skip to content

Commit

Permalink
refine promise timings
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Oct 7, 2019
1 parent ae46f45 commit 96dd1b9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
22 changes: 13 additions & 9 deletions lib/internal/async_hooks.js
Expand Up @@ -289,6 +289,18 @@ function bufferBootstrapHooks() {
}
}

function clearBootstrapHooksBuffer() {
if (!bootstrapBuffer)
return;
if (bootstrapHooks) {
stopBootstrapHooksBuffer();
process._tickCallback();
}
const _bootstrapBuffer = bootstrapBuffer;
bootstrapBuffer = null;
return _bootstrapBuffer;
}

function stopBootstrapHooksBuffer() {
if (!bootstrapHooks)
return;
Expand All @@ -302,18 +314,11 @@ function stopBootstrapHooksBuffer() {
bootstrapHooks = null;
if (async_hook_fields[kTotals] === 0) {
disableHooks();
// Flush microtasks to ensure disable has run.
process._tickCallback();
}
}

function clearBootstrapHooksBuffer() {
if (bootstrapHooks)
stopBootstrapHooksBuffer();
bootstrapBuffer = null;
}

function emitBootstrapHooksBuffer() {
const bootstrapBuffer = clearBootstrapHooksBuffer();
if (!bootstrapBuffer || async_hook_fields[kTotals] === 0) {
return;
}
Expand All @@ -333,7 +338,6 @@ function emitBootstrapHooksBuffer() {
break;
}
}
bootstrapBuffer = null;
}

let wantPromiseHook = false;
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/modules/cjs/loader.js
Expand Up @@ -821,10 +821,12 @@ Module.prototype.load = function(filename) {
if (module !== undefined && module.module !== undefined) {
if (module.module.getStatus() >= kInstantiated)
module.module.setExport('default', exports);
} else { // preemptively cache
} else {
// Preemptively cache
// We use a function to defer promise creation for async hooks.
ESMLoader.moduleMap.set(
url,
new ModuleJob(ESMLoader, url, () =>
() => new ModuleJob(ESMLoader, url, () =>
new ModuleWrap(function() {
this.setExport('default', exports);
}, ['default'], url)
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/modules/esm/loader.js
Expand Up @@ -146,6 +146,9 @@ class Loader {
async getModuleJob(specifier, parentURL) {
const { url, format } = await this.resolve(specifier, parentURL);
let job = this.moduleMap.get(url);
// CJS injects jobs as functions to defer promise creation for async hooks.
if (typeof job === 'function')
this.moduleMap.set(url, job = job());
if (job !== undefined)
return job;

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/module_map.js
Expand Up @@ -16,7 +16,7 @@ class ModuleMap extends SafeMap {
}
set(url, job) {
validateString(url, 'url');
if (job instanceof ModuleJob !== true) {
if (job instanceof ModuleJob !== true && typeof job !== 'function') {
throw new ERR_INVALID_ARG_TYPE('job', 'ModuleJob', job);
}
debug(`Storing ${url} in ModuleMap`);
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-internal-module-map-asserts.js
Expand Up @@ -12,7 +12,7 @@ const ModuleMap = require('internal/modules/esm/module_map');
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "url" argument must be of type string/
}, 15);
}, 12);

const moduleMap = new ModuleMap();

Expand All @@ -21,7 +21,7 @@ const ModuleMap = require('internal/modules/esm/module_map');
// but I think it's useless, and was not simple to mock...
const job = undefined;

[{}, [], true, 1, () => {}].forEach((value) => {
[{}, [], true, 1].forEach((value) => {
assert.throws(() => moduleMap.get(value), errorReg);
assert.throws(() => moduleMap.has(value), errorReg);
assert.throws(() => moduleMap.set(value, job), errorReg);
Expand All @@ -34,11 +34,11 @@ const ModuleMap = require('internal/modules/esm/module_map');
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "job" argument must be of type ModuleJob/
}, 5);
}, 4);

const moduleMap = new ModuleMap();

[{}, [], true, 1, () => {}].forEach((value) => {
[{}, [], true, 1].forEach((value) => {
assert.throws(() => moduleMap.set('', value), errorReg);
});
}

0 comments on commit 96dd1b9

Please sign in to comment.