Skip to content

refactor: namespaced events #1738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import log from 'electron-log';
import { menubar } from 'menubar';

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

nativeTheme.on('updated', () => {
if (nativeTheme.shouldUseDarkColors) {
mb.window.webContents.send('gitify:update-theme', 'DARK');
mb.window.webContents.send(namespacedEvent('update-theme'), 'DARK');
} else {
mb.window.webContents.send('gitify:update-theme', 'LIGHT');
mb.window.webContents.send(namespacedEvent('update-theme'), 'LIGHT');
}
});

/**
* Gitify custom IPC events
*/
ipc.handle('gitify:version', () => app.getVersion());
ipc.handle(namespacedEvent('version'), () => app.getVersion());

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

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

ipc.on('gitify:quit', () => mb.app.quit());
ipc.on(namespacedEvent('quit'), () => mb.app.quit());

ipc.on('gitify:use-alternate-idle-icon', (_, useAlternateIdleIcon) => {
shouldUseAlternateIdleIcon = useAlternateIdleIcon;
});
ipc.on(
namespacedEvent('use-alternate-idle-icon'),
(_, useAlternateIdleIcon) => {
shouldUseAlternateIdleIcon = useAlternateIdleIcon;
},
);

ipc.on('gitify:icon-active', () => {
ipc.on(namespacedEvent('icon-active'), () => {
if (!mb.tray.isDestroyed()) {
mb.tray.setImage(
menuBuilder.isUpdateAvailable()
Expand All @@ -124,7 +128,7 @@ app.whenReady().then(async () => {
}
});

ipc.on('gitify:icon-idle', () => {
ipc.on(namespacedEvent('icon-idle'), () => {
if (!mb.tray.isDestroyed()) {
if (shouldUseAlternateIdleIcon) {
mb.tray.setImage(
Expand All @@ -142,14 +146,14 @@ app.whenReady().then(async () => {
}
});

ipc.on('gitify:update-title', (_, title) => {
ipc.on(namespacedEvent('update-title'), (_, title) => {
if (!mb.tray.isDestroyed()) {
mb.tray.setTitle(title);
}
});

ipc.on(
'gitify:update-keyboard-shortcut',
namespacedEvent('update-keyboard-shortcut'),
(_, { enabled, keyboardShortcut }) => {
if (!enabled) {
globalShortcut.unregister(keyboardShortcut);
Expand All @@ -166,7 +170,7 @@ app.whenReady().then(async () => {
},
);

ipc.on('gitify:update-auto-launch', (_, settings) => {
ipc.on(namespacedEvent('update-auto-launch'), (_, settings) => {
app.setLoginItemSettings(settings);
});
});
3 changes: 2 additions & 1 deletion src/main/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { dialog, shell } from 'electron';
import log from 'electron-log';
import type { Menubar } from 'menubar';

import { namespacedEvent } from '../shared/events';
import { logError, logInfo } from '../shared/logger';

export function takeScreenshot(mb: Menubar) {
Expand Down Expand Up @@ -34,7 +35,7 @@ export function resetApp(mb: Menubar) {
});

if (response === resetButtonId) {
mb.window.webContents.send('gitify:reset-app');
mb.window.webContents.send(namespacedEvent('reset-app'));
mb.app.quit();
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/__mocks__/electron.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { namespacedEvent } = require('../../shared/events');

// @ts-ignore
window.Notification = function (title) {
this.title = title;
Expand Down Expand Up @@ -39,7 +41,7 @@ module.exports = {
switch (channel) {
case 'get-platform':
return Promise.resolve('darwin');
case 'gitify:version':
case namespacedEvent('version'):
return Promise.resolve('0.0.1');
default:
return Promise.reject(new Error(`Unknown channel: ${channel}`));
Expand Down
14 changes: 9 additions & 5 deletions src/renderer/components/settings/AppearanceSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@primer/octicons-react';
import { ipcRenderer, webFrame } from 'electron';
import { type FC, useContext, useEffect, useState } from 'react';
import { namespacedEvent } from '../../../shared/events';
import { AppContext } from '../../context/App';
import { Size, Theme } from '../../types';
import { hasMultipleAccounts } from '../../utils/auth/utils';
Expand All @@ -29,11 +30,14 @@ export const AppearanceSettings: FC = () => {
);

useEffect(() => {
ipcRenderer.on('gitify:update-theme', (_, updatedTheme: Theme) => {
if (settings.theme === Theme.SYSTEM) {
setTheme(updatedTheme);
}
});
ipcRenderer.on(
namespacedEvent('update-theme'),
(_, updatedTheme: Theme) => {
if (settings.theme === Theme.SYSTEM) {
setTheme(updatedTheme);
}
},
);
}, [settings.theme]);

window.addEventListener('resize', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useMemo,
useState,
} from 'react';
import { namespacedEvent } from '../../shared/events';
import { useInterval } from '../hooks/useInterval';
import { useNotifications } from '../hooks/useNotifications';
import {
Expand Down Expand Up @@ -172,7 +173,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
}, [settings.keyboardShortcut]);

useEffect(() => {
ipcRenderer.on('gitify:reset-app', () => {
ipcRenderer.on(namespacedEvent('reset-app'), () => {
clearState();
setAuth(defaultAuth);
setSettings(defaultSettings);
Expand Down
48 changes: 31 additions & 17 deletions src/renderer/utils/comms.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ipcRenderer, shell } from 'electron';

import { namespacedEvent } from '../../shared/events';
import { mockSettings } from '../__mocks__/state-mocks';
import type { Link } from '../types';
import {
Expand Down Expand Up @@ -63,50 +65,60 @@ describe('renderer/utils/comms.ts', () => {
it('should get app version', async () => {
await getAppVersion();
expect(ipcRenderer.invoke).toHaveBeenCalledTimes(1);
expect(ipcRenderer.invoke).toHaveBeenCalledWith('gitify:version');
expect(ipcRenderer.invoke).toHaveBeenCalledWith(namespacedEvent('version'));
});

it('should quit the app', () => {
quitApp();
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:quit');
expect(ipcRenderer.send).toHaveBeenCalledWith(namespacedEvent('quit'));
});

it('should show the window', () => {
showWindow();
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:window-show');
expect(ipcRenderer.send).toHaveBeenCalledWith(
namespacedEvent('window-show'),
);
});

it('should hide the window', () => {
hideWindow();
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:window-hide');
expect(ipcRenderer.send).toHaveBeenCalledWith(
namespacedEvent('window-hide'),
);
});

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

expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:update-auto-launch', {
openAtLogin: true,
openAsHidden: true,
});
expect(ipcRenderer.send).toHaveBeenCalledWith(
namespacedEvent('update-auto-launch'),
{
openAtLogin: true,
openAsHidden: true,
},
);
});

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

expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:update-auto-launch', {
openAsHidden: false,
openAtLogin: false,
});
expect(ipcRenderer.send).toHaveBeenCalledWith(
namespacedEvent('update-auto-launch'),
{
openAsHidden: false,
openAtLogin: false,
},
);
});

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

expect(ipcRenderer.send).toHaveBeenCalledWith(
'gitify:use-alternate-idle-icon',
namespacedEvent('use-alternate-idle-icon'),
true,
);
});
Expand All @@ -115,7 +127,7 @@ describe('renderer/utils/comms.ts', () => {
setKeyboardShortcut(true);
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith(
'gitify:update-keyboard-shortcut',
namespacedEvent('update-keyboard-shortcut'),
{
enabled: true,
keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT,
Expand All @@ -127,7 +139,7 @@ describe('renderer/utils/comms.ts', () => {
setKeyboardShortcut(false);
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith(
'gitify:update-keyboard-shortcut',
namespacedEvent('update-keyboard-shortcut'),
{
enabled: false,
keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT,
Expand All @@ -139,13 +151,15 @@ describe('renderer/utils/comms.ts', () => {
const notificationsLength = 3;
updateTrayIcon(notificationsLength);
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:icon-active');
expect(ipcRenderer.send).toHaveBeenCalledWith(
namespacedEvent('icon-active'),
);
});

it('should send mark the icons as idle', () => {
const notificationsLength = 0;
updateTrayIcon(notificationsLength);
expect(ipcRenderer.send).toHaveBeenCalledTimes(1);
expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:icon-idle');
expect(ipcRenderer.send).toHaveBeenCalledWith(namespacedEvent('icon-idle'));
});
});
21 changes: 11 additions & 10 deletions src/renderer/utils/comms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ipcRenderer, shell } from 'electron';
import { namespacedEvent } from '../../shared/events';
import { defaultSettings } from '../context/App';
import { type Link, OpenPreference } from '../types';
import { Constants } from './constants';
Expand All @@ -20,47 +21,47 @@ export function openExternalLink(url: Link): void {
}

export async function getAppVersion(): Promise<string> {
return await ipcRenderer.invoke('gitify:version');
return await ipcRenderer.invoke(namespacedEvent('version'));
}

export function quitApp(): void {
ipcRenderer.send('gitify:quit');
ipcRenderer.send(namespacedEvent('quit'));
}

export function showWindow(): void {
ipcRenderer.send('gitify:window-show');
ipcRenderer.send(namespacedEvent('window-show'));
}

export function hideWindow(): void {
ipcRenderer.send('gitify:window-hide');
ipcRenderer.send(namespacedEvent('window-hide'));
}

export function setAutoLaunch(value: boolean): void {
ipcRenderer.send('gitify:update-auto-launch', {
ipcRenderer.send(namespacedEvent('update-auto-launch'), {
openAtLogin: value,
openAsHidden: value,
});
}

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

export function setKeyboardShortcut(keyboardShortcut: boolean): void {
ipcRenderer.send('gitify:update-keyboard-shortcut', {
ipcRenderer.send(namespacedEvent('update-keyboard-shortcut'), {
enabled: keyboardShortcut,
keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT,
});
}

export function updateTrayIcon(notificationsLength = 0): void {
if (notificationsLength > 0) {
ipcRenderer.send('gitify:icon-active');
ipcRenderer.send(namespacedEvent('icon-active'));
} else {
ipcRenderer.send('gitify:icon-idle');
ipcRenderer.send(namespacedEvent('icon-idle'));
}
}

export function updateTrayTitle(title = ''): void {
ipcRenderer.send('gitify:update-title', title);
ipcRenderer.send(namespacedEvent('update-title'), title);
}
2 changes: 2 additions & 0 deletions src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ export const APPLICATION = {

NAME: 'Gitify',

EVENT_PREFIX: 'gitify:',

WEBSITE: 'https://gitify.io',
};
5 changes: 5 additions & 0 deletions src/shared/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { APPLICATION } from './constants';

export function namespacedEvent(event: string) {
return `${APPLICATION.EVENT_PREFIX}${event}`;
}
Loading