Skip to content

Commit 3ce9717

Browse files
authored
refactor: namespaced events (#1738)
Signed-off-by: Adam Setch <[email protected]>
1 parent 7d6092b commit 3ce9717

File tree

9 files changed

+83
-49
lines changed

9 files changed

+83
-49
lines changed

src/main/main.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import log from 'electron-log';
33
import { menubar } from 'menubar';
44

55
import { APPLICATION } from '../shared/constants';
6+
import { namespacedEvent } from '../shared/events';
67
import { isMacOS, isWindows } from '../shared/platform';
78
import { onFirstRunMaybe } from './first-run';
89
import { TrayIcons } from './icons';
@@ -93,28 +94,31 @@ app.whenReady().then(async () => {
9394

9495
nativeTheme.on('updated', () => {
9596
if (nativeTheme.shouldUseDarkColors) {
96-
mb.window.webContents.send('gitify:update-theme', 'DARK');
97+
mb.window.webContents.send(namespacedEvent('update-theme'), 'DARK');
9798
} else {
98-
mb.window.webContents.send('gitify:update-theme', 'LIGHT');
99+
mb.window.webContents.send(namespacedEvent('update-theme'), 'LIGHT');
99100
}
100101
});
101102

102103
/**
103104
* Gitify custom IPC events
104105
*/
105-
ipc.handle('gitify:version', () => app.getVersion());
106+
ipc.handle(namespacedEvent('version'), () => app.getVersion());
106107

107-
ipc.on('gitify:window-show', () => mb.showWindow());
108+
ipc.on(namespacedEvent('window-show'), () => mb.showWindow());
108109

109-
ipc.on('gitify:window-hide', () => mb.hideWindow());
110+
ipc.on(namespacedEvent('window-hide'), () => mb.hideWindow());
110111

111-
ipc.on('gitify:quit', () => mb.app.quit());
112+
ipc.on(namespacedEvent('quit'), () => mb.app.quit());
112113

113-
ipc.on('gitify:use-alternate-idle-icon', (_, useAlternateIdleIcon) => {
114-
shouldUseAlternateIdleIcon = useAlternateIdleIcon;
115-
});
114+
ipc.on(
115+
namespacedEvent('use-alternate-idle-icon'),
116+
(_, useAlternateIdleIcon) => {
117+
shouldUseAlternateIdleIcon = useAlternateIdleIcon;
118+
},
119+
);
116120

117-
ipc.on('gitify:icon-active', () => {
121+
ipc.on(namespacedEvent('icon-active'), () => {
118122
if (!mb.tray.isDestroyed()) {
119123
mb.tray.setImage(
120124
menuBuilder.isUpdateAvailable()
@@ -124,7 +128,7 @@ app.whenReady().then(async () => {
124128
}
125129
});
126130

127-
ipc.on('gitify:icon-idle', () => {
131+
ipc.on(namespacedEvent('icon-idle'), () => {
128132
if (!mb.tray.isDestroyed()) {
129133
if (shouldUseAlternateIdleIcon) {
130134
mb.tray.setImage(
@@ -142,14 +146,14 @@ app.whenReady().then(async () => {
142146
}
143147
});
144148

145-
ipc.on('gitify:update-title', (_, title) => {
149+
ipc.on(namespacedEvent('update-title'), (_, title) => {
146150
if (!mb.tray.isDestroyed()) {
147151
mb.tray.setTitle(title);
148152
}
149153
});
150154

151155
ipc.on(
152-
'gitify:update-keyboard-shortcut',
156+
namespacedEvent('update-keyboard-shortcut'),
153157
(_, { enabled, keyboardShortcut }) => {
154158
if (!enabled) {
155159
globalShortcut.unregister(keyboardShortcut);
@@ -166,7 +170,7 @@ app.whenReady().then(async () => {
166170
},
167171
);
168172

169-
ipc.on('gitify:update-auto-launch', (_, settings) => {
173+
ipc.on(namespacedEvent('update-auto-launch'), (_, settings) => {
170174
app.setLoginItemSettings(settings);
171175
});
172176
});

src/main/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { dialog, shell } from 'electron';
55
import log from 'electron-log';
66
import type { Menubar } from 'menubar';
77

8+
import { namespacedEvent } from '../shared/events';
89
import { logError, logInfo } from '../shared/logger';
910

1011
export function takeScreenshot(mb: Menubar) {
@@ -34,7 +35,7 @@ export function resetApp(mb: Menubar) {
3435
});
3536

3637
if (response === resetButtonId) {
37-
mb.window.webContents.send('gitify:reset-app');
38+
mb.window.webContents.send(namespacedEvent('reset-app'));
3839
mb.app.quit();
3940
}
4041
}

src/renderer/__mocks__/electron.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { namespacedEvent } = require('../../shared/events');
2+
13
// @ts-ignore
24
window.Notification = function (title) {
35
this.title = title;
@@ -39,7 +41,7 @@ module.exports = {
3941
switch (channel) {
4042
case 'get-platform':
4143
return Promise.resolve('darwin');
42-
case 'gitify:version':
44+
case namespacedEvent('version'):
4345
return Promise.resolve('0.0.1');
4446
default:
4547
return Promise.reject(new Error(`Unknown channel: ${channel}`));

src/renderer/components/settings/AppearanceSettings.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '@primer/octicons-react';
1010
import { ipcRenderer, webFrame } from 'electron';
1111
import { type FC, useContext, useEffect, useState } from 'react';
12+
import { namespacedEvent } from '../../../shared/events';
1213
import { AppContext } from '../../context/App';
1314
import { Size, Theme } from '../../types';
1415
import { hasMultipleAccounts } from '../../utils/auth/utils';
@@ -29,11 +30,14 @@ export const AppearanceSettings: FC = () => {
2930
);
3031

3132
useEffect(() => {
32-
ipcRenderer.on('gitify:update-theme', (_, updatedTheme: Theme) => {
33-
if (settings.theme === Theme.SYSTEM) {
34-
setTheme(updatedTheme);
35-
}
36-
});
33+
ipcRenderer.on(
34+
namespacedEvent('update-theme'),
35+
(_, updatedTheme: Theme) => {
36+
if (settings.theme === Theme.SYSTEM) {
37+
setTheme(updatedTheme);
38+
}
39+
},
40+
);
3741
}, [settings.theme]);
3842

3943
window.addEventListener('resize', () => {

src/renderer/context/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
useMemo,
88
useState,
99
} from 'react';
10+
import { namespacedEvent } from '../../shared/events';
1011
import { useInterval } from '../hooks/useInterval';
1112
import { useNotifications } from '../hooks/useNotifications';
1213
import {
@@ -172,7 +173,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
172173
}, [settings.keyboardShortcut]);
173174

174175
useEffect(() => {
175-
ipcRenderer.on('gitify:reset-app', () => {
176+
ipcRenderer.on(namespacedEvent('reset-app'), () => {
176177
clearState();
177178
setAuth(defaultAuth);
178179
setSettings(defaultSettings);

src/renderer/utils/comms.test.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { ipcRenderer, shell } from 'electron';
2+
3+
import { namespacedEvent } from '../../shared/events';
24
import { mockSettings } from '../__mocks__/state-mocks';
35
import type { Link } from '../types';
46
import {
@@ -63,50 +65,60 @@ describe('renderer/utils/comms.ts', () => {
6365
it('should get app version', async () => {
6466
await getAppVersion();
6567
expect(ipcRenderer.invoke).toHaveBeenCalledTimes(1);
66-
expect(ipcRenderer.invoke).toHaveBeenCalledWith('gitify:version');
68+
expect(ipcRenderer.invoke).toHaveBeenCalledWith(namespacedEvent('version'));
6769
});
6870

6971
it('should quit the app', () => {
7072
quitApp();
7173
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
72-
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:quit');
74+
expect(ipcRenderer.send).toHaveBeenCalledWith(namespacedEvent('quit'));
7375
});
7476

7577
it('should show the window', () => {
7678
showWindow();
7779
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
78-
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:window-show');
80+
expect(ipcRenderer.send).toHaveBeenCalledWith(
81+
namespacedEvent('window-show'),
82+
);
7983
});
8084

8185
it('should hide the window', () => {
8286
hideWindow();
8387
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
84-
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:window-hide');
88+
expect(ipcRenderer.send).toHaveBeenCalledWith(
89+
namespacedEvent('window-hide'),
90+
);
8591
});
8692

8793
it('should setAutoLaunch (true)', () => {
8894
setAutoLaunch(true);
8995

90-
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:update-auto-launch', {
91-
openAtLogin: true,
92-
openAsHidden: true,
93-
});
96+
expect(ipcRenderer.send).toHaveBeenCalledWith(
97+
namespacedEvent('update-auto-launch'),
98+
{
99+
openAtLogin: true,
100+
openAsHidden: true,
101+
},
102+
);
94103
});
95104

96105
it('should setAutoLaunch (false)', () => {
97106
setAutoLaunch(false);
98107

99-
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:update-auto-launch', {
100-
openAsHidden: false,
101-
openAtLogin: false,
102-
});
108+
expect(ipcRenderer.send).toHaveBeenCalledWith(
109+
namespacedEvent('update-auto-launch'),
110+
{
111+
openAsHidden: false,
112+
openAtLogin: false,
113+
},
114+
);
103115
});
104116

105117
it('should setAlternateIdleIcon', () => {
106118
setAlternateIdleIcon(true);
107119

108120
expect(ipcRenderer.send).toHaveBeenCalledWith(
109-
'gitify:use-alternate-idle-icon',
121+
namespacedEvent('use-alternate-idle-icon'),
110122
true,
111123
);
112124
});
@@ -115,7 +127,7 @@ describe('renderer/utils/comms.ts', () => {
115127
setKeyboardShortcut(true);
116128
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
117129
expect(ipcRenderer.send).toHaveBeenCalledWith(
118-
'gitify:update-keyboard-shortcut',
130+
namespacedEvent('update-keyboard-shortcut'),
119131
{
120132
enabled: true,
121133
keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT,
@@ -127,7 +139,7 @@ describe('renderer/utils/comms.ts', () => {
127139
setKeyboardShortcut(false);
128140
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
129141
expect(ipcRenderer.send).toHaveBeenCalledWith(
130-
'gitify:update-keyboard-shortcut',
142+
namespacedEvent('update-keyboard-shortcut'),
131143
{
132144
enabled: false,
133145
keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT,
@@ -139,13 +151,15 @@ describe('renderer/utils/comms.ts', () => {
139151
const notificationsLength = 3;
140152
updateTrayIcon(notificationsLength);
141153
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
142-
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:icon-active');
154+
expect(ipcRenderer.send).toHaveBeenCalledWith(
155+
namespacedEvent('icon-active'),
156+
);
143157
});
144158

145159
it('should send mark the icons as idle', () => {
146160
const notificationsLength = 0;
147161
updateTrayIcon(notificationsLength);
148162
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
149-
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:icon-idle');
163+
expect(ipcRenderer.send).toHaveBeenCalledWith(namespacedEvent('icon-idle'));
150164
});
151165
});

src/renderer/utils/comms.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ipcRenderer, shell } from 'electron';
2+
import { namespacedEvent } from '../../shared/events';
23
import { defaultSettings } from '../context/App';
34
import { type Link, OpenPreference } from '../types';
45
import { Constants } from './constants';
@@ -20,47 +21,47 @@ export function openExternalLink(url: Link): void {
2021
}
2122

2223
export async function getAppVersion(): Promise<string> {
23-
return await ipcRenderer.invoke('gitify:version');
24+
return await ipcRenderer.invoke(namespacedEvent('version'));
2425
}
2526

2627
export function quitApp(): void {
27-
ipcRenderer.send('gitify:quit');
28+
ipcRenderer.send(namespacedEvent('quit'));
2829
}
2930

3031
export function showWindow(): void {
31-
ipcRenderer.send('gitify:window-show');
32+
ipcRenderer.send(namespacedEvent('window-show'));
3233
}
3334

3435
export function hideWindow(): void {
35-
ipcRenderer.send('gitify:window-hide');
36+
ipcRenderer.send(namespacedEvent('window-hide'));
3637
}
3738

3839
export function setAutoLaunch(value: boolean): void {
39-
ipcRenderer.send('gitify:update-auto-launch', {
40+
ipcRenderer.send(namespacedEvent('update-auto-launch'), {
4041
openAtLogin: value,
4142
openAsHidden: value,
4243
});
4344
}
4445

4546
export function setAlternateIdleIcon(value: boolean): void {
46-
ipcRenderer.send('gitify:use-alternate-idle-icon', value);
47+
ipcRenderer.send(namespacedEvent('use-alternate-idle-icon'), value);
4748
}
4849

4950
export function setKeyboardShortcut(keyboardShortcut: boolean): void {
50-
ipcRenderer.send('gitify:update-keyboard-shortcut', {
51+
ipcRenderer.send(namespacedEvent('update-keyboard-shortcut'), {
5152
enabled: keyboardShortcut,
5253
keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT,
5354
});
5455
}
5556

5657
export function updateTrayIcon(notificationsLength = 0): void {
5758
if (notificationsLength > 0) {
58-
ipcRenderer.send('gitify:icon-active');
59+
ipcRenderer.send(namespacedEvent('icon-active'));
5960
} else {
60-
ipcRenderer.send('gitify:icon-idle');
61+
ipcRenderer.send(namespacedEvent('icon-idle'));
6162
}
6263
}
6364

6465
export function updateTrayTitle(title = ''): void {
65-
ipcRenderer.send('gitify:update-title', title);
66+
ipcRenderer.send(namespacedEvent('update-title'), title);
6667
}

src/shared/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ export const APPLICATION = {
33

44
NAME: 'Gitify',
55

6+
EVENT_PREFIX: 'gitify:',
7+
68
WEBSITE: 'https://gitify.io',
79
};

src/shared/events.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { APPLICATION } from './constants';
2+
3+
export function namespacedEvent(event: string) {
4+
return `${APPLICATION.EVENT_PREFIX}${event}`;
5+
}

0 commit comments

Comments
 (0)