Problems with Custom HTML Dialog in Electron

Viewed 471

So first off, I know about the following question Custom HTML Dialog in Electron already exists. My question expands on this question when some problems came up on the latest version of electron.

So some context:
I actually started a project of mine on like a very old version on Electron(v2.0.5) that I already had cuz I was too lazy to update electron. I got a working dialog class that you could do something like this:

let dialog = new dialog_class("./pages/dialog.html")
dialog.display().then((response) => console.log(response));

However I had to update my version to the current(v9.1.1) so of course my dialog broke, but I can't figure out how/why.

I create my dialog very much like this:

constructor(link){
    this.link = link;
    this.window = new electron.remote.BrowserWindow({...});
}
display(){
    return new Promise((callback)=>{
        this.window.loadURL(...);//url.format function in place of ...
        this.window.on(`close`, () => {
            if (!this.cancelCloseEvent) callback(false);
        });
    }
}
destroy(){
    this.window.closable = true;
    this.widow.close();
}

However, when I run this function:

function openDialog(){
    let dialog = new dialog_class("./pages/dialog.html")
    dialog.display().then((response) => console.log(response));
}

I can open the dialog perfectly, but can only close the dialog once. Like I can open it, close it then open it again but can't close it again. When I try to close it a 2nd time it keeps the dialog open AND throws this:

electron/js2c/renderer_init.js:82 Uncaught TypeError: Object has been destroyed
    at BrowserWindow.get (electron/js2c/browser_init.js:125)
    at electron/js2c/browser_init.js:233
    at IpcMainImpl.<anonymous> (electron/js2c/browser_init.js:233)
    at IpcMainImpl.emit (events.js:223)
    at WebContents.<anonymous> (electron/js2c/browser_init.js:173)
    at WebContents.emit (events.js:223)

I don't know why this happens, as each time I run openDialog it should create a NEW BrowserWindow, so I don't know how it's referencing the old window.

Note: all the code shown here should be enough for my problem. But just in case heres the entire dialog_class: https://pastebin.com/7pAwZJHF

Edit:

Since I'm putting a bounty on this, I wanted people to be able to reproduce the issue. Heres all the code you'll need: https://github.com/Frustrated-Programmer/ElectronBug
However: I still think all the code relevant is still supplied in this question

1 Answers

dialog.js

display(context) {
        return new Promise((cb) => {
            electron.ipcRenderer.on("callback", (event, val) => {
                this._cancelCloseEvent = true;  
                cb(val);
                // this.destroy(); WRONG!!! Remove this and destory your window on test.js which is having this object instance
            });
        }
        ...

test.js

dialog.display().then(function(value) {
    response.innerText = value;
    dialog.destroy();
    // dialog = null; unnecessary!
});
Related