Skip to content

Commit

Permalink
Upgrade boolean args to prop bags
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofman committed Mar 15, 2022
1 parent bde223e commit df12a0c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/init/node-async_hooks-patch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { setup } from './node-async_hooks.js';

setup(true);
setup({ withDestroy: true });
17 changes: 11 additions & 6 deletions packages/init/node-async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const findAsyncSymbolsFromPromiseCreateHook = () => {
}
};

const getAsyncHookFallbackState = (promise, create) => {
const getAsyncHookFallbackState = (promise, { create = false } = {}) => {
let state = promiseAsyncHookFallbackStates.get(promise);
if (!state && create) {
state = {
Expand All @@ -150,7 +150,7 @@ const getAsyncHookFallbackState = (promise, create) => {
};

const setAsyncIdFallback = (promise, symbol, value) => {
const state = getAsyncHookFallbackState(promise, true);
const state = getAsyncHookFallbackState(promise, { create: true });

if (state[symbol]) {
if (state[symbol] !== value) {
Expand All @@ -163,7 +163,10 @@ const setAsyncIdFallback = (promise, symbol, value) => {
}
};

const getAsyncHookSymbolPromiseProtoDesc = (symbol, disallowGet) => ({
const getAsyncHookSymbolPromiseProtoDesc = (
symbol,
{ disallowGet = false } = {},
) => ({
set(value) {
if (Object.isExtensible(this)) {
Object.defineProperty(this, symbol, {
Expand All @@ -181,14 +184,14 @@ const getAsyncHookSymbolPromiseProtoDesc = (symbol, disallowGet) => ({
if (disallowGet) {
return undefined;
}
const state = getAsyncHookFallbackState(this, false);
const state = getAsyncHookFallbackState(this, { create: false });
return state && state[symbol];
},
enumerable: false,
configurable: true,
});

export const setup = (withDestroy = true) => {
export const setup = ({ withDestroy = true } = {}) => {
if (withDestroy) {
findAsyncSymbolsFromPromiseCreateHook();
} else {
Expand Down Expand Up @@ -216,7 +219,9 @@ export const setup = (withDestroy = true) => {
Object.defineProperty(
PromiseProto,
Symbol.nodeAsyncHooksDestroyed,
getAsyncHookSymbolPromiseProtoDesc(Symbol.nodeAsyncHooksDestroyed, true),
getAsyncHookSymbolPromiseProtoDesc(Symbol.nodeAsyncHooksDestroyed, {
disallowGet: true,
}),
);
} else if (withDestroy) {
// console.warn(`Couldn't find destroyed symbol to setup trap`);
Expand Down

0 comments on commit df12a0c

Please sign in to comment.