Skip to content
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

Can't require module in new window because cache is not clearing properly. #4604

Closed
Chayemor opened this issue Mar 18, 2016 · 3 comments
Closed
Labels

Comments

@Chayemor
Copy link

Environment

Problem

New window is opened from parent window. Webchimera module is loaded but exception ensuses because its window object points to parent window. That is because NW.js caches its modules. I get that. So I tried clearing cache both manually and with require-new module, doesn't matter. The wcjs-player module keeps pointing to parent window object no matter what I do.

PARENT

<div id="chimera_container"></div>
...
var gui; // global variable for parent object
var wjs; // global variable for parent object
....
gui = require('nw.gui');
wjs = wjs = require('wcjs-player');
....
video_emit = gui.Window.open('video_emit.html',{
    "window" : {
        "focus": true,
        "toolbar": true,
        "frame": true,
        "width" : 640,
        "height" : 640
    },
    "dependencies": {
        "require-new": "^1.1.0",
        "wcjs-player": "^0.5.6"
    },  
});

video_emit window

<div id="vid"></div>
....
var wjs; // global variable for new window object
wjs = require('wcjs-player');  
video_container = new wjs("#vid"); <---- Exception because it can't find the #vid div because window.document points to parent window.document

:(

In the node_modules\wcjs-player\index.js line 82 I added
console.log(window.document);

PARENT CONSOLE OUTPUT

#document
...
<div id="chimera_container"></div>

video_emit CONSOLE OUTPUT

#document
...
<div id="chimera_container"></div>  

Solutions tried

Manually clearing cache

Calling function clearRequiredModulesbefore requiring 'wcjs-player' module on video_emit window.

var clearRequiredModules = function(){
    // clear wcjs-player from require cache to force new load
    var clearModules = [
        "wcjs-player",
        "jquery" // https://github.com/jaruba/wcjs-player/issues/38
    ];
    for (var i in clearModules) {
        for (var module in global.require.cache) {
            if ( global.require.cache.hasOwnProperty(module) 
                 && module.indexOf(clearModules[i]) > -1
                )
                delete global.require.cache[module];
        }
    }
};

Not working. Same problem as if I hadn't cleared at all. wcjs-player module still points to parent window object.

Using require-new module

There's no science to this code, I load module and use it in video_emit window.

var req_new = require('require-new');
wjs = req_new('wcjs-player');

Same problem. Nothing changes.

I've tried install the wcjs-player module with a different name and loading them separately (as if they were different modules). Using one for each window, didn't matter. Same problem. The only way it works is if I simply make video_emit the first window to open up (by setting it as main in the package.json file). But then if I open a new window and try to load the module, I'm back at square one. That module only points to the window of the first window to open up.

I know that adding new-instance: true to the gui.Window.open of the parent would fix this, but I need parent and new window to communicate via the global variable usable only if they are on the same render.

This is driving me nuts. I don't even know where to look for the problem. Thanks for the help.

@rogerwang
Copy link
Member

Please try the latest RC version of 0.13.0

@Christywl
Copy link
Contributor

Is this issue still reproduced on the latest nwjs build? If so, could you please provide your full test to reproduce it?

@Christywl
Copy link
Contributor

close until there is more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants