Skip to content

Commit

Permalink
Merge pull request #1202 from capricorn86/task/1201-cleartimeout-does…
Browse files Browse the repository at this point in the history
…nt-support-number-values

#1201@patch: Fixes problem with clearTimeout(), clearInterval() and c…
  • Loading branch information
capricorn86 committed Jan 12, 2024
2 parents 34e4ca8 + 5ef720b commit 15a7182
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/happy-dom/src/window/BrowserWindow.ts
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
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

0 comments on commit 15a7182

Please sign in to comment.