browser: cache setTimeout and clearTimeout to isolate from global#58
browser: cache setTimeout and clearTimeout to isolate from global#58calvinmetcalf merged 1 commit intodefunctzombie:masterfrom
Conversation
| // shim for using process in browser | ||
|
|
||
| var process = module.exports = {}; | ||
| var cachedSetTimeout = setTimeout; |
There was a problem hiding this comment.
var setTimeout = global.setTimeout; would probably do the trick
|
I think the update can be simplified to just 2 lines that cache them with the same name, a test would be very helpful as well |
|
@calvinmetcalf of course. you mean window as global with var setTimeout = window.setTimeout syntax? otherwise global would only work with nodejs. |
|
browserify/webpack sets global to be window in the browser or self in a web worker so it's a shorthand to the global object in any of those envs |
|
actually though it wouldn't shock me if this was used in some env that didn't give global, so your way is probably good. a comment explaining this is probably a good idea. |
|
@calvinmetcalf js world does not shock me anymore :). is it good this way? I couldn't come up with a test case though any suggestions welcome |
browser.js
Outdated
|
|
||
| var process = module.exports = {}; | ||
|
|
||
| // borrowed from `window` since `global` may not be provided in all environments. |
There was a problem hiding this comment.
maybe something like cached from whatever global is present so that test runners that stub it don't break things
|
test for you describe('rename globals', function (t) {
it('throws an eroror', function (done){
var oldTimeout = setTimeout;
setTimeout = function () {
setTimeout = oldTimeout;
assert.ok(false);
done();
}
ourProcess.nextTick(function () {
assert.ok(true);
setTimeout = oldTimeout;
done();
});
});
}); |
|
right! I've added some tests also for clearTimeout |
|
0.11.4 |
|
thanks! @calvinmetcalf fyi, setting clearTimeout to original one inside nextTick for cleanup has a side effect that makes tests to do not complain when clearTimeout replaced with original one because of execution order inside drainQueue() |
|
oh i see fixed 53fdf64 |
tl;dr:
sinon stubs time related global objects for unit testing and this breaks all packages depended on process.nextTick. caching setTimeout and clearTimeout at bootstrap protects against mutations.
related issue:
sinonjs/fake-timers#66