Skip to content

Commit a0fb2f9

Browse files
committed
code improvements
1 parent 5ba9723 commit a0fb2f9

File tree

2 files changed

+99
-79
lines changed

2 files changed

+99
-79
lines changed

src/main.ts

Lines changed: 87 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ const production = process.env.NODE_ENV === 'production';
1414
const development = process.env.NODE_ENV === 'development';
1515
const OS_Windows = process.platform == 'win32';
1616

17-
import { app, BrowserWindow, ipcMain, dialog, shell, Menu } from "electron";
17+
import { app, BrowserWindow, ipcMain, dialog, shell, Menu, session } from "electron";
1818
import * as path from "path";
1919
import * as fs from "fs";
2020
import * as webr from "webr";
2121
import * as interfaces from './library/interfaces';
2222
import { util } from "./library/helpers";
23-
import * as comm from "./communication";
2423

2524

2625
const webR = new webr.WebR({ interactive: false });
2726
let mainWindow: BrowserWindow;
28-
const root = production ? "../../" : "../";
27+
// const root = production ? "../../" : "../";
2928

3029
async function mount(dir: string) {
3130
webR.FS.unmount("/host");
@@ -36,45 +35,51 @@ async function mount(dir: string) {
3635
{ root: dir },
3736
"/host"
3837
);
39-
return(true)
4038
} catch (error) {
41-
return false;
39+
console.log(error);
40+
throw error;
4241
}
4342
}
4443

4544
async function initWebR() {
46-
await webR.init();
47-
48-
// mount a virtual filesystem containing contributed R packages
49-
const data = new Blob([
50-
fs.readFileSync(
51-
path.join(__dirname, '../src/library/R/library.data')
52-
)
53-
]);
54-
55-
const metadata = JSON.parse(
56-
fs.readFileSync(
57-
path.join(__dirname, '../src/library/R/library.js.metadata'),
58-
'utf-8'
59-
)
60-
);
61-
62-
const options = {
63-
packages: [{
64-
blob: data,
65-
metadata: metadata,
66-
}]
67-
};
68-
69-
await webR.FS.mkdir('/my-library');
70-
await webR.FS.mount(
71-
"WORKERFS",
72-
options,
73-
'/my-library'
74-
);
75-
76-
await webR.evalR(`.libPaths(c(.libPaths(), "/my-library"))`);
77-
await webR.evalR(`library(DDIwR)`);
45+
try {
46+
await webR.init();
47+
48+
console.log(__dirname);
49+
50+
// mount a virtual filesystem containing contributed R packages
51+
const data = new Blob([
52+
fs.readFileSync(
53+
path.join(__dirname, '../src/library/R/library.data')
54+
)
55+
]);
56+
57+
const metadata = JSON.parse(
58+
fs.readFileSync(
59+
path.join(__dirname, '../src/library/R/library.js.metadata'),
60+
'utf-8'
61+
)
62+
);
63+
64+
const options = {
65+
packages: [{
66+
blob: data,
67+
metadata: metadata,
68+
}]
69+
};
70+
71+
await webR.FS.mkdir('/my-library');
72+
await webR.FS.mount(
73+
"WORKERFS",
74+
options,
75+
'/my-library'
76+
);
77+
78+
await webR.evalR(`.libPaths(c(.libPaths(), "/my-library"))`);
79+
await webR.evalR(`library(DDIwR)`);
80+
} catch (error) {
81+
throw error;
82+
}
7883
}
7984

8085
// Create the main browser window
@@ -110,15 +115,9 @@ function createWindow() {
110115

111116
}
112117

