Skip to content

Commit

Permalink
#1102@patch: Improves logic in whenAsyncComplete, to ensure that the …
Browse files Browse the repository at this point in the history
…Promise is resolved after all async tasks.
  • Loading branch information
capricorn86 committed Sep 30, 2023
1 parent 9700666 commit 2c1ff00
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions packages/happy-dom/src/async-task-manager/AsyncTaskManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class AsyncTaskManager {
private runningTasks: { [k: string]: () => void } = {};
private runningTimers: NodeJS.Timeout[] = [];
private callbacks: Array<() => void> = [];
private callbackTimeout: NodeJS.Timeout | null = null;

/**
* Returns a promise that is fulfilled when async tasks are complete.
Expand Down Expand Up @@ -36,6 +37,10 @@ export default class AsyncTaskManager {
*/
public startTimer(timerID: NodeJS.Timeout): void {
this.runningTimers.push(timerID);
if (this.callbackTimeout) {
global.clearTimeout(this.callbackTimeout);
this.callbackTimeout = null;
}
}

/**
Expand All @@ -48,6 +53,10 @@ export default class AsyncTaskManager {
if (index !== -1) {
this.runningTimers.splice(index, 1);
}
if (this.callbackTimeout) {
global.clearTimeout(this.callbackTimeout);
this.callbackTimeout = null;
}
if (!Object.keys(this.runningTasks).length && !this.runningTimers.length) {
this.endAll();
}
Expand All @@ -62,6 +71,10 @@ export default class AsyncTaskManager {
public startTask(abortHandler?: () => void): number {
const taskID = this.newTaskID();
this.runningTasks[taskID] = abortHandler ? abortHandler : () => {};
if (this.callbackTimeout) {
global.clearTimeout(this.callbackTimeout);
this.callbackTimeout = null;
}
return taskID;
}

Expand All @@ -74,6 +87,10 @@ export default class AsyncTaskManager {
if (this.runningTasks[taskID]) {
delete this.runningTasks[taskID];
}
if (this.callbackTimeout) {
global.clearTimeout(this.callbackTimeout);
this.callbackTimeout = null;
}
if (!Object.keys(this.runningTasks).length && !this.runningTimers.length) {
this.endAll();
}
Expand Down Expand Up @@ -118,6 +135,10 @@ export default class AsyncTaskManager {
runningTasks[key]();
}

if (this.callbackTimeout) {
global.clearTimeout(this.callbackTimeout);
this.callbackTimeout = null;
}
if (this.callbacks.length) {
if (canceled) {
const callbacks = this.callbacks;
Expand All @@ -126,19 +147,15 @@ export default class AsyncTaskManager {
callback();
}
} else {
const timerID = global.setTimeout(() => {
if (!Object.keys(this.runningTasks).length && this.runningTimers.length === 1) {
const callbacks = this.callbacks;
this.callbacks = [];
this.runningTimers = [];
for (const callback of callbacks) {
callback();
}
} else {
this.endTimer(timerID);
this.callbackTimeout = global.setTimeout(() => {
const callbacks = this.callbacks;
this.callbackTimeout = null;
this.callbacks = [];
this.runningTimers = [];
for (const callback of callbacks) {
callback();
}
}, 10);
this.startTimer(timerID);
}
}
}
Expand Down

0 comments on commit 2c1ff00

Please sign in to comment.