Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(cli): drop activates that come right after a GUI launch

Etienne Lescota999816b33a1
diff --git a/electron/cli/openGui.test.ts b/electron/cli/openGui.test.ts
index 193b6763e..d6c00230f 100644
--- a/electron/cli/openGui.test.ts
+++ b/electron/cli/openGui.test.ts
@@ -30,8 +30,10 @@ describe("openGuiFromCli", () => {
spawn.mockReturnValue({ on: vi.fn(), unref: vi.fn() });
});
+ // Each test launches at its own time, far from the others, so the launch guard never
+ // carries over between them.
it("opens the bundle as a new instance, detached", () => {
- openGuiFromCli("/Applications/Openscreen.app/Contents/MacOS/Openscreen");
+ openGuiFromCli("/Applications/Openscreen.app/Contents/MacOS/Openscreen", 1_000_000);
expect(spawn).toHaveBeenCalledWith("open", ["-n", "/Applications/Openscreen.app"], {
detached: true,
stdio: "ignore",
@@ -39,7 +41,17 @@ describe("openGuiFromCli", () => {
});
it("does nothing outside a bundle", () => {
- openGuiFromCli("/usr/local/bin/openscreen");
+ openGuiFromCli("/usr/local/bin/openscreen", 2_000_000);
expect(spawn).not.toHaveBeenCalled();
});
+
+ it("drops a second activate that comes right after a launch", () => {
+ const exe = "/Applications/Openscreen.app/Contents/MacOS/Openscreen";
+ openGuiFromCli(exe, 3_000_000);
+ openGuiFromCli(exe, 3_000_500);
+ openGuiFromCli(exe, 3_001_999);
+ expect(spawn).toHaveBeenCalledTimes(1);
+ openGuiFromCli(exe, 3_002_000);
+ expect(spawn).toHaveBeenCalledTimes(2);
+ });
});
diff --git a/electron/cli/openGui.ts b/electron/cli/openGui.ts
index a6841edcc..4df915c9a 100644
--- a/electron/cli/openGui.ts
+++ b/electron/cli/openGui.ts
@@ -26,10 +26,23 @@ export function appBundleOf(execPath: string): string | null {
return bundle;
}
+/**
+ * One launch can arrive as several `activate`s (a double-click, a Dock click while the GUI is
+ * still starting). Every extra `open -n` would start an instance only for it to lose the
+ * single-instance lock and quit, so the ones this soon after a launch are dropped.
+ */
+const LAUNCH_GUARD_MS = 2000;
+let lastLaunchAt = Number.NEGATIVE_INFINITY;
+
/** Start the GUI from its bundle as a new instance. A no-op outside a packaged `.app`. */
-export function openGuiFromCli(execPath: string = process.execPath): void {
+export function openGuiFromCli(
+ execPath: string = process.execPath,
+ now: number = Date.now(),
+): void {
const bundle = appBundleOf(execPath);
if (!bundle) return;
+ if (now - lastLaunchAt < LAUNCH_GUARD_MS) return;
+ lastLaunchAt = now;
const child = spawn("open", ["-n", bundle], { detached: true, stdio: "ignore" });
child.on("error", () => {
// Nothing to report to: stdout and stderr belong to the CLI protocol.