Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1201@patch: Fixes problem with clearTimeout(), clearInterval() and c… #1202

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/happy-dom/src/window/BrowserWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const ORIGINAL_CLEAR_TIMEOUT = clearTimeout;
const ORIGINAL_SET_INTERVAL = setInterval;
const ORIGINAL_CLEAR_INTERVAL = clearInterval;
const ORIGINAL_QUEUE_MICROTASK = queueMicrotask;
const IS_NODE_JS_TIMEOUT_ENVIRONMENT = setTimeout.toString().includes('new Timeout');

/**
* Browser window.
Expand Down Expand Up @@ -978,6 +979,11 @@ export default class BrowserWindow extends EventTarget implements IBrowserWindow
* @param id ID of the timeout.
*/
public clearTimeout(id: NodeJS.Timeout): void {
// We need to make sure that the ID is a Timeout object, otherwise Node.js might throw an error.
// This is only necessary if we are in a Node.js environment.
if (IS_NODE_JS_TIMEOUT_ENVIRONMENT && (!id || id.constructor.name !== 'Timeout')) {
return;
}
this.#clearTimeout(id);
this.#browserFrame[PropertySymbol.asyncTaskManager].endTimer(id);
}
Expand Down Expand Up @@ -1017,6 +1023,11 @@ export default class BrowserWindow extends EventTarget implements IBrowserWindow
* @param id ID of the interval.
*/
public clearInterval(id: NodeJS.Timeout): void {
// We need to make sure that the ID is a Timeout object, otherwise Node.js might throw an error.
// This is only necessary if we are in a Node.js environment.
if (IS_NODE_JS_TIMEOUT_ENVIRONMENT && (!id || id.constructor.name !== 'Timeout')) {
return;
}
this.#clearInterval(id);
this.#browserFrame[PropertySymbol.asyncTaskManager].endTimer(id);
}
Expand Down Expand Up @@ -1051,6 +1062,11 @@ export default class BrowserWindow extends EventTarget implements IBrowserWindow
* @param id ID.
*/
public cancelAnimationFrame(id: NodeJS.Immediate): void {
// We need to make sure that the ID is an Immediate object, otherwise Node.js might throw an error.
// This is only necessary if we are in a Node.js environment.
if (IS_NODE_JS_TIMEOUT_ENVIRONMENT && (!id || id.constructor.name !== 'Immediate')) {
return;
}
global.clearImmediate(id);
this.#browserFrame[PropertySymbol.asyncTaskManager].endImmediate(id);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/happy-dom/test/window/BrowserWindow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,10 @@ describe('BrowserWindow', () => {
});
window.clearTimeout(timeoutId);
});

it('Supports number values.', () => {
window.clearTimeout(<NodeJS.Timeout>(<unknown>-1));
});
});

describe('setInterval()', () => {
Expand Down Expand Up @@ -863,6 +867,10 @@ describe('BrowserWindow', () => {
});
window.clearInterval(intervalId);
});

it('Supports number values.', () => {
window.clearInterval(<NodeJS.Timeout>(<unknown>-1));
});
});

describe('requestAnimationFrame()', () => {
Expand Down Expand Up @@ -922,6 +930,10 @@ describe('BrowserWindow', () => {
});
window.cancelAnimationFrame(timeoutId);
});

it('Supports number values.', () => {
window.cancelAnimationFrame(<NodeJS.Immediate>(<unknown>-1));
});
});

describe('matchMedia()', () => {
Expand Down
Loading