Fix native window objects leak after refreshes

Summary:
It looks like electron creates new native window object after each window refresh, but we hold previous objects because of listeners attached to them. In dev mode, Electron signalises about that in console log each time when window is focused / blured after refresh:
```
Attempting to call a function in a renderer window that has been closed or released.
Function provided here: init-fast-refresh.bundle?platform=web&dev=true&minify=false:800356:19
```

Reviewed By: passy

Differential Revision: D27619305

fbshipit-source-id: 51d1590903a37dba721fb7a711fdebb9f0034b98
This commit is contained in:
Anton Nikolaev
2021-04-07 05:51:22 -07:00
committed by Facebook GitHub Bot
parent 1e0dafa4cf
commit eabf0fc340

View File

@@ -37,27 +37,33 @@ export const uriComponents = (url: string): Array<string> => {
export default (store: Store, _logger: Logger) => {
const currentWindow = remote.getCurrentWindow();
currentWindow.on('focus', () => {
const onFocus = () => {
setImmediate(() => {
store.dispatch({
type: 'windowIsFocused',
payload: {isFocused: true, time: Date.now()},
});
});
});
currentWindow.on('blur', () => {
};
const onBlur = () => {
setImmediate(() => {
store.dispatch({
type: 'windowIsFocused',
payload: {isFocused: false, time: Date.now()},
});
});
};
currentWindow.on('focus', onFocus);
currentWindow.on('blur', onBlur);
window.addEventListener('beforeunload', () => {
currentWindow.removeListener('focus', onFocus);
currentWindow.removeListener('blur', onBlur);
});
// windowIsFocussed is initialized in the store before the app is fully ready.
// So wait until everything is up and running and then check and set the isFocussed state.
window.addEventListener('flipper-store-ready', () => {
const isFocused = currentWindow.isFocused();
const isFocused = remote.getCurrentWindow().isFocused();
store.dispatch({
type: 'windowIsFocused',
payload: {isFocused: isFocused, time: Date.now()},