Skip to content

Commit b584192

Browse files
Here we go again.
1 parent 7a22d60 commit b584192

File tree

13 files changed

+748
-107
lines changed

13 files changed

+748
-107
lines changed

client/app/root.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {Icon, href as iconsHref} from "@thorium/ui/Icon";
1919
import {ReactNode} from "react";
2020
import {getBackground} from "./utils/getBackground";
2121
import {ClientOnly} from "remix-utils/client-only";
22-
import NoMatch from "./components/NotFound";
22+
import NoMatch from "./components/NotFound/index.client";
2323
import Button from "@thorium/ui/Button";
2424

2525
export const meta: MetaFunction = () => {

desktop/main/electron.ts

+20-31
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import {ipcHandlers} from "./helpers/ipcHandlers";
1111
import {autoUpdater} from "electron-updater";
1212
import {initWin} from "./helpers/autoUpdate";
13+
import {port} from "./helpers/settings";
1314

1415
let win: BrowserWindow | null = null;
1516
app.enableSandbox();
@@ -29,22 +30,14 @@ app.on("will-finish-launching", () => {
2930
});
3031
});
3132

32-
const cert = fs.readFileSync(
33-
path.join(
34-
app.getAppPath(),
35-
is.development ? `desktop/resources/server.cert` : `../app/server.cert`
36-
),
37-
"utf8"
38-
);
39-
const port = Number(process.env.PORT) || 4444;
40-
4133
async function createWindow() {
42-
await startThoriumServer();
43-
ipcHandlers();
44-
loaded = true;
45-
if (loadedPath) {
46-
loadFile(loadedPath);
47-
}
34+
const cert = fs.readFileSync(
35+
path.join(
36+
app.getAppPath(),
37+
is.development ? `desktop/resources/server.cert` : `../app/server.cert`
38+
),
39+
"utf8"
40+
);
4841
// TODO: Manage this with the multi-window manager some day
4942
app.on(
5043
"certificate-error",
@@ -55,7 +48,7 @@ async function createWindow() {
5548
callback(certificate.data === cert);
5649
}
5750
);
58-
restoreMenubar(app);
51+
5952
win = new BrowserWindow({
6053
width: 1024,
6154
height: 768,
@@ -71,33 +64,29 @@ async function createWindow() {
7164
},
7265
show: false,
7366
});
67+
7468
initWin(win);
75-
win.webContents.setWindowOpenHandler(({url}) => {
76-
shell.openExternal(url);
77-
return {action: "deny"};
78-
});
69+
await win.loadFile("index.html");
70+
await startThoriumServer();
71+
ipcHandlers();
72+
loaded = true;
73+
if (loadedPath) {
74+
loadFile(loadedPath);
75+
}
76+
restoreMenubar(app);
7977

8078
// We add 1 to the port, since we want to connect to the HTTPS server
8179
// which is 1 more than the default port
82-
win.loadURL(`http://localhost:${port}`);
83-
win.on("closed", () => {
84-
win = null;
85-
});
86-
win.on("ready-to-show", () => {
87-
if (win) {
88-
win.show();
89-
win.focus();
90-
}
91-
});
80+
await win.loadURL(`http://0.0.0.0:${port}`);
9281
}
9382

9483
app.whenReady().then(async () => {
84+
await createWindow();
9585
try {
9686
await autoUpdater.checkForUpdatesAndNotify();
9787
} catch (error) {
9888
// Ignore it
9989
}
100-
await createWindow();
10190
});
10291

10392
app.on("window-all-closed", async () => {

desktop/main/helpers/autoUpdate.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
import {autoUpdater} from "electron-updater";
2-
import {BrowserWindow} from "electron";
2+
import {BrowserWindow, shell} from "electron";
33

44
let win: BrowserWindow | null = null;
55
export function initWin(newWin: BrowserWindow) {
66
win = newWin;
7+
win.webContents.setWindowOpenHandler(({url}) => {
8+
shell.openExternal(url);
9+
return {action: "deny"};
10+
});
11+
win.on("closed", () => {
12+
win = null;
13+
});
14+
win.on("ready-to-show", () => {
15+
if (win) {
16+
win.show();
17+
win.focus();
18+
}
19+
});
720
}
821
function sendStatusToWindow(text: string) {
922
win?.webContents.send("update-message", text);

desktop/main/helpers/settings.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export const settings = new Store<{recentDocs: string[]}>({
55
recentDocs: [],
66
},
77
});
8+
9+
export const port = Number(process.env.PORT) || 4444;

desktop/main/helpers/startThoriumServer.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {app} from "electron";
22
import path from "path";
33
import {fork, ChildProcess} from "child_process";
44
import {hostSecret} from "../hostSecret";
5+
import waitOn from "wait-on";
6+
import {port} from "./settings";
57

68
let child: ChildProcess | null = null;
79
export async function startThoriumServer() {
@@ -23,11 +25,6 @@ export async function startThoriumServer() {
2325
// ],
2426
silent: true,
2527
});
26-
child.once("message", function (msg) {
27-
if (msg === "ready") {
28-
} else if (msg === "error") {
29-
}
30-
});
3128
child.stdout?.on("data", function (data) {
3229
const message: string = data.toString();
3330
console.info(message);
@@ -41,7 +38,9 @@ export async function startThoriumServer() {
4138
console.error(err);
4239
});
4340

44-
await new Promise(res => setTimeout(res, 1000));
41+
await waitOn({
42+
resources: [`http://0.0.0.0:${port}/healthcheck`],
43+
});
4544
}
4645

4746
app.on("before-quit", async event => {
@@ -55,14 +54,7 @@ app.on("before-quit", async event => {
5554
export async function stopThoriumServer() {
5655
if (child) {
5756
await Promise.race([
58-
new Promise<void>(res => {
59-
child?.once("message", message => {
60-
if (message === "saved") {
61-
res();
62-
}
63-
});
64-
child?.send("save");
65-
}),
57+
fetch(`http://0.0.0.0:${port}/snapshot`, {method: "POST"}),
6658
new Promise(res => setTimeout(res, 5000)),
6759
]);
6860
child.kill();

desktop/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"author": "Alex Anderson",
1212
"devDependencies": {
1313
"@types/bonjour": "^3.5.13",
14+
"@types/wait-on": "^5.3.4",
1415
"copy-dir-cli": "^0.0.3",
1516
"electron-esbuild": "^8.0.0"
1617
},
@@ -19,6 +20,7 @@
1920
"electron-better-ipc": "^2.0.1",
2021
"electron-store": "^8.1.0",
2122
"electron-updater": "^6.1.7",
22-
"electron-util": "^0.17.2"
23+
"electron-util": "^0.17.2",
24+
"wait-on": "^7.2.0"
2325
}
2426
}

0 commit comments

Comments
 (0)