Skip to content

Commit f07e04f

Browse files
committed
fix(desktop): await Linux relaunch helper startup
1 parent 3efac70 commit f07e04f

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

‎apps/desktop/src/electron/ElectronApp.test.ts‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import * as NodeReadline from "node:readline";
33

44
import { assert, describe, it } from "@effect/vitest";
55
import * as HostProcess from "@t3tools/shared/HostProcess";
6+
import * as Cause from "effect/Cause";
67
import * as Effect from "effect/Effect";
8+
import * as Fiber from "effect/Fiber";
79
import { beforeEach, vi } from "vite-plus/test";
810

911
const { getSystemLocaleMock, getUserDefaultMock, relaunchMock, spawnMock, unrefMock } = vi.hoisted(
@@ -13,7 +15,7 @@ const { getSystemLocaleMock, getUserDefaultMock, relaunchMock, spawnMock, unrefM
1315
getSystemLocaleMock: vi.fn(() => "en-GB"),
1416
getUserDefaultMock: vi.fn((_key: string) => false),
1517
relaunchMock: vi.fn(),
16-
spawnMock: vi.fn(() => ({ unref: unrefMock })),
18+
spawnMock: vi.fn<() => NodeEvents.EventEmitter & { unref: () => void }>(),
1719
unrefMock,
1820
};
1921
},
@@ -38,7 +40,11 @@ describe("ElectronApp", () => {
3840
getSystemLocaleMock.mockReset();
3941
getUserDefaultMock.mockReset();
4042
relaunchMock.mockReset();
41-
spawnMock.mockClear();
43+
spawnMock.mockReset().mockImplementation(() => {
44+
const child = Object.assign(new NodeEvents.EventEmitter(), { unref: unrefMock });
45+
queueMicrotask(() => child.emit("spawn"));
46+
return child;
47+
});
4248
unrefMock.mockReset();
4349
});
4450

@@ -78,6 +84,41 @@ describe("ElectronApp", () => {
7884
}).pipe(Effect.provide(ElectronApp.layer)),
7985
);
8086

87+
it.effect("waits for the shell to spawn and preserves asynchronous startup errors", () =>
88+
Effect.gen(function* () {
89+
const electronApp = yield* ElectronApp.ElectronApp;
90+
for (const event of ["spawn", "error"] as const) {
91+
const child = Object.assign(new NodeEvents.EventEmitter(), { unref: vi.fn() });
92+
const started = Promise.withResolvers<void>();
93+
const cause = new Error("shell spawn failed");
94+
spawnMock.mockImplementationOnce(() => {
95+
started.resolve();
96+
return child;
97+
});
98+
const relaunch = yield* electronApp
99+
.relaunch({ execPath: "/opt/t3code/t3code" })
100+
.pipe(
101+
Effect.provideService(HostProcess.Platform, "linux"),
102+
Effect.provideService(HostProcess.Environment, {}),
103+
Effect.exit,
104+
Effect.forkChild({ startImmediately: true }),
105+
);
106+
yield* Effect.promise(() => started.promise);
107+
assert.isUndefined(relaunch.pollUnsafe());
108+
child.emit(event, cause);
109+
const exit = yield* Fiber.join(relaunch);
110+
if (event === "spawn") {
111+
assert.strictEqual(exit._tag, "Success");
112+
assert.strictEqual(child.unref.mock.calls.length, 1);
113+
} else {
114+
assert.strictEqual(exit._tag, "Failure");
115+
if (exit._tag === "Failure") assert.strictEqual(Cause.squash(exit.cause), cause);
116+
assert.strictEqual(child.unref.mock.calls.length, 0);
117+
}
118+
}
119+
}).pipe(Effect.provide(ElectronApp.layer)),
120+
);
121+
81122
it.effect("uses an AppImage only when its mount contains the current executable", () =>
82123
Effect.gen(function* () {
83124
const electronApp = yield* ElectronApp.ElectronApp;

‎apps/desktop/src/electron/ElectronApp.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,16 @@ export const make = ElectronApp.of({
224224
appImage && execPath === currentExecPath && isAppImageExecutable ? appImage : execPath,
225225
options.args ?? process.argv.slice(1),
226226
);
227-
yield* Effect.sync(() => {
228-
NodeChildProcess.spawn(launch.command, launch.args, {
227+
yield* Effect.callback<void>((resume) => {
228+
const child = NodeChildProcess.spawn(launch.command, launch.args, {
229229
detached: true,
230230
stdio: "ignore",
231-
}).unref();
231+
});
232+
child.once("spawn", () => {
233+
child.unref();
234+
resume(Effect.void);
235+
});
236+
child.once("error", (cause) => resume(Effect.die(cause)));
232237
});
233238
}),
234239
setPath: Effect.fn("ElectronApp.setPath")(function (name, path) {

0 commit comments

Comments
 (0)