|
| 1 | +// Import necessary modules from Electron, the file system, and the path utility. |
| 2 | +const { app, BrowserWindow, ipcMain } = require('electron'); |
| 3 | +const fs = require('fs').promises; |
| 4 | +const path = require('path'); |
| 5 | +// Import the OpenTelemetry tracer you've configured in tracing.js for creating spans. |
| 6 | +const tracer = require('./tracing'); |
| 7 | + |
| 8 | +// Defines an asynchronous function to simulate a time-consuming task. |
| 9 | +async function simulateLongRunningTask() { |
| 10 | + // Start a new span for tracing this task. |
| 11 | + const span = tracer.startSpan('simulateLongRunningTask'); |
| 12 | + try { |
| 13 | + // Simulate a delay of 2000 milliseconds (2 seconds). |
| 14 | + await new Promise(resolve => setTimeout(resolve, 2000)); |
| 15 | + // Record an event indicating the task is completed. |
| 16 | + span.addEvent('Task completed'); |
| 17 | + } catch (error) { |
| 18 | + // Record any exceptions that occur during the task. |
| 19 | + span.recordException(error); |
| 20 | + } finally { |
| 21 | + // End the span once the task is complete or if an error occurred. |
| 22 | + span.end(); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// Defines an asynchronous function to read the contents of a file named 'example.txt'. |
| 27 | +async function readFileContents() { |
| 28 | + // Start a new span for tracing the file reading operation. |
| 29 | + const span = tracer.startSpan('readFileContents'); |
| 30 | + try { |
| 31 | + // Read the contents of the file asynchronously. |
| 32 | + const data = await fs.readFile(path.join(__dirname, 'example.txt'), 'utf8'); |
| 33 | + // Log the data to the console. |
| 34 | + console.log(data); |
| 35 | + // Record an event indicating the file was read successfully. |
| 36 | + span.addEvent('File read successfully'); |
| 37 | + } catch (error) { |
| 38 | + // Record any exceptions that occur while reading the file. |
| 39 | + span.recordException(error); |
| 40 | + console.error('Error reading file:', error); |
| 41 | + } finally { |
| 42 | + // End the span once the operation is complete or if an error occurred. |
| 43 | + span.end(); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Defines an asynchronous function to create a new browser window and load the HTML file. |
| 48 | +async function createWindow() { |
| 49 | + // Start a new span for tracing the application startup process. |
| 50 | + const span = tracer.startSpan('ApplicationStart'); |
| 51 | + // Create a new browser window with specific dimensions and web preferences. |
| 52 | + const win = new BrowserWindow({ |
| 53 | + width: 800, |
| 54 | + height: 600, |
| 55 | + webPreferences: { |
| 56 | + nodeIntegration: true, |
| 57 | + contextIsolation: false, // Important for security, see Electron documentation. |
| 58 | + }, |
| 59 | + }); |
| 60 | + // Load an HTML file into the window. |
| 61 | + win.loadFile('index.html'); |
| 62 | + |
| 63 | + // Perform additional tasks as part of the startup process. |
| 64 | + await simulateLongRunningTask(); |
| 65 | + await readFileContents(); |
| 66 | + |
| 67 | + // End the span for the application startup process. |
| 68 | + span.end(); |
| 69 | +} |
| 70 | + |
| 71 | +// Listen for the 'whenReady' event to create the browser window. |
| 72 | +app.whenReady().then(createWindow); |
| 73 | + |
| 74 | +// Listen for 'button-click' messages sent from renderer processes. |
| 75 | +ipcMain.on('button-click', async () => { |
| 76 | + // Start a new span for tracing the button click action. |
| 77 | + const span = tracer.startSpan('buttonClick'); |
| 78 | + try { |
| 79 | + // Simulate processing related to the button click. |
| 80 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 81 | + // Record an event indicating the action related to the button click is completed. |
| 82 | + span.addEvent('Button action completed'); |
| 83 | + } catch (error) { |
| 84 | + // Record any exceptions that occur during the processing. |
| 85 | + span.recordException(error); |
| 86 | + } finally { |
| 87 | + // End the span once the action is complete or if an error occurred. |
| 88 | + span.end(); |
| 89 | + } |
| 90 | +}); |
| 91 | + |
| 92 | +// Listen for the 'window-all-closed' event to quit the application on non-macOS platforms. |
| 93 | +app.on('window-all-closed', () => { |
| 94 | + if (process.platform !== 'darwin') { |
| 95 | + app.quit(); |
| 96 | + } |
| 97 | +}); |
0 commit comments