Skip to content

Commit f67d97e

Browse files
committed
fix(preload) tighten IPC events, simplify API
1 parent 3f50fa1 commit f67d97e

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

app/features/app/components/App.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class App extends Component {
2828

2929
this._listenOnProtocolMessages
3030
= this._listenOnProtocolMessages.bind(this);
31+
32+
this._listeners = [];
3133
}
3234

3335
/**
@@ -37,7 +39,9 @@ class App extends Component {
3739
*/
3840
componentDidMount() {
3941
// start listening on this events
40-
window.jitsiNodeAPI.ipc.on('protocol-data-msg', this._listenOnProtocolMessages);
42+
const removeListener = window.jitsiNodeAPI.ipc.addListener('protocol-data-msg', this._listenOnProtocolMessages);
43+
44+
this._listeners.push(removeListener);
4145

4246
// send notification to main process
4347
window.jitsiNodeAPI.ipc.send('renderer-ready');
@@ -49,16 +53,15 @@ class App extends Component {
4953
* @returns {void}
5054
*/
5155
componentWillUnmount() {
52-
// remove listening for this events
53-
window.jitsiNodeAPI.ipc.removeListener(
54-
'protocol-data-msg',
55-
this._listenOnProtocolMessages
56-
);
56+
const listeners = this._listeners;
57+
58+
this._listeners = [];
59+
listeners.forEach(removeListener => removeListener());
5760
}
5861

5962

6063
/**
61-
* Handler when main proccess contact us.
64+
* Handler when main process contact us.
6265
*
6366
* @param {Object} event - Message event.
6467
* @param {string} inputURL - String with room name.

app/preload/preload.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const { ipcRenderer } = require('electron');
88

99
const whitelistedIpcChannels = [ 'protocol-data-msg', 'renderer-ready' ];
1010

11+
ipcRenderer.setMaxListeners(0);
12+
1113
/**
1214
* Open an external URL.
1315
*
@@ -48,26 +50,29 @@ window.jitsiNodeAPI = {
4850
openExternalLink,
4951
setupRenderer,
5052
ipc: {
51-
on: (channel, listener) => {
53+
addListener: (channel, listener) => {
5254
if (!whitelistedIpcChannels.includes(channel)) {
5355
return;
5456
}
5557

56-
return ipcRenderer.on(channel, listener);
57-
},
58-
send: channel => {
59-
if (!whitelistedIpcChannels.includes(channel)) {
60-
return;
61-
}
58+
const cb = (_event, ...args) => {
59+
listener(...args);
60+
};
61+
62+
const remove = () => {
63+
ipcRenderer.removeListener(channel, cb);
64+
};
6265

63-
return ipcRenderer.send(channel);
66+
ipcRenderer.addListener(channel, cb);
67+
68+
return remove;
6469
},
65-
removeListener: (channel, listener) => {
70+
send: channel => {
6671
if (!whitelistedIpcChannels.includes(channel)) {
6772
return;
6873
}
6974

70-
return ipcRenderer.removeListener(channel, listener);
75+
ipcRenderer.send(channel);
7176
}
7277
}
7378
};

0 commit comments

Comments
 (0)