diff --git a/src/main/blockchain/electrum.ts b/src/main/blockchain/electrum.ts index 82e4da60..4cc88fa0 100644 --- a/src/main/blockchain/electrum.ts +++ b/src/main/blockchain/electrum.ts @@ -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; diff --git a/src/main/ipc.ts b/src/main/ipc.ts new file mode 100644 index 00000000..9f62c78d --- /dev/null +++ b/src/main/ipc.ts @@ -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(); +} diff --git a/src/main/main.ts b/src/main/main.ts index a58e3cba..005292aa 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -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'); @@ -128,6 +125,7 @@ if (gotTheLock) { .whenReady() .then(async () => { createWindow(); + registerIpcHandlers(); initSocket(); watchDatabase(); spawnBalanceCheck(); @@ -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); diff --git a/src/main/socket.ts b/src/main/socket.ts index 97a9a632..fde3806c 100644 --- a/src/main/socket.ts +++ b/src/main/socket.ts @@ -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,