113-
app.whenReady().then(async () => {
114-
118+
app.whenReady().then(() => {
115119
createWindow();
116-
117-
try {
118-
await initWebR();
119-
} catch (error) {
120-
console.error("Error during initialization:", error);
121-
}
120+
initWebR();
122121
});
123122

124123

@@ -156,9 +155,9 @@ ipcMain.on("declared", () => {
156155
});
157156

158157

159-
ipcMain.on("selectFileFrom", async (event, args) => {
158+
ipcMain.on("selectFileFrom", (event, args) => {
160159
if (args.inputType === "Select file type") {
161-
comm.showError("Select input type");
160+
dialog.showErrorBox("Error", "Select input type");
162161
} else {
163162
const info = util.fileFromInfo(args.inputType);
164163

@@ -171,7 +170,8 @@ ipcMain.on("selectFileFrom", async (event, args) => {
171170
},
172171
],
173172
properties: ["openFile"],
174-
}).then((result) => {
173+
}).then(async (result) => {
174+
// if (!result.canceled) {
175175
if (!result.canceled) {
176176
inputOutput.fileFrom = result.filePaths[0];
177177

@@ -184,20 +184,14 @@ ipcMain.on("selectFileFrom", async (event, args) => {
184184

185185
inputOutput.fileFromExt = ext;
186186

187-
mount(inputOutput.fileFromDir).then((result) => {
188-
if (result) {
189-
// consolog(inputOutput);
190-
event.reply("selectFileFrom-reply", inputOutput);
191-
} else {
192-
comm.showError("Could not mount the directory");
193-
}
194-
195-
});
196-
197187
if (OS_Windows) {
198188
inputOutput.fileFrom = inputOutput.fileFrom.replace(/\\/g, '/');
199189
inputOutput.fileFromDir = inputOutput.fileFromDir.replace(/\\/g, '/');
200190
}
191+
192+
mount(inputOutput.fileFromDir).then(() => {
193+
mainWindow.webContents.send("selectFileFrom-reply", inputOutput);
194+
});
201195
}
202196
});
203197

@@ -211,7 +205,7 @@ ipcMain.on("outputType", (event, args) => {
211205

212206
ipcMain.on("selectFileTo", (event, args) => {
213207
if (args.outputType === "Select file type") {
214-
comm.showError("Select output type");
208+
dialog.showErrorBox("Error", "Select output type");
215209
} else {
216210
const ext = util.getExtensionFromType(args.outputType);
217211

@@ -239,11 +233,11 @@ ipcMain.on("selectFileTo", (event, args) => {
239233
inputOutput.fileToDir = inputOutput.fileToDir.replace(/\\/g, '/');
240234
}
241235

242-
event.reply("selectFileTo-reply", inputOutput);
236+
mainWindow.webContents.send("selectFileTo-reply", inputOutput);
243237
}
244238
})
245239
.catch((err) => {
246-
console.log(err);
240+
consolog(err);
247241
});
248242
}
249243
});
@@ -260,13 +254,20 @@ ipcMain.on("declared", () => {
260254

261255
// Handle the command request
262256
ipcMain.on("sendCommand", async (event, args) => {
263-
const command = args.command
264-
console.log(command);
257+
const command = args.command;
265258
mainWindow.webContents.send("startLoader");
259+
// consolog("main266: " + command);
260+
266261
try {
267262
await webR.evalR(command);
268-
if (args.variables) {
263+
} catch (error) {
264+
console.log(error);
265+
throw error;
266+
}
269267

268+
if (util.isTrue(args.updateVariables)) {
269+
// consolog("main: updating variables");
270+
try {
270271
const result = await webR.evalR(`as.character(jsonlite::toJSON(lapply(
271272
collectRMetadata(dataset),
272273
function(x) {
@@ -277,20 +278,20 @@ ipcMain.on("sendCommand", async (event, args) => {
277278
}
278279
)))`);
279280

280-
if (!webr.isRCharacter(result)) {
281-
comm.showError('Not a character!');
282-
}
281+
if (!webr.isRCharacter(result)) throw new Error('Not a character!');
283282

284283
const response = await result.toString();
285-
// consolog(JSON.parse(response[0] as string));
286284
webR.destroy(result);
285+
mainWindow.webContents.send("updateVariables", JSON.parse(response));
287286

288-
event.reply("sendCommand-reply", JSON.parse(response));
287+
} catch (error) {
288+
console.log(error);
289+
throw error;
289290
}
290-
} catch (error) {
291-
comm.showError(String(error));
292291
}
292+
293293
mainWindow.webContents.send("clearLoader");
294+
294295
});
295296

296297
const inputOutput: interfaces.InputOutput = {
@@ -307,6 +308,20 @@ const inputOutput: interfaces.InputOutput = {
307308
fileToExt: ""
308309
};
309310

311+
310312
function consolog(x: any) {
311313
mainWindow.webContents.send("consolog", x);
312-
}
314+
}
315+
316+
317+
function consoletrace(x: any) {
318+
mainWindow.webContents.send("consoletrace", x);
319+
}
320+
321+
322+
process.on('unhandledRejection', (error: Error, promise) => {
323+
consoletrace(error);
324+
});
325+
326+
327+
app.commandLine.appendSwitch('log-level', '3');

src/preload.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ window.addEventListener('DOMContentLoaded', () => {
5151
(<HTMLDivElement>document.getElementById('cover')).classList.add('d-none');
5252
});
5353

54-
// ipcRenderer.send('sendCommand', 'require(DDIwR)');
55-
5654
let all_vars_selected = true;
5755
const variables: interfaces.Variables = {};
5856

@@ -113,11 +111,11 @@ window.addEventListener('DOMContentLoaded', () => {
113111
}
114112

115113
command += ")";
116-
console.log(command);
114+
console.log("preload116: ", command);
117115

118116
ipcRenderer.send('sendCommand', {
119117
command: command.replace(/\\/g, '/'),
120-
variables: true
118+
updateVariables: true
121119
});
122120

123121
fileFrom.value = io.fileFrom;
@@ -311,7 +309,7 @@ window.addEventListener('DOMContentLoaded', () => {
311309

312310
ipcRenderer.send('sendCommand', {
313311
command: command.replace(/\\/g, '/'),
314-
variables: false
312+
updateVariables: false
315313
});
316314
}
317315
});
@@ -329,7 +327,10 @@ window.addEventListener('DOMContentLoaded', () => {
329327
}
330328

331329
command += ")";
332-
ipcRenderer.send('sendCommand', command.replace(/\\/g, '/'));
330+
ipcRenderer.send('sendCommand', {
331+
command: command.replace(/\\/g, '/'),
332+
updateVariables: true
333+
});
333334
}
334335
});
335336

@@ -373,7 +374,7 @@ window.addEventListener('DOMContentLoaded', () => {
373374

374375
// =================================================
375376
// ================= Variables =====================
376-
ipcRenderer.on('sendCommand-reply', (event, variables) => {
377+
ipcRenderer.on('updateVariables', (event, variables) => {
377378
// console.log(variables);
378379
//load variable list
379380
const variablesList = util.htmlElement('variables');
@@ -627,3 +628,7 @@ function insertAtPosition(areaId: string, text: string) {
627628
ipcRenderer.on('consolog', (event, object: any) => {
628629
console.log(object);
629630
});
631+
632+
ipcRenderer.on('consoletrace', (event, object: any) => {
633+
console.trace(object);
634+
});

0 commit comments

Comments
 (0)