TypeScript
fix(desktop): a settings relaunch no longer replays the link that launched the app (#28464)
Victor Vogel6014358bcf3e
diff --git a/apps/desktop/src/app/DesktopLifecycle.test.ts b/apps/desktop/src/app/DesktopLifecycle.test.tsindex 6a6e991fc6b..fa1bcd8bbf0 100644--- a/apps/desktop/src/app/DesktopLifecycle.test.ts+++ b/apps/desktop/src/app/DesktopLifecycle.test.ts@@ -5,6 +5,8 @@ import * as Layer from "effect/Layer";import * as Ref from "effect/Ref";import * as TestClock from "effect/testing/TestClock";+import * as HostProcess from "@t3tools/shared/HostProcess";+import type * as Electron from "electron";import * as ElectronApp from "../electron/ElectronApp.ts";@@ -20,6 +22,7 @@ function layerElectronApp(appListeners: Map<string, (...args: readonly unknown[]) => void>,quit: Effect.Effect<void> = Effect.void,exit: (code: number) => Effect.Effect<void> = () => Effect.void,+ relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void> = () => Effect.void,) {const registerListener = (eventName: string, listener: (...args: readonly unknown[]) => void) =>Effect.acquireRelease(@@ -40,7 +43,7 @@ function layerElectronApp(requestSingleInstanceLock: Effect.succeed(true),quit,exit,- relaunch: () => Effect.void,+ relaunch,setPath: () => Effect.void,setName: () => Effect.void,setAboutPanelOptions: () => Effect.void,@@ -223,6 +226,50 @@ describe("DesktopLifecycle", () => {}),);+ it.effect("relaunches without the app link that launched it", () =>+ Effect.gen(function* () {+ const relaunched = yield* Deferred.make<Electron.RelaunchOptions>();+ const layerEnvironment = Layer.succeed(DesktopEnvironment.DesktopEnvironment, {+ platform: "linux",+ isDevelopment: false,+ } as DesktopEnvironment.DesktopEnvironment["Service"]);+ const layer = DesktopLifecycle.layer.pipe(+ Layer.provideMerge(+ layerElectronApp(+ new Map(),+ Effect.void,+ () => Effect.void,+ (options) => Deferred.succeed(relaunched, options).pipe(Effect.asVoid),+ ),+ ),+ Layer.provideMerge(layerElectronTheme),+ Layer.provideMerge(layerElectronWindow()),+ Layer.provideMerge(layerDesktopWindow()),+ Layer.provideMerge(layerEnvironment),+ Layer.provideMerge(DesktopShutdown.layer),+ Layer.provideMerge(DesktopState.layer),+ );++ const options = yield* Effect.gen(function* () {+ const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;+ yield* (yield* DesktopShutdown.DesktopShutdown).markComplete;+ yield* lifecycle.relaunch("network access changed");+ return yield* Deferred.await(relaunched);+ }).pipe(+ Effect.provide(layer),+ Effect.provideService(HostProcess.ExecutablePath, "/opt/T3 Code/t3code"),+ Effect.provideService(HostProcess.Arguments, [+ "/opt/T3 Code/t3code",+ "--no-sandbox",+ "T3CODE://pair?token=spent",+ "t3code://new?prompt=hello",+ ]),+ );++ assert.deepEqual(options, { execPath: "/opt/T3 Code/t3code", args: ["--no-sandbox"] });+ }),+ );+it.effect("exits on a repeated quit only once shutdown has stalled", () =>Effect.gen(function* () {const appListeners = new Map<string, (...args: readonly unknown[]) => void>();diff --git a/apps/desktop/src/app/DesktopLifecycle.ts b/apps/desktop/src/app/DesktopLifecycle.tsindex 93347949e00..0b5039bd623 100644--- a/apps/desktop/src/app/DesktopLifecycle.ts+++ b/apps/desktop/src/app/DesktopLifecycle.ts@@ -9,10 +9,13 @@ import * as Scope from "effect/Scope";import type * as Electron from "electron";+import * as HostProcess from "@t3tools/shared/HostProcess";+import * as DesktopEnvironment from "./DesktopEnvironment.ts";import { makeComponentLogger } from "./DesktopObservability.ts";import * as DesktopShutdown from "./DesktopShutdown.ts";import * as ElectronApp from "../electron/ElectronApp.ts";+import * as ElectronProtocol from "../electron/ElectronProtocol.ts";import * as ElectronTheme from "../electron/ElectronTheme.ts";import * as ElectronWindow from "../electron/ElectronWindow.ts";import * as DesktopState from "./DesktopState.ts";@@ -223,9 +226,12 @@ export const make = DesktopLifecycle.of({yield* electronApp.exit(75);return;}+ // Startup already acted on the app link that launched it; passing it on replays it.+ const appLinkPrefix = `${ElectronProtocol.getDesktopScheme(environment.isDevelopment)}:`;+ const args = yield* HostProcess.Arguments;yield* electronApp.relaunch({- execPath: process.execPath,- args: process.argv.slice(1),+ execPath: yield* HostProcess.ExecutablePath,+ args: args.slice(1).filter((arg) => !arg.toLowerCase().startsWith(appLinkPrefix)),});yield* electronApp.exit(0);}).pipe(