electron/remote

Attempt to call getCurrentWindow on remote throws error

Treverix opened this issue · 5 comments

this.storage[id].count++

I tried this on a client side script

const {getCurrentWindow} = require('@electron/remote');
console.log('Remote window Id: ', getCurrentWindow().id);

and it crashes with that error:

Uncaught TypeError: Cannot read property 'count' of undefined
    at ObjectsRegistry.add (C:\dev\prj\xxx\node_modules\@electron\remote\dist\src\main\objects-registry.js:33:25)
    at valueToMeta (C:\dev\prj\xxx\node_modules\@electron\remote\dist\src\main\server.js:148:44)
    at C:\dev\prj\xxx\node_modules\@electron\remote\dist\src\main\server.js:389:16
    at IpcMainImpl.<anonymous> (C:\dev\prj\xxx\node_modules\@electron\remote\dist\src\main\server.js:301:27)
    at IpcMainImpl.emit (events.js:310:20)
    at WebContents.<anonymous> (electron/js2c/browser_init.js:153:8925)
    at WebContents.emit (events.js:310:20)

I've debugged and saw that the v8Util.getHiddenValue(object, 'electronId'); in saveToStorage returns an id for the given object and therefore the object is not put on this.storage. this.storage therefore is an empty object {} and so it throws the error.

It failed in my case because I used both the original remote module and @electron/remote in parallel (while migrating). And this causes crashes because both modules use the same key on the same storage:

for example:

v8Util.setHiddenValue(object, 'electronId', id);

This can be solved by using different keys (namespaces) on the storage. I've tried that and replaced electronId be @electronId on the package sources in node_modules and with that, I was able to run the electron application again in 'mixed mode' with no errors.

I'm pretty sure that such a mixed mode is not recommended but it may be unavoidable for many until we reach electron 12 and can only use a single remote module (if needed). There's a chance thate 3rd party libs already depend on this userland remote while the hosting application still uses the old one. And that will lead to the above mentioned errors.

Thanks for the report! It would be great to support mixing this module with the native remote module—did you find any other changes to be necessary? I'd love it if you would open a PR with the change you described!

(Sorry for the late response btw—just realised I didn't have notifications turned on for this repo.)

Hi - I have a working fork where I applied the change - it has unfortunately a few more changes because I needed to push it npm... But please, have a look. It runs stable in my project ('mixed-mode') but I can't tell, if it's ok the way I did it. I basically renamed the electronId value to @electronId and now they don't clash anymore.

https://github.com/Treverix/remote

I also changed something in server.ts but - shame on me - can't tell why. It probably didn't compile or run with process.mainModule!.require(moduleName) anymore.

Give @electron/remote@1.0.2 a try!