Skip to content

Conversation

@mgol
Copy link
Member

@mgol mgol commented Jun 15, 2015

Fixes gh-2177

Alternative to #2391 & #2392. +40 bytes.

Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary since the globals are only removed for the built file?

Copy link
Member Author

Choose a reason for hiding this comment

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

I removed clearInterval from JSHint globals but this is the one place where it has to appear.

Copy link
Member

Choose a reason for hiding this comment

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

Of course. I was reading it all backwards.

@mgol mgol force-pushed the window-settimeout3 branch from 9bd14d2 to 3f5d1ea Compare June 15, 2015 17:40
@mgol mgol force-pushed the window-settimeout3 branch from 3f5d1ea to 1d11b30 Compare June 15, 2015 18:29
Copy link
Member

Choose a reason for hiding this comment

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

It looks weird without context, i would suggest add descriptive comments to each of those new files

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a long comment, see https://github.com/mzgol/jquery/blob/7eef60f491be191e5844f3a3420bee238c7d73f3/src/intro.js#L19-L25. Should I repeat this comment in every of those 4 files?

Copy link
Member

Choose a reason for hiding this comment

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

I'd say link should suffice

Copy link
Member Author

Choose a reason for hiding this comment

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

But link to what exactly? I linked to my first PR, this is the third where there is no central place to put it.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, then it would duplicate it everywhere :-(, would be really awkward though, or maybe just link to this pull would be enough?

@markelog
Copy link
Member

I still question legitimacy of that ticket, we didn't receive any real issues about that and it seems only one reason why we need to do that is

However, some environments provide their own window.setTimeout etc.

Those environments should create browser-like conditions, not that other way around. If you think about it, it seems really weird, for us to do this, it is like for Firefox to change some their behaviour to keep consistency with the jsdom, which, actually should create Firefox like environment. Sounds like absurd, i bet no browser lib would do that.

In other words, jsdom should create hacks and not us, and again to emphasize - we didn't received any issues which should provoke this PR.

Also, @domenic could you please check it out, since it seems, we doing that just for you.

Otherwise LGTM :-).

@domenic
Copy link

domenic commented Jun 15, 2015

There's no reason to do this. Scripts that run in jsdom can use setTimeout and friends just fine, with no prefix.

If you run into any other "do this to support jsdom" issues, let me know and we can fix jsdom.

@mgol
Copy link
Member Author

mgol commented Jun 15, 2015

@markelog

However, some environments provide their own window.setTimeout etc.

Those environments should create browser-like conditions, not that other way around.

The thing is those environments do provide those conditions. The current situation is that we support both scenarios:

  1. The global behaves like a browser window (web browsers, browserify): we return the jQuery object.
  2. The global doesn't behave like a browser window: we then return a function to which you can provide your own window instance and it'll return the jQuery object using that window.

In the (2) case we're cheating a little as we generally take stuff off this window but with exception of setTimeout & friends which we always take from the global.

We could decide that this is an edge case that's not worth supporting but it will mean a little divergence from the (2) use case.

@domenic This mostly isn't about jsdom but about running in an environment where you can provide most needed stuff via your own window instance but without assuming you have anything on a global. Though if you were creating & closing (window.close()) a lot of windows in jsdom and using setTimeout etc. in them you'd leak memory, wouldn't you? jsdom has its own setTimeout etc. for a reason, doesn't it?

@mgol
Copy link
Member Author

mgol commented Jun 15, 2015

we didn't received any issues which should provoke this PR

We did, the exact one I mentioned in the description of this PR... i.e. #2177.

@domenic
Copy link

domenic commented Jun 15, 2015

Oh, I don't really know anything about the use case of pasing in a window to a factory function. jsdom is geared around the use case of running jquery inside the jsdom environment itself, not that thing.

@markelog
Copy link
Member

@mzgol well, in there, owner of that ticket tries to use Deferreds (in the environment that doesn't have setTimeout!) which is better to do through https://github.com/jaubourg/jquery-deferred-for-node package or through custom build, so that is really out there which doesn't worth 40 bytes and code complication from my perspective.

So i as i see it, since jsdom could work with jQuery just fine, the reason to do it is to be more consistent but a lot more hacky?

Thank you for the replay @domenic, you helped a lot.

@mgol
Copy link
Member Author

mgol commented Jun 15, 2015

@domenic Hm. It was all added in 1f67d07. You even posted some comments under the commit. ;)

@mgol
Copy link
Member Author

mgol commented Jun 15, 2015

Well, if we just want to assume that window is available then the whole factory function with all this complicated logic when to return the object and when the function accepting window as a parameter is unnecessary... So how did we end up here? :P @timmywil?

@mgol
Copy link
Member Author

mgol commented Jun 15, 2015

Also, if we want to go full-in in requiring a proper window to be present
then I'd prefer to remove the JSHint exception for setTimeout etc. and just
use window.setTimeout without a fallback to the global one like we do with
most browser globals.

@timmywil
Copy link
Member

Actually, I'd be fine with requiring a proper window and moving to window.setTimeout everywhere. The problems that would cause in Node would be mitigated by using tools like jsdom along with jQuery, which is probably the only way jQuery should be used in Node anyway.

Side note: we probably shouldn't recommend https://github.com/jaubourg/jquery-deferred-for-node after 3.0 comes out. It hasn't received updates in a long time and does not support Promises/A+. I know that was the point of packaging jquery deferreds separately, but it would no longer be consistent with jQuery 3.0 Deferreds.

@mgol
Copy link
Member Author

mgol commented Jun 16, 2015

@timmywil @markelog OK, I created yet another attempt: #2397.

@markelog
Copy link
Member

Side note: we probably shouldn't recommend https://github.com/jaubourg/jquery-deferred-for-node after

Let's ask @jaubourg, i thought he is still commited to supporting this module

@markelog
Copy link
Member

I'd be fine with requiring a proper window and moving to window.setTimeout everywhere

Why we need a window "prefix" for global vars?

@mgol
Copy link
Member Author

mgol commented Jun 16, 2015

Why we need a window "prefix" for global vars?

For the same reason we prefix getComputedStyle, document, document.documentElement etc. - so that we're consistent in taking them from the provided window instance instead of the global. If we e.g. assumed a global getComputedStyle, jsdom wouldn't work. It would work without scoping setTimeout but there are other use cases. I think we should be consistent.

@dmethvin
Copy link
Member

It took me a while to catch up. In this particular case on #2177 it seems like the methods would normally be on global but are only on window for this unusual Firefox env, so we reference them from there?

@mgol
Copy link
Member Author

mgol commented Jun 16, 2015

@dmethvin This comment of mine is a good summary: #2394 (comment)

We take most globals from window but we take setTimeout etc. from the global which works for jsdom but not necessarily for other envs and is inconsistent. This PR tries to support both but the simpler one (the 4th my PR trying to fix this issue ;)) that just takes them from window is at #2397.

@markelog
Copy link
Member

Now i remember, okay, i agree, but i still would just write window.setTimeout

@mgol
Copy link
Member Author

mgol commented Jun 16, 2015

Closing in favor of #2397.

@mgol mgol closed this Jun 16, 2015
@mgol mgol deleted the window-settimeout3 branch June 16, 2015 18:11
@mgol mgol removed the Needs review label Jun 16, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Development

Successfully merging this pull request may close these issues.

7 participants