0
votes

I've created an Electron app primarily a web-app, that has minor integration with the Electron app. The majority is just loading a website through a BrowserWindow with loadURL.

However for each CreateWindow spawned (they have the same content), I need variables to affect the single window, and not all of the windows.

Currently if I change a let variable they are changed for all of the windows. I'm not sure how I would make the variables only be updated and usable with a certain value in the specific window.

e.g. if I set userId = 1 in one window, it's also changed in the 2nd window when I'm sending an ipc message through the web app. I'm not sure what would be the best logic here.

1
It just hit me... do you have a variable in the main process and expect it to be different for all windows?Elias
@Elias Yeah, not sure what I was thinking. But that’s actually what I need. Because I can call the same browser window and they will work in the different windows, but not variables.Path
I've updated my answer (EDIT 2)Elias

1 Answers

1
votes

EDIT:

If you open two separate instances of a window, like below, the variables should each be in their own field.

Just change the location in the main Process

// Create the browser window.
win = new BrowserWindow({ width: 800, height: 600 })

// and load the index.html of the window.
win.loadURL('https://google.com')

// Create the browser window.
win2 = new BrowserWindow({ width: 800, height: 600 })

// and load the index.html of the window.
win2.loadURL('https://youtube.com')

EDIT 2

So depending on a comment I think I got a solution for you. If you want individual variables for each window, you could just store them in the window object its self. Just be sure to extract them (if you still need them) before you destroy the object :)

// Create the browser windows
let win_0 = new BrowserWindow({ width: 800, height: 600 });
let win_1 = new BrowserWindow({ width: 800, height: 600 });

// Set variables
win_0.UID = 'ASDF-ASDF';
win_1.UID = 'QWER-QWER';

// And now you can just get them like
console.log(win_0.UID, win_1.UID);