Skip to content

Commit c8ab568

Browse files
committed
Don't use top-level await.
Indeed, this doesn't go well with Electron since it uses CommonJS which... yeah, doesn't support top-level await.
1 parent 6db7316 commit c8ab568

3 files changed

Lines changed: 42 additions & 9 deletions

File tree

src/preload/index.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import type { ISplashScreenInfo } from '../renderer/src/common/electronApi.ts';
1111

1212
const exec = promisify(_exec);
1313

14-
const _operatingSystem = await (async (): Promise<string> => {
14+
let _operatingSystem: string | null = null;
15+
const defaultOperatingSystem = `${process.platform} (${process.arch === 'x64' ? 'Intel' : 'ARM'})`;
16+
17+
const retrieveOperatingSystem = async (): Promise<string> => {
1518
const safeExec = async (cmd: string): Promise<string | null> => {
1619
try {
1720
const { stdout } = await exec(cmd);
@@ -69,8 +72,19 @@ const _operatingSystem = await (async (): Promise<string> => {
6972

7073
operatingSystem = operatingSystem || 'macOS';
7174
}
72-
return `${operatingSystem || process.platform} (${process.arch === 'x64' ? 'Intel' : 'ARM'})`;
73-
})();
75+
76+
return operatingSystem ? `${operatingSystem} (${process.arch === 'x64' ? 'Intel' : 'ARM'})` : defaultOperatingSystem;
77+
};
78+
79+
// Retrieve the version of the operating system.
80+
81+
retrieveOperatingSystem()
82+
.then((operatingSystem) => {
83+
_operatingSystem = operatingSystem;
84+
})
85+
.catch(() => {
86+
_operatingSystem = defaultOperatingSystem;
87+
});
7488

7589
// Some bridging between our main process and renderer process.
7690
// Note: this must be in sync with src/electronApi.ts.
@@ -79,7 +93,9 @@ electron.contextBridge.exposeInMainWorld('electronApi', {
7993
// Some general methods.
8094

8195
operatingSystem: () => {
82-
return _operatingSystem;
96+
// Return a cached version of the operating system if we have it, otherwise return a default value.
97+
98+
return _operatingSystem || defaultOperatingSystem;
8399
},
84100

85101
// Splash screen window.

src/renderer/src/common/dependencies.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export let _jsZip: Module = null;
88
export let _mathJs: Module = null;
99
export let _plotlyJs: Module = null;
1010
export let _vueTippy: Module = null;
11-
export const _xxhash = await xxhash();
11+
export let _xxhash: Module = null;
1212

1313
export const setJsonSchema = (module: Module): void => {
1414
_jsonSchema = module;
@@ -29,3 +29,9 @@ export const setPlotlyJs = (module: Module): void => {
2929
export const setVueTippy = (module: Module): void => {
3030
_vueTippy = module;
3131
};
32+
33+
export const initialiseXxhash: Promise<Module> = xxhash().then((module: Module) => {
34+
_xxhash = module;
35+
36+
return module;
37+
});

src/renderer/src/common/initialisation.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ const externalDependencies: ExternalDependency[] = [
4545
];
4646

4747
// Some variables to keep track of the initialisation progress.
48-
// Note: the 2 initial steps are to import libOpenCOR's WASM and to instantiate it. Then, we have one or two steps per
49-
// external dependency, depending on whether it has an associated CSS file or not.
48+
// Note: the 2 initial steps are to import libOpenCOR's WASM and to instantiate it. We then have one or two steps per
49+
// external dependency, depending on whether it has an associated CSS file or not. Finally, we have one step to
50+
// initialise xxHash.
5051

5152
const crtNbOfSteps = vue.ref<number>(0);
5253
const totalNbOfSteps =
53-
(electronApi ? 0 : 2) + externalDependencies.reduce((acc, dep) => acc + (dep.url ? 1 : 0) + (dep.cssUrl ? 1 : 0), 0);
54+
(electronApi ? 0 : 2) +
55+
externalDependencies.reduce((acc, dep) => acc + (dep.url ? 1 : 0) + (dep.cssUrl ? 1 : 0), 0) +
56+
1;
5457

5558
// Retrieve the version of libOpenCOR that is to be used. Two options:
5659
// - OpenCOR: libOpenCOR can be accessed using window.locApi, which references our C++ API.
@@ -155,7 +158,7 @@ const initialisationError = (error: unknown): void => {
155158
failed.value = true;
156159
};
157160

158-
// Initialise libOpenCOR and then our external dependencies.
161+
// Initialise libOpenCOR, our external dependencies, and xxHash.
159162

160163
initialiseLocApi().catch((error: unknown) => {
161164
initialisationError(error);
@@ -172,6 +175,14 @@ for (const externalDependency of externalDependencies) {
172175
});
173176
}
174177

178+
dependencies.initialiseXxhash
179+
.then(() => {
180+
++crtNbOfSteps.value;
181+
})
182+
.catch((error: unknown) => {
183+
initialisationError(error);
184+
});
185+
175186
// Let people know whether initialisation is done and how it's progressing.
176187

177188
export const done = vue.computed(() => {

0 commit comments

Comments
 (0)