pjeby / hot-reload

Automatically reload Obsidian plugins in development when their files are changed
ISC License
496 stars 23 forks source link

Browser Window fail on reload? #11

Closed thesamim closed 7 months ago

thesamim commented 7 months ago

I'm probably doing something wrong.

I have a modal in my window. Launched from my settings. When the plugin reloads after changes, and I try to launch the modal again I get:

remote.js:273 Uncaught Error: Could not call remote method 'show'. Check that the method signature is correct. Underlying error: TypeError: Object has been destroyedUnderlying stack: TypeError: Object has been destroyed
    at [my local folder]\Obsidian\resources\app.asar\node_modules\@electron\remote\dist\src\main\server.js:465:71
    at IpcMainImpl.<anonymous> ([my local folder]\Obsidian\resources\app.asar\node_modules\@electron\remote\dist\src\main\server.js:323:27)
    at IpcMainImpl.emit (node:events:513:28)
    at WebContents.<anonymous> (node:electron/js2c/browser_init:2:89772)
    at WebContents.emit (node:events:513:28)

    at [my local folder]\Obsidian\resources\app.asar\node_modules\@electron\remote\dist\src\main\server.js:468:25
    at IpcMainImpl.<anonymous> ([my local folder]\Obsidian\resources\app.asar\node_modules\@electron\remote\dist\src\main\server.js:323:27)
    at IpcMainImpl.emit (node:events:513:28)
    at WebContents.<anonymous> (node:electron/js2c/browser_init:2:89772)
    at WebContents.emit (node:events:513:28)

I instantiate with: const window = new BrowserWindow({ show: false,{ show: false,[...]});

In the on close method I end with: window.destroy();

What should I be doing to reload correctly? (Incidentally: if I force a reload with Ctrl-R in the console window, it works fine.)

pjeby commented 7 months ago

The difference between ctrl-r and hot reload is that hot reload unloads the existing plugin before loading it again. So any plugin shutdown code runs. When you use ctrl-r, your shutdown code does not run, but (almost) everything is shut down forcibly, including many (but not all) electron resources.

(In my own experience with browserwindow and Obsidian, I found I needed to have my plugin unload code explicitly close any custom browserwindows I created that are still open.)

In general, to work with hot-reload, your plugin needs to have proper unload handling, which means that it should shut down or undo everything that it is doing at the time it's unloaded, or it runs the risk of leaving Obsidian in a corrupted state. If you're using the electron API directly, that usually means being sure to unregister things, close them, etc. when the plugin as a whole is shut down.

You can confirm this for yourself by manually disabling and enabling your plugin instead of using hot-reload -- you will get the same error, unless of course you have to close your extra window in order to do the manual disable/enable. Hot reload just automates flicking on and off the toggle switch for your plugin in the community plugins list.

thesamim commented 7 months ago

Thanks for the explanation.