Skip to content

Commit

Permalink
chore(hadron-ipc): refactor to typescript; do not throw if module imp…
Browse files Browse the repository at this point in the history
…ort happens in non-electron environment COMPASS-7361 (#5023)

chore(hadron-ipc): refactor to typescript; do not throw if module import happens in non-electron environment
  • Loading branch information
gribnoysup authored Oct 27, 2023
1 parent 8874067 commit 0ca8110
Show file tree
Hide file tree
Showing 40 changed files with 506 additions and 311 deletions.
132 changes: 118 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/compass-collection/src/stores/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function configureStore({
});

// eslint-disable-next-line @typescript-eslint/no-var-requires
const ipc = require('hadron-ipc');
const { ipcRenderer: ipc } = require('hadron-ipc');

// TODO: importing hadron-ipc in unit tests doesn't work right now
if (ipc?.on) {
Expand Down
4 changes: 2 additions & 2 deletions packages/compass-find-in-page/src/stores/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const _store = createStore(reducer, applyMiddleware(thunk));
// cmd-f in main app emits an ipc event to find results
const store = Object.assign(_store, {
onActivated() {
ipcRenderer.on('app:find', () => {
ipcRenderer?.on('app:find', () => {
if (store.getState().enabled) {
void ipcRenderer.call('app:stop-find-in-page', 'clearSelection');
void ipcRenderer?.call('app:stop-find-in-page', 'clearSelection');
store.dispatch(setSearchTerm(''));
return store.dispatch(toggleStatus());
}
Expand Down
12 changes: 6 additions & 6 deletions packages/compass-home/src/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import type { Cue, GroupCue } from '@mongodb-js/compass-components';
import Connections from '@mongodb-js/compass-connections';
import Welcome from '@mongodb-js/compass-welcome';
import ipc from 'hadron-ipc';
import { ipcRenderer } from 'hadron-ipc';
import type {
DataService,
ReauthenticationHandler,
Expand Down Expand Up @@ -151,15 +151,15 @@ function reducer(state: State, action: Action): State {
}

function showCollectionSubMenu({ isReadonly }: { isReadonly: boolean }) {
void ipc.ipcRenderer?.call('window:show-collection-submenu', { isReadonly });
void ipcRenderer?.call('window:show-collection-submenu', { isReadonly });
}

function hideCollectionSubMenu() {
void ipc.ipcRenderer?.call('window:hide-collection-submenu');
void ipcRenderer?.call('window:hide-collection-submenu');
}

function notifyMainProcessOfDisconnect() {
void ipc.ipcRenderer?.call('compass:disconnected');
void ipcRenderer?.call('compass:disconnected');
}

function Home({
Expand Down Expand Up @@ -280,11 +280,11 @@ function Home({
void handleDisconnectClicked();
}

ipc.ipcRenderer?.on('app:disconnect', onDisconnect);
ipcRenderer?.on('app:disconnect', onDisconnect);

return () => {
// Clean up the ipc listener.
ipc.ipcRenderer?.removeListener('app:disconnect', onDisconnect);
ipcRenderer?.removeListener('app:disconnect', onDisconnect);
};
}, [appRegistry, onDataServiceDisconnected]);

Expand Down
3 changes: 2 additions & 1 deletion packages/compass-logging/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export function createLoggerAndTelemetry(
): LoggerAndTelemetry {
// This application may not be running in an Node.js/Electron context.
const ipc: HadronIpcRenderer | null = isElectronRenderer
? require('hadron-ipc')
? // eslint-disable-next-line @typescript-eslint/no-var-requires
require('hadron-ipc').ipcRenderer
: null;

// Do not create an actual Writable stream here, since the callback
Expand Down
7 changes: 4 additions & 3 deletions packages/compass-preferences-model/src/renderer-ipc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { HadronIpcRenderer } from 'hadron-ipc';
import hadronIpc from 'hadron-ipc';
import { ipcRenderer } from 'hadron-ipc';
import type { PreferencesAccess } from '.';
import type {
AllPreferences,
Expand Down Expand Up @@ -91,5 +91,6 @@ export const makePreferencesIpc = (ipcRenderer: HadronIpcRenderer) => {
};
};

export const preferencesIpc: PreferencesAccess | undefined =
hadronIpc.ipcRenderer ? makePreferencesIpc(hadronIpc.ipcRenderer) : undefined;
export const preferencesIpc: PreferencesAccess | undefined = ipcRenderer
? makePreferencesIpc(ipcRenderer)
: undefined;
6 changes: 2 additions & 4 deletions packages/compass-preferences-model/src/setup-preferences.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Preferences } from './preferences';
import hadronIpc from 'hadron-ipc';
import { ipcMain } from 'hadron-ipc';
import type {
AllPreferences,
PreferenceStateInformation,
Expand Down Expand Up @@ -28,8 +28,6 @@ export async function setupPreferences(

await preferences.setupStorage();

const { ipcMain } = hadronIpc;

if (!ipcMain) {
// Ignore missing ipc if COMPASS_TEST_ env is set, this means that we are in
// a test environment where it's expected not to have ipc
Expand All @@ -41,7 +39,7 @@ export async function setupPreferences(

preferences.onPreferencesChanged(
(changedPreferencesValues: Partial<AllPreferences>) => {
ipcMain.broadcast(
ipcMain?.broadcast(
'compass:preferences-changed',
changedPreferencesValues
);
Expand Down
2 changes: 1 addition & 1 deletion packages/compass/src/app/auto-connect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('auto connection argument parsing', function () {

beforeEach(function () {
sandbox = sinon.createSandbox();
sandbox.stub(ipcRenderer, 'call').resolves(true);
sandbox.stub(ipcRenderer!, 'call').resolves(true);
process.env.COMPASS_E2E_DISABLE_KEYCHAIN_USAGE = 'true';
});

Expand Down
2 changes: 1 addition & 1 deletion packages/compass/src/app/auto-connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ConnectionString } from 'mongodb-connection-string-url';
import type { AutoConnectPreferences } from '../main/auto-connect';

async function getWindowAutoConnectPreferences(): Promise<AutoConnectPreferences> {
return await ipcRenderer.call('compass:get-window-auto-connect-preferences');
return await ipcRenderer?.call('compass:get-window-auto-connect-preferences');
}

function applyUsernameAndPassword(
Expand Down
2 changes: 1 addition & 1 deletion packages/compass/src/app/menu-renderer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const electron = require('electron');
const ipc = require('hadron-ipc');
const { ipcRenderer: ipc } = require('hadron-ipc');

const ZOOM_DEFAULT = 0;
const ZOOM_INCREMENT = 0.5;
Expand Down
6 changes: 3 additions & 3 deletions packages/compass/src/main/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,18 @@ class CompassApplication {
app.exit();
});

ipcMain.respondTo({
ipcMain?.respondTo({
'license:disagree': function () {
debug('Did not agree to license, quitting app.');
app.quit();
},
});

ipcMain.handle('coverage', () => {
ipcMain?.handle('coverage', () => {
return (global as any).__coverage__;
});

ipcMain.handle('compass:appName', () => {
ipcMain?.handle('compass:appName', () => {
return app.getName();
});
}
Expand Down
8 changes: 4 additions & 4 deletions packages/compass/src/main/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async function setupLogging(compassApp: typeof CompassApplication) {
}
);

ipcMain.on('compass:error:fatal', (evt, meta) => {
ipcMain?.on('compass:error:fatal', (evt, meta) => {
writer.fatal(
'COMPASS-MAIN',
mongoLogId(1_001_000_002),
Expand Down Expand Up @@ -141,15 +141,15 @@ async function setupLogging(compassApp: typeof CompassApplication) {
process.emit('compass:log' as any, ev as any);
}

ipcMain.respondTo('compass:log', (evt, meta) => {
ipcMain?.respondTo('compass:log', (evt, meta) => {
(process as EventEmitter).emit('compass:log', meta);
});

ipcMain.handle('compass:logPath', () => {
ipcMain?.handle('compass:logPath', () => {
return app.getPath('logs');
});

ipcMain.handle('compass:userDataPath', () => {
ipcMain?.handle('compass:userDataPath', () => {
return app.getPath('userData');
});

Expand Down
Loading

0 comments on commit 0ca8110

Please sign in to comment.