-
Notifications
You must be signed in to change notification settings - Fork 3.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
[nw13] rethink the way we deal with Contexts #3107
Comments
Copying @Mithgol 's survey and suggestion from #890 here: Different windows of a node-webkit's application have different JavaScript contexts; each window has its own global object and its own set of global constructors (such as It's generally a good thing because when an object's prototype is replaced or augmented by a library (such as Prototype) or a simpler script, the analogous objects in other windows are unaffected nevertheless. Otherwise bugs could affect larger areas (several windows), malicious applications could access confidential data structures, etc. And Node modules in node-webkit run in their own shared Node context for the same reason. Such difference in contexts is generally benefitial, but sometimes it comes with a price. For example, a simple check such as Several issues of the past (#702, #716, #832) were caused by this difference of contexts and object inheritance. That's why, for such special cases, I have to request an option for the That option should be The following Node.js globals should also become the globals (i.e. properties of the global object) in such window:
It is important to maintain The name for such option could be The current workaround is using my |
We're going to see more and more libraries begin supporting both node/browser as more people rely on npm for client libs. They need to be able to determine which context they are run in. For that reason only, I would suggest keeping With that being said, I don't fully understand all of the implications you listed above, so I'm not aware of all of the problems that may cause. |
I've been running my apps completely in the node context to avoid having to worry about that. I manually set the window object and other globals, and for the most part, things just work. IMO, the ability to have just 1 context is a huge benefit of nw.js... If I didn't want my use case above, I could just use AtomShell where the distinction is a lot more clear cut. As in the suggestions above, making it an option would probably make everyone happy, but personally, I really like to think of nw.js as a normal node instance that just happen to be able to spawn windows (because of that, I'd also be pretty happy if I could do it just that way too... start in my node main and spawn my window from there, but that's a much bigger change, probably not for this discussion). 1 context (the node one) is very nice for that. |
Thanks for raising this issue; it's a very important and fundamental design question of NW.js. Thank you and everyone else also for NW.js itself, which is an incredible undertaking.
What would the global |
I've actually been pretty satisfied with the two-context system so far. I have a simple (almost empty) index.html file, where I include a few browser-oriented javascript libraries (jquery, CKEditor, Leaflet, Stripe, and a few others). Each of these libraries defines globals (for example: From the |
Actually, let me expand on that a little bit... When I say that the rest of my application is in node context, I'm even including DOM manipulation (via jQuery), because I pass the jQuery object ( If I wanted to use the The only thing that feels awkward to me is that Maybe something like this:
Keeping node's |
It can be difficult to get 6to5/babel (#3086) to work, if you want to use ES6 modules and translate them back to AMD modules which also need the |
I've had multiple issues with libraries, that try to detect node environment and so don't add global objects to |
What problem does it solve if we make it defined? (And it also can create problems with libraries that assume that
Two-context system is fine and helpful, but sometimes it also could be useful if a window could be explicitly specified to run in Node context (that's what #890 was about). |
I would like to behave nw.is that way: let me drop in any web app and it should work (e.g. also AMD based apps would work as |
tldr window by default in node context wouldn't make a whole lot of difference to me, and Atom Shell-style context separation would add complexity making certain designs very difficult. I'm porting the GUI for Pure Data, a realtime audio programming app, from tcl/tk to nw.js. It is a multi-window application with editable diagrams in each window. (Plus a "main" window for error printout and audio controls.) The core logic in C communicates with the GUI over localhost. I receive messages in a node context and manipulate the contents of the relevant window from there. (There are some window menus and a few other functions that live in the various window DOM contexts, but those usually just call some function in the node context.) If the "main" DOM were accessible automatically from the node context, it would only save me a single function call (i.e., setting a variable in the node context so that I can manipulate main window DOM from there). I see some comments here praising the Atom Shell design. If nw.js went that route it would add an enormous amount of complexity to a multi-window design like mine. There are two reasons for this: Now let's say I open another window from my main.js, and the page in my new window contains this script: In Atom Shell, I cannot retrieve the value 42 because my node context isn't shared between the two windows. Because of this... b) In Atom Shell, you use the ipc and/or remote module to send data among windows/iframes/etc. That's great for security, and it probably makes a lot of sense if you're porting web apps to the desktop which communicate through a REST API to a server somewhere. (Qt seems to have a similar type API for QWebView.) But for apps that are multi-window Atom Shell makes it expensive to incrementally design the GUI. (Prohibitively so in my case.) The ease with which you can get started in nw.js compared to Atom Shell is a testament to this. |
Added a new comment to #3086 (comment). I think this illustrates well how the current way of contexts can make debugging more difficult. |
I'd like to see the two context system remain in place by default but with the 'require' function (node context entry point) namespaced as something like nw.require (until one is within the node context). At present, I using the inverse of this, with the requirejs optimizer being applied to Agree that AMD modules working out of the box seems important. |
Agree with @slartibardfast... Once in node context, |
If something like |
Our product relies on |
Also remember that new ES6 modules syntax is on it's way to be native. Won't it be a problem for NW? Right now thanks to different way of loading scripts we know what is loaded to which context ( |
@szwacz Do you mean that the current Because otherwise (i.e. if they are different) the community of Node.js, in order to abandon
Theoretically such commitment to On the other hand, if the current |
Will introduce a The default value for the |
how to use 'nw.gui.window' in nw13? |
@fxpoet just use nw.Window . no need to call require('nw.gui') any more. |
v0.13.0-alpha2 is released with mixed context mode, To turn on the Mixed Context mode, add '--mixed-context' to the command line or set 'mixed-context' to true in manifest. When it's off, it should have zero side effect on the normal mode. https://groups.google.com/d/msg/nwjs-general/zeC6SedUSFY/BlumrYJeVHYJ |
@rogerwang Can you explain more your answer for @fxpoet... I can't make it work. |
I have solved this issue. |
@Mithgol I am currently facing the same problem porting a jspm managed app. Because Systemjs provides a universal module loader for the browser, everything is being loaded in Webkit context. I had sometime back seen some serious discussion going on regarding AMD/CJS/ES6 module support at those fora (will link once I can find the page again). For now I could do with a 'jailbreak' that will allow me to move over contexts arbitrarily while continuing to use |
Sharing objects directly by reference between multiple windows is very useful,it's make easy way to emit event between different windows ,I feel troubled and difficult to use the atom/electron's ipc implements。 |
Agree with this:
Moving a generic "require" into the browser context can cause some unnecessary conflicts (as can exposing window to the Node context). That said, it's not been a major issue for me. |
From day 1 In NW we've been using a separate
Node context
fromDOM context (or Window context)
. This was a design decision to support the following:But this has side effects like: comparison of types from different contexts; confusing libraries that supports Node.js already. There is a good survey and suggestion: #890 , and other issue like #1588, jsbin/jsbin#1150 (comment)
So the questions are: Should we keep
window
undefined in Node context? Should we keep a single context rather than Node/Window context split in NW?What's your opinion from user's perspective? I'm eager to hear from you because this would be an important decision we're going to make when preparing for nw13, which will be a major step forward from previous versions.
The text was updated successfully, but these errors were encountered: