Skip to content

Commit 124a842

Browse files
abhigyankakashnimare
authored andcommitted
electron-bridge: Implement electron bridge.
This PR adds a bridge to communicate with the webapp in real time. As of now, the bridge listens for following events - * When realm name changes * When realm icon changes * When the unread count changes Partially fixes #425.
1 parent 7130103 commit 124a842

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

app/main/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ app.on('ready', () => {
279279
});
280280
});
281281
});
282+
283+
ipcMain.on('realm-icon-changed', (event, serverURL, iconURL) => {
284+
page.send('update-realm-icon', serverURL, iconURL);
285+
});
282286
});
283287

284288
app.on('before-quit', () => {

app/renderer/js/electron-bridge.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const events = require('events');
2+
const { ipcRenderer } = require('electron');
3+
4+
// we have and will have some non camelcase stuff
5+
// while working with zulip so just turning the rule off
6+
// for the whole file.
7+
/* eslint-disable camelcase */
8+
class ElectronBridge extends events {
9+
send_event(...args) {
10+
this.emit(...args);
11+
}
12+
13+
on_event(...args) {
14+
this.on(...args);
15+
}
16+
}
17+
18+
const electron_bridge = new ElectronBridge();
19+
20+
electron_bridge.on('total_unread_count', (...args) => {
21+
ipcRenderer.send('unread-count', ...args);
22+
});
23+
24+
electron_bridge.on('realm_name', (...args) => {
25+
ipcRenderer.send('realm-name-changed', ...args);
26+
});
27+
28+
electron_bridge.on('realm_icon_url', iconURL => {
29+
const serverURL = location.origin;
30+
iconURL = iconURL.includes('http') ? iconURL : `${serverURL}${iconURL}`;
31+
ipcRenderer.send('realm-icon-changed', serverURL, iconURL);
32+
});
33+
34+
// this follows node's idiomatic implementation of event
35+
// emitters to make event handling more simpler instead of using
36+
// functions zulip side will emit event using ElectronBrigde.send_event
37+
// which is alias of .emit and on this side we can handle the data by adding
38+
// a listener for the event.
39+
module.exports = electron_bridge;

app/renderer/js/main.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,22 @@ class ServerManagerView {
397397
const webContents = webview.getWebContents();
398398
webContents.send('toggle-dnd', state, newSettings);
399399
});
400+
401+
ipcRenderer.on('update-realm-icon', (event, serverURL, iconURL) => {
402+
DomainUtil.getDomains().forEach((domain, index) => {
403+
if (domain.url.includes(serverURL)) {
404+
DomainUtil.saveServerIcon(iconURL).then(localIconUrl => {
405+
const serverImgsSelector = `.tab .server-icons`;
406+
const serverImgs = document.querySelectorAll(serverImgsSelector);
407+
serverImgs[index].src = localIconUrl;
408+
409+
domain.icon = localIconUrl;
410+
DomainUtil.db.push(`/domains[${index}]`, domain, true);
411+
DomainUtil.reloadDB();
412+
});
413+
}
414+
});
415+
});
400416
}
401417

402418
destroyTab(name, index) {

app/renderer/js/preload.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ require('./notification');
1212
// Prevent drag and drop event in main process which prevents remote code executaion
1313
require(__dirname + '/shared/preventdrag.js');
1414

15+
// eslint-disable-next-line camelcase
16+
window.electron_bridge = require('./electron-bridge');
17+
1518
const logout = () => {
1619
// Create the menu for the below
1720
document.querySelector('.dropdown-toggle').click();

0 commit comments

Comments
 (0)