Skip to content

Commit

Permalink
Revert "use symbols instead of sets"
Browse files Browse the repository at this point in the history
This reverts commit 6d5375c.
  • Loading branch information
leonardoventurini committed Nov 30, 2023
1 parent 6d5375c commit bd46806
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions src/await-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
AsyncLocalStorage,
createHook,
executionAsyncId,
executionAsyncResource,
} from 'async_hooks';

type AsyncCallback = (asyncId: number, triggerAsyncId: number) => void;
Expand All @@ -15,11 +14,11 @@ export class AwaitDetector {
static Storage = new AsyncLocalStorage();
static IgnoreStorage = new AsyncLocalStorage();
static Symbol = Symbol('AsyncDetector');
static IsAwait = Symbol('IsAwait');
static IsAsyncFunction = Symbol('IsAsyncFunction');

start = Date.now();

asyncFunctions = new Set();
awaits = new Set();
afterAwaits = new Map();
ignoreNextPromise = 0;

Expand Down Expand Up @@ -49,6 +48,7 @@ export class AwaitDetector {
this.hook = createHook({
init: this.init.bind(this),
before: this.before.bind(this),
destroy: this.destroy.bind(this),
promiseResolve: this.promiseResolve.bind(this),
});

Expand Down Expand Up @@ -114,7 +114,7 @@ export class AwaitDetector {
);
}

init(asyncId: number, type: string, triggerAsyncId: number, resource: any) {
init(asyncId: number, type: string, triggerAsyncId: number) {
if (type !== 'PROMISE') {
return;
}
Expand All @@ -133,21 +133,19 @@ export class AwaitDetector {
const isAsyncFunction = triggerAsyncId === executionAsyncId();

if (isAsyncFunction) {
resource[AwaitDetector.IsAsyncFunction] = true;
this.asyncFunctions.add(asyncId);
this.log(`${type}(${asyncId}): async function start`);
return;
}

const triggerResource = executionAsyncResource() as any;

if (triggerResource[AwaitDetector.IsAsyncFunction]) {
if (this.asyncFunctions.has(triggerAsyncId)) {
this.onAwaitStart(asyncId, triggerAsyncId);
resource[AwaitDetector.IsAwait] = true;
this.awaits.add(asyncId);
this.awaitData.set(asyncId, [triggerAsyncId]);
this.log(
`${type}(${asyncId}): await start - async function: ${triggerAsyncId}`,
);
} else if (triggerResource[AwaitDetector.IsAwait]) {
} else if (this.awaits.has(triggerAsyncId)) {
this.afterAwaits.set(asyncId, triggerAsyncId);
}
}
Expand All @@ -157,8 +155,6 @@ export class AwaitDetector {
return;
}

const resource = executionAsyncResource() as any;

if (this.afterAwaits.has(asyncId)) {
const awaitAsyncId = this.afterAwaits.get(asyncId);

Expand All @@ -168,7 +164,7 @@ export class AwaitDetector {
this.awaitData.delete(awaitAsyncId);
// Awaited a thenable or non-promise value
this.log(`await end: ${this.afterAwaits.get(asyncId)} (A)`);
} else if (resource[AwaitDetector.IsAwait]) {
} else if (this.awaits.has(asyncId)) {
if (!this.awaitData.has(asyncId)) return;
const [triggerAsyncId] = this.awaitData.get(asyncId) as [number];
this.onAwaitEnd(asyncId, triggerAsyncId);
Expand All @@ -178,12 +174,31 @@ export class AwaitDetector {
}
}

destroy(asyncId: number) {
if (!this.isWithinContext()) {
return;
}

if (this.asyncFunctions.has(asyncId)) {
this.asyncFunctions.delete(asyncId);
return;
}

if (this.awaits.has(asyncId)) {
this.awaits.delete(asyncId);
}
}

promiseResolve(asyncId: number) {
if (!this.isWithinContext()) {
return;
}

if (this.afterAwaits.has(asyncId)) {
if (this.asyncFunctions.has(asyncId)) {
this.asyncFunctions.delete(asyncId);
} else if (this.awaits.has(asyncId)) {
this.awaits.delete(asyncId); // Added later
} else if (this.afterAwaits.has(asyncId)) {
this.log(`promise resolve: ${asyncId} (C)`);
this.afterAwaits.delete(asyncId); // Added later
}
Expand Down

0 comments on commit bd46806

Please sign in to comment.