Skip to content

Commit

Permalink
Fix clearTimeout in web workers
Browse files Browse the repository at this point in the history
Closes #1732. This worked fine in Node since there clearTimeout does not care what its thisArg is. But in browsers, it does, causing the error described there.
  • Loading branch information
domenic committed Mar 12, 2017
1 parent 8838684 commit 580e557
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/jsdom/browser/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,15 +534,17 @@ function startTimer(window, startFn, stopFn, timerId, callback, ms, args) {
function stopTimer(window, id) {
const timer = window.__timers[id];
if (timer) {
timer[1].call(window, timer[0]);
// Need to .call() with undefined to ensure the thisArg is not timer itself
timer[1].call(undefined, timer[0]);
delete window.__timers[id];
}
}

function stopAllTimers(window) {
Object.keys(window.__timers).forEach(key => {
const timer = window.__timers[key];
timer[1].call(window, timer[0]);
// Need to .call() with undefined to ensure the thisArg is not timer itself
timer[1].call(undefined, timer[0]);
});
window.__timers = Object.create(null);
}
13 changes: 13 additions & 0 deletions test/jsdom/inside-worker-smoke-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,17 @@ describe("jsdom/inside-worker-smoke-tests", () => {
}
});
});

specify("clearTimeout (GH-1732)", { async: true }, t => {
const document = jsdom.jsdom(`<script>const t = setTimeout(() => {
clearTimeout(t);
window.done();
}, 100);</script>`);

const window = document.defaultView;
window.addEventListener("error", e => {
assert.ifError(e.error);
});
window.done = () => t.done();
});
});

0 comments on commit 580e557

Please sign in to comment.