-
Notifications
You must be signed in to change notification settings - Fork 22.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enable configuration of a different window object #1250
Conversation
This adds an extra layer of closures on all of d3, so has a performance cost in the common case where only the current window is needed. I see it as being valuable to support this use case, but I’d greatly prefer to do it in a way that doesn’t cost other use cases (particularly the main one, where D3 is loaded in a browser). For example, perhaps there’s something we can do with |
That makes total sense, I'll have a think about alternative solutions. Thanks for getting back to me so quickly. |
perhaps this change would make more sense? In the above commit I've added a new build specific to node.js. It adds a little build time overhead but avoids the performance penalty for the common case. |
Eh, that seems complicated (and duplicates src/d3.js), sorry. There’s already a node-specific “build” of D3, in the sense of the index.js that is only used in a Node ( |
no worries, I'm always happy to go back to the drawing board. The I'll give it more thought. |
Right, that’s because it’s loading a minimal instance of D3 to verify that all the required import statements are there. |
Hi could you use the arguments.callee property of the constructor function and assign it to the var d3 object created on line 2? For example:
|
That is a really nice solution Nick! |
Would need to remember to pass in the window object when d3 self invokes. // src/end.js
return d3;
})(window); |
names the self invoking function that creates d3 so a reference to it can be stored in d3.env
the same thing could be achieved with a named function expression. I've put together this branch to demonstrate. The NFE approach (or |
updated this pull request to reflect the NFE approach. |
This looks promising. What about if the name of the argument was |
(Also, |
I've updated the I tried to pass in the |
Right, the issue with the tests is that large parts of D3 don’t depend on having a Of course, to compile that symbol needs to be defined, but it can still be null. I pushed my edits to an environment branch. |
If I understand this correctly there would be a new complete d3 instance for each additional window. Would it not be better to keep track of local window in the selection prototype, as window is only used in selections? |
Well, in a sense that’s already possible because you can |
Splendid stuff, I see what the test harness is doing now. Thank you. |
This is very useful. I'm also using D3 with domino server side, to not only construct HTML documents but also to query and process long and deeply nested XML documents; D3 is very useful in this context. Wouldn't it be better if there were no default document/window objects on D3 when running server side? Client side, of course, there's no reason not to have them, but server side the use cases are more diverse and the explicit configuration of the document might not be felt as a burden. At work we don't need jsdom at all but still we run into problems with it because jsdom is a D3 dependency but it won't easily build on our windows machines. |
Yeah I'm also in the position where the inclusion of jsdom as a dependency, and the creation of the default window is unnecessary. My aim with this pull request was to add the environments concept without breaking anything that currently depends on that default window.
very cool. |
Fixed in #2225; D3 no longer assumes a global window or document. So, if there’s no global window or document, you must call d3.select(node).select(string) to first specify the root node of a selection rather than calling d3.select(string) which tries to use the global document.documentElement. |
Presently when running in node.js, d3 creates it's own window object to operate on. While initially convenient this makes it quite difficult to have independent flows of execution all using d3.
This change enables the creation of new d3 instances which are bound to provided windows.
My driving motivation is that I want to use d3 to build my views on both the client and the server. On the server I need each request to have an isolated environment.
Here is an example of using the proposed
d3.env
function with jsdomAs well as allowing for isolated windows this enables the use of alternative DOM implementations. As an example for my own purposes I believe domino is more appropriate.
Grateful for you thoughts
sammy