Process Architecture
FitFileViewer uses Electron's multi-process architecture for security and performance.
Process Overviewβ
Main Processβ
The main process (main.js) handles:
App Lifecycleβ
app.whenReady().then(() => {
createWindow();
setupMenu();
setupIpcHandlers();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
Window Managementβ
- Creating browser windows
- Window state persistence
- Multi-window support
File System Accessβ
- File dialogs (open/save)
- File reading
- Recent files management
IPC Handlersβ
ipcMain.handle('dialog:open-fit-file', async () => {
const result = await dialog.showOpenDialog({
filters: [{ name: 'FIT Files', extensions: ['fit'] }]
});
return result;
});
Preload Scriptβ
The preload script (preload.js) bridges main and renderer:
Context Bridgeβ
contextBridge.exposeInMainWorld('electronAPI', {
openFile: () => ipcRenderer.invoke('dialog:open-fit-file'),
onFileOpened: (callback) => ipcRenderer.on('file:opened', callback),
// Limited, validated operations only
});
Securityβ
- Validates all messages
- Sanitizes data
- Prevents prototype pollution
Renderer Processβ
The renderer process handles the UI:
Entry Point (renderer.js)β
// Initialize application
document.addEventListener('DOMContentLoaded', () => {
initializeApp();
setupEventListeners();
loadTheme();
});
UI Management (main-ui.js)β
- Tab navigation
- User interactions
- View updates
Module Loadingβ
// Dynamic imports for code splitting
const { renderMap } = await import('./utils/maps/renderMap.js');
const { renderChart } = await import('./utils/charts/renderChart.js');
IPC Communicationβ
Main β Rendererβ
// Main process
mainWindow.webContents.send('file:opened', fileData);
// Renderer (via preload)
window.electronAPI.onFileOpened((event, data) => {
processFileData(data);
});
Renderer β Mainβ
// Renderer
const result = await window.electronAPI.openFile();
// Main process
ipcMain.handle('dialog:open-fit-file', async () => {
return dialog.showOpenDialog(options);
});
Security Isolationβ
Context Isolationβ
// main.js - Window creation
const mainWindow = new BrowserWindow({
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
preload: path.join(__dirname, 'preload.js')
}
});
Why This Mattersβ
| Setting | Value | Purpose |
|---|---|---|
| contextIsolation | true | Separate JS contexts |
| nodeIntegration | false | No Node in renderer |
| sandbox | true | OS-level isolation |
Process Communication Flowβ
Next: Module System β