@@ -36,6 +36,9 @@ class ElectronIPCTransport extends JSONRPC.ClientPluginBase
3636 super ( ) ;
3737
3838
39+ this . _arrDisposeCalls = [ ] ;
40+
41+
3942 // JSONRPC call ID as key, {promise: {Promise}, fnResolve: {Function}, fnReject: {Function}, outgoingRequest: {OutgoingRequest}} as values.
4043 this . _objBrowserWindowRequestsPromises = { } ;
4144
@@ -46,10 +49,28 @@ class ElectronIPCTransport extends JSONRPC.ClientPluginBase
4649 // eslint-disable-next-line no-undef
4750 this . _strChannel = "jsonrpc_winid_" + ( browserWindow ? browserWindow . id : ( window || self ) . require ( "electron" ) . remote . getCurrentWindow ( ) . id ) ;
4851
52+
4953 this . _setupIPCTransport ( ) ;
5054 }
5155
5256
57+ /**
58+ * @returns {null }
59+ */
60+ dispose ( )
61+ {
62+ for ( const fnDispose of this . _arrDisposeCalls )
63+ {
64+ fnDispose ( ) ;
65+ }
66+ this . _arrDisposeCalls . slice ( 0 ) ;
67+
68+ this . rejectAllPromises ( ) ;
69+
70+ super . dispose ( ) ;
71+ }
72+
73+
5374 /**
5475 * @returns {BrowserWindow|null }
5576 */
@@ -227,32 +248,33 @@ class ElectronIPCTransport extends JSONRPC.ClientPluginBase
227248 {
228249 if ( ! this . _bBidirectionalMode )
229250 {
251+ const fnOnChannel = async ( event , objJSONRPCRequest ) => {
252+ await this . processResponse ( objJSONRPCRequest ) ;
253+ } ;
254+
230255 // eslint-disable-next-line no-undef
231- ( window || self ) . require ( "electron" ) . ipcRenderer . on (
232- this . _strChannel ,
233- async ( event , objJSONRPCRequest ) => {
234- await this . processResponse ( objJSONRPCRequest ) ;
235- }
236- ) ;
256+ ( window || self ) . require ( "electron" ) . ipcRenderer . on ( this . _strChannel , fnOnChannel ) ;
257+
258+ // eslint-disable-next-line no-undef
259+ this . _arrDisposeCalls . push ( ( ) => { ( window || self ) . require ( "electron" ) . ipcRenderer . removeListener ( this . _strChannel , fnOnChannel ) ; } ) ;
237260 }
238261 }
239262 else
240263 {
241- this . _browserWindow . on (
242- "closed" ,
243- ( ) => {
244- this . rejectAllPromises ( new Error ( `BrowserWindow ${ this . browserWindow . id } closed` ) ) ;
245- }
246- ) ;
264+ const fnOnClosed = ( ) => {
265+ this . rejectAllPromises ( new Error ( `BrowserWindow ${ this . browserWindow . id } closed` ) ) ;
266+ } ;
267+ this . _browserWindow . on ( "closed" , fnOnClosed ) ;
268+ this . _arrDisposeCalls . push ( ( ) => { this . _browserWindow . removeListener ( "closed" , fnOnClosed ) ; } ) ;
247269
248270 if ( ! this . _bBidirectionalMode )
249271 {
250- electron . ipcMain . on (
251- this . channel ,
252- async ( event , objJSONRPCRequest ) => {
253- await this . processResponse ( objJSONRPCRequest ) ;
254- }
255- ) ;
272+ const fnOnChannel = async ( event , objJSONRPCRequest ) => {
273+ await this . processResponse ( objJSONRPCRequest ) ;
274+ } ;
275+
276+ electron . ipcMain . on ( this . channel , fnOnChannel ) ;
277+ this . _arrDisposeCalls . push ( ( ) => { electron . ipcMain . removeListener ( this . channel , fnOnChannel ) ; } ) ;
256278 }
257279 }
258280 }
0 commit comments