Refacor electron app and add migrations
This commit is contained in:
39
electron/ipc/cqrs.ts
Normal file
39
electron/ipc/cqrs.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { ipcMain } from 'electron';
|
||||
import { buildCommandHandlers } from '../cqrs/commands';
|
||||
import { buildQueryHandlers } from '../cqrs/queries';
|
||||
import {
|
||||
Command,
|
||||
Query,
|
||||
CommandTypeKey,
|
||||
QueryTypeKey
|
||||
} from '../cqrs/types';
|
||||
import { getDataSource } from '../db/database';
|
||||
|
||||
export function setupCqrsHandlers(): void {
|
||||
const dataSource = getDataSource();
|
||||
|
||||
if (!dataSource) {
|
||||
throw new Error('DataSource not initialised');
|
||||
}
|
||||
|
||||
const commandHandlerMap = buildCommandHandlers(dataSource) as Record<CommandTypeKey, (command: Command) => unknown>;
|
||||
const queryHandlerMap = buildQueryHandlers(dataSource) as Record<QueryTypeKey, (query: Query) => unknown>;
|
||||
|
||||
ipcMain.handle('cqrs:command', async (_evt, command: Command) => {
|
||||
const handler = commandHandlerMap[command.type as CommandTypeKey];
|
||||
|
||||
if (!handler)
|
||||
throw new Error(`No command handler for type: ${command.type}`);
|
||||
|
||||
return handler(command);
|
||||
});
|
||||
|
||||
ipcMain.handle('cqrs:query', async (_evt, query: Query) => {
|
||||
const handler = queryHandlerMap[query.type as QueryTypeKey];
|
||||
|
||||
if (!handler)
|
||||
throw new Error(`No query handler for type: ${query.type}`);
|
||||
|
||||
return handler(query);
|
||||
});
|
||||
}
|
||||
3
electron/ipc/index.ts
Normal file
3
electron/ipc/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { setupCqrsHandlers } from './cqrs';
|
||||
export { setupSystemHandlers } from './system';
|
||||
export { setupWindowControlHandlers } from './window-controls';
|
||||
61
electron/ipc/system.ts
Normal file
61
electron/ipc/system.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
app,
|
||||
desktopCapturer,
|
||||
ipcMain,
|
||||
shell
|
||||
} from 'electron';
|
||||
import * as fs from 'fs';
|
||||
import * as fsp from 'fs/promises';
|
||||
|
||||
export function setupSystemHandlers(): void {
|
||||
ipcMain.handle('open-external', async (_event, url: string) => {
|
||||
if (typeof url === 'string' && (url.startsWith('http://') || url.startsWith('https://'))) {
|
||||
await shell.openExternal(url);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
ipcMain.handle('get-sources', async () => {
|
||||
const sources = await desktopCapturer.getSources({
|
||||
types: ['window', 'screen'],
|
||||
thumbnailSize: { width: 150, height: 150 }
|
||||
});
|
||||
|
||||
return sources.map((source) => ({
|
||||
id: source.id,
|
||||
name: source.name,
|
||||
thumbnail: source.thumbnail.toDataURL()
|
||||
}));
|
||||
});
|
||||
|
||||
ipcMain.handle('get-app-data-path', () => app.getPath('userData'));
|
||||
|
||||
ipcMain.handle('file-exists', async (_event, filePath: string) => {
|
||||
try {
|
||||
await fsp.access(filePath, fs.constants.F_OK);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('read-file', async (_event, filePath: string) => {
|
||||
const data = await fsp.readFile(filePath);
|
||||
|
||||
return data.toString('base64');
|
||||
});
|
||||
|
||||
ipcMain.handle('write-file', async (_event, filePath: string, base64Data: string) => {
|
||||
const buffer = Buffer.from(base64Data, 'base64');
|
||||
|
||||
await fsp.writeFile(filePath, buffer);
|
||||
return true;
|
||||
});
|
||||
|
||||
ipcMain.handle('ensure-dir', async (_event, dirPath: string) => {
|
||||
await fsp.mkdir(dirPath, { recursive: true });
|
||||
return true;
|
||||
});
|
||||
}
|
||||
21
electron/ipc/window-controls.ts
Normal file
21
electron/ipc/window-controls.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { ipcMain } from 'electron';
|
||||
import { getMainWindow } from '../window/create-window';
|
||||
|
||||
export function setupWindowControlHandlers(): void {
|
||||
ipcMain.on('window-minimize', () => {
|
||||
getMainWindow()?.minimize();
|
||||
});
|
||||
|
||||
ipcMain.on('window-maximize', () => {
|
||||
const win = getMainWindow();
|
||||
|
||||
if (win?.isMaximized())
|
||||
win.unmaximize();
|
||||
else
|
||||
win?.maximize();
|
||||
});
|
||||
|
||||
ipcMain.on('window-close', () => {
|
||||
getMainWindow()?.close();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user