Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
Export ipc handlers to dedicated file
Browse files Browse the repository at this point in the history
  • Loading branch information
binarybaron committed Jun 12, 2023
1 parent 4bdc9a3 commit af2f3ac
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/main/blockchain/electrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
transactionsStatusChanged,
} from '../../store/features/electrumSlice';
import { ELECTRUM_PROBE_TRANSACTIONS, ELECTRUM_SERVERS } from './electrumData';
import { sendSnackbarAlertToRenderer } from '../main';
import { sendSnackbarAlertToRenderer } from '../ipc';

const REPROBE_DELAY_MS = 60 * 1000;

Expand Down
86 changes: 86 additions & 0 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { ipcMain } from 'electron';
import { stopCli } from './cli/cli';
import spawnBalanceCheck from './cli/commands/balanceCommand';
import { resumeBuyXmr, spawnBuyXmr } from './cli/commands/buyXmrCommand';
import spawnCancelRefund from './cli/commands/cancelRefundCommand';
import spawnWithdrawBtc from './cli/commands/withdrawBtcCommand';
import spawnListSellersCommand from './cli/commands/listSellersCommand';
import { getCliLogFile } from './cli/dirs';
import { spawnTor, stopTor } from './tor';
import logger from '../utils/logger';
import { mainWindow } from './main';

export default function registerIpcHandlers() {
ipcMain.handle('stop-cli', stopCli);

ipcMain.handle('spawn-balance-check', spawnBalanceCheck);

ipcMain.handle(
'spawn-buy-xmr',
(_event, provider, redeemAddress, refundAddress) =>
spawnBuyXmr(provider, redeemAddress, refundAddress)
);

ipcMain.handle('resume-buy-xmr', (_event, swapId) => resumeBuyXmr(swapId));

ipcMain.handle('spawn-cancel-refund', (_event, swapId) =>
spawnCancelRefund(swapId)
);

ipcMain.handle('spawn-withdraw-btc', (_event, address) =>
spawnWithdrawBtc(address)
);

ipcMain.handle('spawn-list-sellers', (_event, rendezvousPointAddress) =>
spawnListSellersCommand(rendezvousPointAddress)
);

ipcMain.handle('get-cli-log-path', (_event, swapId) => getCliLogFile(swapId));

ipcMain.handle('spawn-tor', spawnTor);

ipcMain.handle('stop-tor', stopTor);
}

export function sendSnackbarAlertToRenderer(
message: string,
variant: string,
autoHideDuration: number | null,
key: string | null
) {
function send() {
logger.debug(
{ message, variant, autoHideDuration, key },
'Attempting to send snackbar alert to renderer'
);
if (mainWindow) {
if (
mainWindow.webContents.isDestroyed() ||
mainWindow.webContents.isLoading()
) {
logger.debug(
'Main window is loading, waiting for it to finish before sending snackbar alert'
);
mainWindow.webContents.once('did-finish-load', () =>
setTimeout(send, 5000)
);
} else {
logger.debug(
{ message, variant, autoHideDuration, key },
'Sending snackbar alert to renderer'
);
mainWindow?.webContents.send(
'display-snackbar-alert',
message,
variant,
autoHideDuration,
key
);
}
} else {
setTimeout(send, 1000);
}
}

send();
}
87 changes: 6 additions & 81 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,24 @@
*/
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import { app, BrowserWindow, ipcMain, shell } from 'electron';
import { app, BrowserWindow, shell } from 'electron';
import blocked from 'blocked-at';
import { resolveHtmlPath } from './util';
import { stopCli } from './cli/cli';
import spawnBalanceCheck from './cli/commands/balanceCommand';
import { resumeBuyXmr, spawnBuyXmr } from './cli/commands/buyXmrCommand';
import spawnWithdrawBtc from './cli/commands/withdrawBtcCommand';
import watchDatabase from './cli/database';
import { getPlatform, isDevelopment } from '../store/config';
import { getAssetPath, fixAppDataPath, getCliLogFile } from './cli/dirs';
import { getAssetPath, fixAppDataPath } from './cli/dirs';
import initSocket from './socket';
import logger from '../utils/logger';
import watchElectrumTransactions from './blockchain/electrum';
import watchLogs from './cli/log';
import spawnListSellersCommand from './cli/commands/listSellersCommand';
import { spawnTor, stopTor } from './tor';
import spawnCancelRefund from './cli/commands/cancelRefundCommand';
import { stopTor } from './tor';
import initAutoUpdater from './updater';
import initStats from './stats';
import registerIpcHandlers from './ipc';

let mainWindow: BrowserWindow | null = null;
export let mainWindow: BrowserWindow | null = null;

async function installExtensions() {
const installer = require('electron-devtools-installer');
Expand Down Expand Up @@ -128,6 +125,7 @@ if (gotTheLock) {
.whenReady()
.then(async () => {
createWindow();
registerIpcHandlers();
initSocket();
watchDatabase();
spawnBalanceCheck();
Expand Down Expand Up @@ -157,76 +155,3 @@ if (isDevelopment) {
}
);
}

export function sendSnackbarAlertToRenderer(
message: string,
variant: string,
autoHideDuration: number | null,
key: string | null
) {
function send() {
logger.debug(
{ message, variant, autoHideDuration, key },
'Attempting to send snackbar alert to renderer'
);
if (mainWindow) {
if (
mainWindow.webContents.isDestroyed() ||
mainWindow.webContents.isLoading()
) {
logger.debug(
'Main window is loading, waiting for it to finish before sending snackbar alert'
);
mainWindow.webContents.once('did-finish-load', () =>
setTimeout(send, 5000)
);
} else {
logger.debug(
{ message, variant, autoHideDuration, key },
'Sending snackbar alert to renderer'
);
mainWindow?.webContents.send(
'display-snackbar-alert',
message,
variant,
autoHideDuration,
key
);
}
} else {
setTimeout(send, 1000);
}
}

send();
}

ipcMain.handle('stop-cli', stopCli);

ipcMain.handle('spawn-balance-check', spawnBalanceCheck);

ipcMain.handle(
'spawn-buy-xmr',
(_event, provider, redeemAddress, refundAddress) =>
spawnBuyXmr(provider, redeemAddress, refundAddress)
);

ipcMain.handle('resume-buy-xmr', (_event, swapId) => resumeBuyXmr(swapId));

ipcMain.handle('spawn-cancel-refund', (_event, swapId) =>
spawnCancelRefund(swapId)
);

ipcMain.handle('spawn-withdraw-btc', (_event, address) =>
spawnWithdrawBtc(address)
);

ipcMain.handle('spawn-list-sellers', (_event, rendezvousPointAddress) =>
spawnListSellersCommand(rendezvousPointAddress)
);

ipcMain.handle('get-cli-log-path', (_event, swapId) => getCliLogFile(swapId));

ipcMain.handle('spawn-tor', spawnTor);

ipcMain.handle('stop-tor', stopTor);
2 changes: 1 addition & 1 deletion src/main/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { io, Socket } from 'socket.io-client';
import { app } from 'electron';
import { ExtendedProviderStatus, Provider } from '../models/apiModel';
import logger from '../utils/logger';
import { sendSnackbarAlertToRenderer } from './main';
import { sendSnackbarAlertToRenderer } from './ipc';
import { store } from '../store/store';
import {
increaseFailedRegistryReconnectAttemptsSinceLastSuccess,
Expand Down

0 comments on commit af2f3ac

Please sign in to comment.