@@ -23,12 +23,10 @@ class ScreenShareRenderHook {
2323 this . _sendCloseTrackerEvent = this . _sendCloseTrackerEvent . bind ( this ) ;
2424 this . _onScreenSharingEvent = this . _onScreenSharingEvent . bind ( this ) ;
2525 this . _onIframeApiLoad = this . _onIframeApiLoad . bind ( this ) ;
26- this . _cleanContext = this . _cleanContext . bind ( this ) ;
26+ this . _cleanTrackerContext = this . _cleanTrackerContext . bind ( this ) ;
27+ this . _onApiDispose = this . _onApiDispose . bind ( this ) ;
2728
28- ipcRenderer . on ( SCREEN_SHARE_EVENTS_CHANNEL , this . _onScreenSharingEvent ) ;
29- this . _api . on ( 'screenSharingStatusChanged' , this . _onScreenSharingStatusChanged ) ;
30- this . _api . on ( 'videoConferenceLeft' , this . _cleanContext ) ;
31- this . _api . on ( '_willDispose' , this . _cleanContext ) ;
29+ this . _api . on ( '_willDispose' , this . _onApiDispose ) ;
3230 this . _iframe . addEventListener ( 'load' , this . _onIframeApiLoad ) ;
3331 }
3432
@@ -59,6 +57,10 @@ class ScreenShareRenderHook {
5957 . catch ( ( error ) => errorCallback ( error ) ) ;
6058 }
6159 } ;
60+
61+ ipcRenderer . on ( SCREEN_SHARE_EVENTS_CHANNEL , this . _onScreenSharingEvent ) ;
62+ this . _api . on ( 'screenSharingStatusChanged' , this . _onScreenSharingStatusChanged ) ;
63+ this . _api . on ( 'videoConferenceLeft' , this . _cleanTrackerContext ) ;
6264 }
6365
6466 /**
@@ -121,19 +123,32 @@ class ScreenShareRenderHook {
121123 }
122124
123125 /**
124- * Clear all event handler in order to avoid any potential leaks and close the screen sharing tracker
125- * window in the event that it's currently being displayed.
126+ * Clear all event handlers related to the tracker in order to avoid any potential leaks and closes it in the event
127+ * that it's currently being displayed.
126128 *
127129 * @returns {void }
128130 */
129- _cleanContext ( ) {
131+ _cleanTrackerContext ( ) {
130132 ipcRenderer . removeListener ( SCREEN_SHARE_EVENTS_CHANNEL , this . _onScreenSharingEvent ) ;
131133 this . _api . removeListener ( 'screenSharingStatusChanged' , this . _onScreenSharingStatusChanged ) ;
132- this . _api . removeListener ( 'videoConferenceLeft' , this . _sendCloseTrackerEvent ) ;
133- this . _api . removeListener ( '_willDispose' , this . _sendCloseTrackerEvent ) ;
134- this . _iframe . removeEventListener ( 'load' , this . _onIframeApiLoad ) ;
134+ this . _api . removeListener ( 'videoConferenceLeft' , this . _cleanTrackerContext ) ;
135135 this . _sendCloseTrackerEvent ( ) ;
136136 }
137+
138+ /**
139+ * Clear all event handlers in order to avoid any potential leaks.
140+ *
141+ * NOTE: It is very important to remove the load listener only when we are sure that the iframe won't be used
142+ * anymore. Otherwise if we use the videoConferenceLeft event for example, when the iframe is internally reloaded
143+ * because of an error and then loads again we won't initialize the screen sharing functionality.
144+ *
145+ * @returns {void }
146+ */
147+ _onApiDispose ( ) {
148+ this . _cleanTrackerContext ( ) ;
149+ this . _api . removeListener ( '_willDispose' , this . _onApiDispose ) ;
150+ this . _iframe . removeEventListener ( 'load' , this . _onIframeApiLoad ) ;
151+ }
137152}
138153
139154/**
0 commit comments