Skip to content

browser: cache setTimeout and clearTimeout to isolate from global#58

Merged
calvinmetcalf merged 1 commit intodefunctzombie:masterfrom
ilgooz:timeout
Jun 10, 2016
Merged

browser: cache setTimeout and clearTimeout to isolate from global#58
calvinmetcalf merged 1 commit intodefunctzombie:masterfrom
ilgooz:timeout

Conversation

@ilgooz
Copy link
Copy Markdown
Contributor

@ilgooz ilgooz commented Jun 9, 2016

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

// shim for using process in browser

var process = module.exports = {};
var cachedSetTimeout = setTimeout;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var setTimeout = global.setTimeout; would probably do the trick

@calvinmetcalf
Copy link
Copy Markdown
Collaborator

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

@ilgooz
Copy link
Copy Markdown
Contributor Author

ilgooz commented Jun 9, 2016

@calvinmetcalf of course. you mean window as global with var setTimeout = window.setTimeout syntax? otherwise global would only work with nodejs.

@calvinmetcalf
Copy link
Copy Markdown
Collaborator

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

@calvinmetcalf
Copy link
Copy Markdown
Collaborator

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.

@ilgooz
Copy link
Copy Markdown
Contributor Author

ilgooz commented Jun 9, 2016

@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.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe something like cached from whatever global is present so that test runners that stub it don't break things

@ilgooz
Copy link
Copy Markdown
Contributor Author

ilgooz commented Jun 9, 2016

@calvinmetcalf 👍

@calvinmetcalf
Copy link
Copy Markdown
Collaborator

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();
          });
        });
      });

@ilgooz
Copy link
Copy Markdown
Contributor Author

ilgooz commented Jun 10, 2016

right! I've added some tests also for clearTimeout

@calvinmetcalf calvinmetcalf merged commit e481e47 into defunctzombie:master Jun 10, 2016
@calvinmetcalf
Copy link
Copy Markdown
Collaborator

0.11.4

@ilgooz
Copy link
Copy Markdown
Contributor Author

ilgooz commented Jun 10, 2016

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()

@calvinmetcalf
Copy link
Copy Markdown
Collaborator

oh i see fixed 53fdf64

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants