Save state during beforeunload event

The window `unload` event is now async, see #3144 for more details, so saving needs to be done during a `beforeunload` to ensure that on window reload the new render process is using the state saved from the previous render process.

This splits unload into two steps, the serializing/saving is done in a `beforeunload` while the workspace view and project are destroyed in the `unload` handler.

Fixes #3144