From eabf0fc34050bd3a484989ac7abd5b8ca13690e3 Mon Sep 17 00:00:00 2001 From: Anton Nikolaev Date: Wed, 7 Apr 2021 05:51:22 -0700 Subject: [PATCH] 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 --- desktop/app/src/dispatcher/application.tsx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/desktop/app/src/dispatcher/application.tsx b/desktop/app/src/dispatcher/application.tsx index 6c4e6bbca..899e6fcfa 100644 --- a/desktop/app/src/dispatcher/application.tsx +++ b/desktop/app/src/dispatcher/application.tsx @@ -37,27 +37,33 @@ export const uriComponents = (url: string): Array => { 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()},